blob: c6122a7fc8195b775af392a12b7819a1b96e1559 [file] [log] [blame]
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +00001/*
2 * Copyright (c) 2013 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 */
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +000010#include <algorithm> // max
kwiberg27f982b2016-03-01 11:52:33 -080011#include <memory>
magjed@webrtc.org2d2a30c2015-03-24 12:37:36 +000012#include <vector>
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "call/call.h"
15#include "call/rtp_transport_controller_send.h"
16#include "common_video/include/frame_callback.h"
17#include "common_video/include/video_frame.h"
18#include "modules/rtp_rtcp/include/rtp_header_parser.h"
19#include "modules/rtp_rtcp/include/rtp_rtcp.h"
20#include "modules/rtp_rtcp/source/rtcp_sender.h"
21#include "modules/rtp_rtcp/source/rtp_format_vp9.h"
22#include "modules/video_coding/codecs/vp8/include/vp8.h"
23#include "modules/video_coding/codecs/vp9/include/vp9.h"
24#include "rtc_base/bind.h"
25#include "rtc_base/checks.h"
26#include "rtc_base/criticalsection.h"
27#include "rtc_base/event.h"
Sebastian Janssoncabe3832018-01-12 10:54:18 +010028#include "rtc_base/experiments/alr_experiment.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/logging.h"
30#include "rtc_base/platform_thread.h"
31#include "rtc_base/rate_limiter.h"
32#include "rtc_base/timeutils.h"
33#include "system_wrappers/include/sleep.h"
34#include "test/call_test.h"
35#include "test/configurable_frame_size_encoder.h"
Niels Möller4db138e2018-04-19 09:04:13 +020036#include "test/encoder_proxy_factory.h"
37#include "test/fake_encoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "test/fake_texture_frame.h"
39#include "test/field_trial.h"
40#include "test/frame_generator.h"
41#include "test/frame_generator_capturer.h"
42#include "test/frame_utils.h"
Danil Chapovalov45d725d2018-02-19 19:09:53 +010043#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/gtest.h"
45#include "test/null_transport.h"
46#include "test/rtcp_packet_parser.h"
47#include "test/testsupport/perf_test.h"
perkjfa10b552016-10-02 23:45:26 -070048
Sebastian Janssona45c8da2018-01-16 10:55:29 +010049#include "call/video_send_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "video/send_statistics_proxy.h"
51#include "video/transport_adapter.h"
Sebastian Janssona45c8da2018-01-16 10:55:29 +010052#include "video/video_send_stream.h"
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +000053
54namespace webrtc {
Sebastian Janssona45c8da2018-01-16 10:55:29 +010055namespace test {
56class VideoSendStreamPeer {
57 public:
58 explicit VideoSendStreamPeer(webrtc::VideoSendStream* base_class_stream)
59 : internal_stream_(
60 static_cast<internal::VideoSendStream*>(base_class_stream)) {}
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020061 absl::optional<float> GetPacingFactorOverride() const {
Sebastian Janssona45c8da2018-01-16 10:55:29 +010062 return internal_stream_->GetPacingFactorOverride();
63 }
64
65 private:
66 internal::VideoSendStream const* const internal_stream_;
67};
68} // namespace test
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +000069
Yves Gerey665174f2018-06-19 15:03:05 +020070enum VideoFormat {
71 kGeneric,
72 kVP8,
73};
sprang@webrtc.org346094c2014-02-18 08:40:33 +000074
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070075void ExpectEqualFramesVector(const std::vector<VideoFrame>& frames1,
76 const std::vector<VideoFrame>& frames2);
77VideoFrame CreateVideoFrame(int width, int height, uint8_t data);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +000078
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000079class VideoSendStreamTest : public test::CallTest {
pbos@webrtc.org013d9942013-08-22 09:42:17 +000080 protected:
stefan@webrtc.org69969e22013-11-15 12:32:15 +000081 void TestNackRetransmission(uint32_t retransmit_ssrc,
stefan@webrtc.orgcb254aa2014-06-12 15:12:25 +000082 uint8_t retransmit_payload_type);
sprang@webrtc.org346094c2014-02-18 08:40:33 +000083 void TestPacketFragmentationSize(VideoFormat format, bool with_fec);
Åsa Perssonff24c042015-12-04 10:58:08 +010084
85 void TestVp9NonFlexMode(uint8_t num_temporal_layers,
86 uint8_t num_spatial_layers);
perkj803d97f2016-11-01 11:45:46 -070087
88 void TestRequestSourceRotateVideo(bool support_orientation_ext);
pbos@webrtc.org013d9942013-08-22 09:42:17 +000089};
90
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +000091TEST_F(VideoSendStreamTest, CanStartStartedStream) {
eladalon413ee9a2017-08-22 04:02:52 -070092 task_queue_.SendTask([this]() {
93 CreateSenderCall(Call::Config(event_log_.get()));
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +000094
eladalon413ee9a2017-08-22 04:02:52 -070095 test::NullTransport transport;
96 CreateSendConfig(1, 0, 0, &transport);
97 CreateVideoStreams();
98 video_send_stream_->Start();
99 video_send_stream_->Start();
100 DestroyStreams();
101 DestroyCalls();
102 });
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +0000103}
104
105TEST_F(VideoSendStreamTest, CanStopStoppedStream) {
eladalon413ee9a2017-08-22 04:02:52 -0700106 task_queue_.SendTask([this]() {
107 CreateSenderCall(Call::Config(event_log_.get()));
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +0000108
eladalon413ee9a2017-08-22 04:02:52 -0700109 test::NullTransport transport;
110 CreateSendConfig(1, 0, 0, &transport);
111 CreateVideoStreams();
112 video_send_stream_->Stop();
113 video_send_stream_->Stop();
114 DestroyStreams();
115 DestroyCalls();
116 });
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +0000117}
118
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000119TEST_F(VideoSendStreamTest, SupportsCName) {
120 static std::string kCName = "PjQatC14dGfbVwGPUOA9IH7RlsFDbWl4AhXEiDsBizo=";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000121 class CNameObserver : public test::SendTest {
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000122 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000123 CNameObserver() : SendTest(kDefaultTimeoutMs) {}
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000124
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000125 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000126 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
danilchap3dc929e2016-11-02 08:21:59 -0700127 test::RtcpPacketParser parser;
128 EXPECT_TRUE(parser.Parse(packet, length));
129 if (parser.sdes()->num_packets() > 0) {
130 EXPECT_EQ(1u, parser.sdes()->chunks().size());
131 EXPECT_EQ(kCName, parser.sdes()->chunks()[0].cname);
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000132
danilchap3dc929e2016-11-02 08:21:59 -0700133 observation_complete_.Set();
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000134 }
135
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000136 return SEND_PACKET;
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000137 }
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000138
stefanff483612015-12-21 03:14:00 -0800139 void ModifyVideoConfigs(
140 VideoSendStream::Config* send_config,
141 std::vector<VideoReceiveStream::Config>* receive_configs,
142 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000143 send_config->rtp.c_name = kCName;
144 }
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000145
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000146 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100147 EXPECT_TRUE(Wait()) << "Timed out while waiting for RTCP with CNAME.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000148 }
149 } test;
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000150
stefane74eef12016-01-08 06:47:13 -0800151 RunBaseTest(&test);
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +0000152}
153
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000154TEST_F(VideoSendStreamTest, SupportsAbsoluteSendTime) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000155 class AbsoluteSendTimeObserver : public test::SendTest {
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000156 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000157 AbsoluteSendTimeObserver() : SendTest(kDefaultTimeoutMs) {
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000158 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
Stefan Holmer4654d202015-12-08 09:10:43 +0100159 kRtpExtensionAbsoluteSendTime, test::kAbsSendTimeExtensionId));
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000160 }
161
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000162 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000163 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000164 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000165
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000166 EXPECT_FALSE(header.extension.hasTransmissionTimeOffset);
167 EXPECT_TRUE(header.extension.hasAbsoluteSendTime);
168 EXPECT_EQ(header.extension.transmissionTimeOffset, 0);
skvladc3f35152016-09-02 13:23:46 -0700169 if (header.extension.absoluteSendTime != 0) {
170 // Wait for at least one packet with a non-zero send time. The send time
171 // is a 16-bit value derived from the system clock, and it is valid
172 // for a packet to have a zero send time. To tell that from an
173 // unpopulated value we'll wait for a packet with non-zero send time.
174 observation_complete_.Set();
175 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100176 RTC_LOG(LS_WARNING)
177 << "Got a packet with zero absoluteSendTime, waiting"
178 " for another packet...";
skvladc3f35152016-09-02 13:23:46 -0700179 }
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000180
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000181 return SEND_PACKET;
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000182 }
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000183
stefanff483612015-12-21 03:14:00 -0800184 void ModifyVideoConfigs(
185 VideoSendStream::Config* send_config,
186 std::vector<VideoReceiveStream::Config>* receive_configs,
187 VideoEncoderConfig* encoder_config) override {
Erik Språng95261872015-04-10 11:58:49 +0200188 send_config->rtp.extensions.clear();
Stefan Holmer4654d202015-12-08 09:10:43 +0100189 send_config->rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700190 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000191 }
pbos@webrtc.orgdde16f12014-08-05 23:35:43 +0000192
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000193 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100194 EXPECT_TRUE(Wait()) << "Timed out while waiting for single RTP packet.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000195 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000196 } test;
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000197
stefane74eef12016-01-08 06:47:13 -0800198 RunBaseTest(&test);
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000199}
200
pbos@webrtc.org29023282013-09-11 10:14:56 +0000201TEST_F(VideoSendStreamTest, SupportsTransmissionTimeOffset) {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000202 static const int kEncodeDelayMs = 5;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000203 class TransmissionTimeOffsetObserver : public test::SendTest {
pbos@webrtc.org29023282013-09-11 10:14:56 +0000204 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000205 TransmissionTimeOffsetObserver()
Niels Möller4db138e2018-04-19 09:04:13 +0200206 : SendTest(kDefaultTimeoutMs), encoder_factory_([]() {
207 return rtc::MakeUnique<test::DelayedEncoder>(
208 Clock::GetRealTimeClock(), kEncodeDelayMs);
209 }) {
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000210 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
Stefan Holmer12952972015-10-29 15:13:24 +0100211 kRtpExtensionTransmissionTimeOffset, test::kTOffsetExtensionId));
pbos@webrtc.org29023282013-09-11 10:14:56 +0000212 }
213
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000214 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000215 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org29023282013-09-11 10:14:56 +0000216 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000217 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org29023282013-09-11 10:14:56 +0000218
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000219 EXPECT_TRUE(header.extension.hasTransmissionTimeOffset);
220 EXPECT_FALSE(header.extension.hasAbsoluteSendTime);
pbos@webrtc.org29023282013-09-11 10:14:56 +0000221 EXPECT_GT(header.extension.transmissionTimeOffset, 0);
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000222 EXPECT_EQ(header.extension.absoluteSendTime, 0u);
Peter Boström5811a392015-12-10 13:02:50 +0100223 observation_complete_.Set();
pbos@webrtc.org29023282013-09-11 10:14:56 +0000224
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000225 return SEND_PACKET;
pbos@webrtc.org29023282013-09-11 10:14:56 +0000226 }
pbos@webrtc.org29023282013-09-11 10:14:56 +0000227
stefanff483612015-12-21 03:14:00 -0800228 void ModifyVideoConfigs(
229 VideoSendStream::Config* send_config,
230 std::vector<VideoReceiveStream::Config>* receive_configs,
231 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +0200232 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Stefan Holmer12952972015-10-29 15:13:24 +0100233 send_config->rtp.extensions.clear();
isheriff6f8d6862016-05-26 11:24:55 -0700234 send_config->rtp.extensions.push_back(RtpExtension(
235 RtpExtension::kTimestampOffsetUri, test::kTOffsetExtensionId));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000236 }
pbos@webrtc.org29023282013-09-11 10:14:56 +0000237
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000238 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100239 EXPECT_TRUE(Wait()) << "Timed out while waiting for a single RTP packet.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000240 }
pbos@webrtc.org29023282013-09-11 10:14:56 +0000241
Niels Möller4db138e2018-04-19 09:04:13 +0200242 test::FunctionVideoEncoderFactory encoder_factory_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000243 } test;
244
stefane74eef12016-01-08 06:47:13 -0800245 RunBaseTest(&test);
pbos@webrtc.org29023282013-09-11 10:14:56 +0000246}
247
sprang867fb522015-08-03 04:38:41 -0700248TEST_F(VideoSendStreamTest, SupportsTransportWideSequenceNumbers) {
danilchap42ca68a2016-10-31 03:34:40 -0700249 static const uint8_t kExtensionId = test::kTransportSequenceNumberExtensionId;
sprang867fb522015-08-03 04:38:41 -0700250 class TransportWideSequenceNumberObserver : public test::SendTest {
251 public:
252 TransportWideSequenceNumberObserver()
Niels Möller4db138e2018-04-19 09:04:13 +0200253 : SendTest(kDefaultTimeoutMs), encoder_factory_([]() {
254 return rtc::MakeUnique<test::FakeEncoder>(
255 Clock::GetRealTimeClock());
256 }) {
sprang867fb522015-08-03 04:38:41 -0700257 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
258 kRtpExtensionTransportSequenceNumber, kExtensionId));
259 }
260
261 private:
262 Action OnSendRtp(const uint8_t* packet, size_t length) override {
263 RTPHeader header;
264 EXPECT_TRUE(parser_->Parse(packet, length, &header));
265
266 EXPECT_TRUE(header.extension.hasTransportSequenceNumber);
267 EXPECT_FALSE(header.extension.hasTransmissionTimeOffset);
268 EXPECT_FALSE(header.extension.hasAbsoluteSendTime);
269
Peter Boström5811a392015-12-10 13:02:50 +0100270 observation_complete_.Set();
sprang867fb522015-08-03 04:38:41 -0700271
272 return SEND_PACKET;
273 }
274
stefanff483612015-12-21 03:14:00 -0800275 void ModifyVideoConfigs(
276 VideoSendStream::Config* send_config,
277 std::vector<VideoReceiveStream::Config>* receive_configs,
278 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +0200279 send_config->encoder_settings.encoder_factory = &encoder_factory_;
sprang867fb522015-08-03 04:38:41 -0700280 }
281
282 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100283 EXPECT_TRUE(Wait()) << "Timed out while waiting for a single RTP packet.";
sprang867fb522015-08-03 04:38:41 -0700284 }
285
Niels Möller4db138e2018-04-19 09:04:13 +0200286 test::FunctionVideoEncoderFactory encoder_factory_;
sprang867fb522015-08-03 04:38:41 -0700287 } test;
288
stefane74eef12016-01-08 06:47:13 -0800289 RunBaseTest(&test);
sprang867fb522015-08-03 04:38:41 -0700290}
291
perkj803d97f2016-11-01 11:45:46 -0700292TEST_F(VideoSendStreamTest, SupportsVideoRotation) {
293 class VideoRotationObserver : public test::SendTest {
294 public:
295 VideoRotationObserver() : SendTest(kDefaultTimeoutMs) {
296 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
297 kRtpExtensionVideoRotation, test::kVideoRotationExtensionId));
298 }
299
300 Action OnSendRtp(const uint8_t* packet, size_t length) override {
301 RTPHeader header;
302 EXPECT_TRUE(parser_->Parse(packet, length, &header));
ilnik7a3006b2017-05-23 09:34:21 -0700303 // Only the last packet of the frame is required to have the extension.
304 if (!header.markerBit)
305 return SEND_PACKET;
perkj803d97f2016-11-01 11:45:46 -0700306 EXPECT_TRUE(header.extension.hasVideoRotation);
307 EXPECT_EQ(kVideoRotation_90, header.extension.videoRotation);
308 observation_complete_.Set();
309 return SEND_PACKET;
310 }
311
312 void ModifyVideoConfigs(
313 VideoSendStream::Config* send_config,
314 std::vector<VideoReceiveStream::Config>* receive_configs,
315 VideoEncoderConfig* encoder_config) override {
316 send_config->rtp.extensions.clear();
317 send_config->rtp.extensions.push_back(RtpExtension(
318 RtpExtension::kVideoRotationUri, test::kVideoRotationExtensionId));
319 }
320
321 void OnFrameGeneratorCapturerCreated(
322 test::FrameGeneratorCapturer* frame_generator_capturer) override {
323 frame_generator_capturer->SetFakeRotation(kVideoRotation_90);
324 }
325
326 void PerformTest() override {
327 EXPECT_TRUE(Wait()) << "Timed out while waiting for single RTP packet.";
328 }
329 } test;
330
331 RunBaseTest(&test);
332}
333
ilnik00d802b2017-04-11 10:34:31 -0700334TEST_F(VideoSendStreamTest, SupportsVideoContentType) {
ilnik10894992017-06-21 08:23:19 -0700335 class VideoContentTypeObserver : public test::SendTest {
ilnik00d802b2017-04-11 10:34:31 -0700336 public:
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100337 VideoContentTypeObserver()
338 : SendTest(kDefaultTimeoutMs), first_frame_sent_(false) {
ilnik00d802b2017-04-11 10:34:31 -0700339 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
340 kRtpExtensionVideoContentType, test::kVideoContentTypeExtensionId));
341 }
342
343 Action OnSendRtp(const uint8_t* packet, size_t length) override {
344 RTPHeader header;
345 EXPECT_TRUE(parser_->Parse(packet, length, &header));
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100346 // Only the last packet of the key-frame must have extension.
347 if (!header.markerBit || first_frame_sent_)
ilnik7a3006b2017-05-23 09:34:21 -0700348 return SEND_PACKET;
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100349 // First marker bit seen means that the first frame is sent.
350 first_frame_sent_ = true;
ilnik00d802b2017-04-11 10:34:31 -0700351 EXPECT_TRUE(header.extension.hasVideoContentType);
ilnik6d5b4d62017-08-30 03:32:14 -0700352 EXPECT_TRUE(videocontenttypehelpers::IsScreenshare(
353 header.extension.videoContentType));
ilnik00d802b2017-04-11 10:34:31 -0700354 observation_complete_.Set();
355 return SEND_PACKET;
356 }
357
358 void ModifyVideoConfigs(
359 VideoSendStream::Config* send_config,
360 std::vector<VideoReceiveStream::Config>* receive_configs,
361 VideoEncoderConfig* encoder_config) override {
362 send_config->rtp.extensions.clear();
363 send_config->rtp.extensions.push_back(
364 RtpExtension(RtpExtension::kVideoContentTypeUri,
365 test::kVideoContentTypeExtensionId));
366 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
367 }
368
369 void PerformTest() override {
370 EXPECT_TRUE(Wait()) << "Timed out while waiting for single RTP packet.";
371 }
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100372
373 private:
374 bool first_frame_sent_;
ilnik00d802b2017-04-11 10:34:31 -0700375 } test;
376
377 RunBaseTest(&test);
378}
379
ilnik04f4d122017-06-19 07:18:55 -0700380TEST_F(VideoSendStreamTest, SupportsVideoTimingFrames) {
ilnik10894992017-06-21 08:23:19 -0700381 class VideoTimingObserver : public test::SendTest {
ilnik04f4d122017-06-19 07:18:55 -0700382 public:
ilnik10894992017-06-21 08:23:19 -0700383 VideoTimingObserver() : SendTest(kDefaultTimeoutMs) {
ilnik04f4d122017-06-19 07:18:55 -0700384 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
385 kRtpExtensionVideoTiming, test::kVideoTimingExtensionId));
386 }
387
388 Action OnSendRtp(const uint8_t* packet, size_t length) override {
389 RTPHeader header;
390 EXPECT_TRUE(parser_->Parse(packet, length, &header));
ilnik10894992017-06-21 08:23:19 -0700391 // Only the last packet of the frame must have extension.
392 if (!header.markerBit)
393 return SEND_PACKET;
394 EXPECT_TRUE(header.extension.has_video_timing);
395 observation_complete_.Set();
ilnik04f4d122017-06-19 07:18:55 -0700396 return SEND_PACKET;
397 }
398
399 void ModifyVideoConfigs(
400 VideoSendStream::Config* send_config,
401 std::vector<VideoReceiveStream::Config>* receive_configs,
402 VideoEncoderConfig* encoder_config) override {
403 send_config->rtp.extensions.clear();
404 send_config->rtp.extensions.push_back(RtpExtension(
405 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
406 }
407
408 void PerformTest() override {
409 EXPECT_TRUE(Wait()) << "Timed out while waiting for timing frames.";
410 }
411 } test;
412
413 RunBaseTest(&test);
414}
415
danilchap901b2df2017-07-28 08:56:04 -0700416class FakeReceiveStatistics : public ReceiveStatisticsProvider {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000417 public:
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +0000418 FakeReceiveStatistics(uint32_t send_ssrc,
419 uint32_t last_sequence_number,
420 uint32_t cumulative_lost,
danilchap901b2df2017-07-28 08:56:04 -0700421 uint8_t fraction_lost) {
422 stat_.SetMediaSsrc(send_ssrc);
423 stat_.SetExtHighestSeqNum(last_sequence_number);
424 stat_.SetCumulativeLost(cumulative_lost);
425 stat_.SetFractionLost(fraction_lost);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000426 }
427
danilchap901b2df2017-07-28 08:56:04 -0700428 std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override {
429 EXPECT_GE(max_blocks, 1u);
430 return {stat_};
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000431 }
432
433 private:
danilchap901b2df2017-07-28 08:56:04 -0700434 rtcp::ReportBlock stat_;
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000435};
436
brandtre602f0a2016-10-31 03:40:49 -0700437class UlpfecObserver : public test::EndToEndTest {
Stefan Holmer4654d202015-12-08 09:10:43 +0100438 public:
brandtre602f0a2016-10-31 03:40:49 -0700439 UlpfecObserver(bool header_extensions_enabled,
brandtr65a1e772016-12-12 01:54:58 -0800440 bool use_nack,
441 bool expect_red,
442 bool expect_ulpfec,
brandtr696c9c62016-12-19 05:47:28 -0800443 const std::string& codec,
Niels Möller4db138e2018-04-19 09:04:13 +0200444 VideoEncoderFactory* encoder_factory)
brandtr20d45472017-01-02 00:34:27 -0800445 : EndToEndTest(kTimeoutMs),
Niels Möller4db138e2018-04-19 09:04:13 +0200446 encoder_factory_(encoder_factory),
Peter Boström39593972016-02-15 11:27:15 +0100447 payload_name_(codec),
448 use_nack_(use_nack),
449 expect_red_(expect_red),
brandtre602f0a2016-10-31 03:40:49 -0700450 expect_ulpfec_(expect_ulpfec),
brandtr65a1e772016-12-12 01:54:58 -0800451 sent_media_(false),
452 sent_ulpfec_(false),
brandtr696c9c62016-12-19 05:47:28 -0800453 header_extensions_enabled_(header_extensions_enabled) {}
Stefan Holmer4654d202015-12-08 09:10:43 +0100454
brandtr20d45472017-01-02 00:34:27 -0800455 // Some of the test cases are expected to time out and thus we are using
456 // a shorter timeout window than the default here.
457 static constexpr size_t kTimeoutMs = 10000;
458
Stefan Holmer4654d202015-12-08 09:10:43 +0100459 private:
460 Action OnSendRtp(const uint8_t* packet, size_t length) override {
461 RTPHeader header;
462 EXPECT_TRUE(parser_->Parse(packet, length, &header));
463
Stefan Holmer4654d202015-12-08 09:10:43 +0100464 int encapsulated_payload_type = -1;
465 if (header.payloadType == VideoSendStreamTest::kRedPayloadType) {
Peter Boström39593972016-02-15 11:27:15 +0100466 EXPECT_TRUE(expect_red_);
Stefan Holmer4654d202015-12-08 09:10:43 +0100467 encapsulated_payload_type = static_cast<int>(packet[header.headerLength]);
468 if (encapsulated_payload_type !=
Peter Boström39593972016-02-15 11:27:15 +0100469 VideoSendStreamTest::kFakeVideoSendPayloadType) {
Stefan Holmer4654d202015-12-08 09:10:43 +0100470 EXPECT_EQ(VideoSendStreamTest::kUlpfecPayloadType,
471 encapsulated_payload_type);
Peter Boström39593972016-02-15 11:27:15 +0100472 }
Stefan Holmer4654d202015-12-08 09:10:43 +0100473 } else {
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100474 EXPECT_EQ(VideoSendStreamTest::kFakeVideoSendPayloadType,
475 header.payloadType);
Peter Boström39593972016-02-15 11:27:15 +0100476 if (static_cast<size_t>(header.headerLength + header.paddingLength) <
477 length) {
478 // Not padding-only, media received outside of RED.
479 EXPECT_FALSE(expect_red_);
brandtr65a1e772016-12-12 01:54:58 -0800480 sent_media_ = true;
Peter Boström39593972016-02-15 11:27:15 +0100481 }
Stefan Holmer4654d202015-12-08 09:10:43 +0100482 }
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000483
Stefan Holmer4654d202015-12-08 09:10:43 +0100484 if (header_extensions_enabled_) {
485 EXPECT_TRUE(header.extension.hasAbsoluteSendTime);
486 uint32_t kHalf24BitsSpace = 0xFFFFFF / 2;
487 if (header.extension.absoluteSendTime <= kHalf24BitsSpace &&
488 prev_header_.extension.absoluteSendTime > kHalf24BitsSpace) {
489 // 24 bits wrap.
490 EXPECT_GT(prev_header_.extension.absoluteSendTime,
491 header.extension.absoluteSendTime);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000492 } else {
Stefan Holmer4654d202015-12-08 09:10:43 +0100493 EXPECT_GE(header.extension.absoluteSendTime,
494 prev_header_.extension.absoluteSendTime);
Stefan Holmer01b48882015-05-05 10:21:24 +0200495 }
Stefan Holmer4654d202015-12-08 09:10:43 +0100496 EXPECT_TRUE(header.extension.hasTransportSequenceNumber);
497 uint16_t seq_num_diff = header.extension.transportSequenceNumber -
498 prev_header_.extension.transportSequenceNumber;
499 EXPECT_EQ(1, seq_num_diff);
500 }
Stefan Holmer01b48882015-05-05 10:21:24 +0200501
Stefan Holmer4654d202015-12-08 09:10:43 +0100502 if (encapsulated_payload_type != -1) {
503 if (encapsulated_payload_type ==
504 VideoSendStreamTest::kUlpfecPayloadType) {
brandtre602f0a2016-10-31 03:40:49 -0700505 EXPECT_TRUE(expect_ulpfec_);
brandtr65a1e772016-12-12 01:54:58 -0800506 sent_ulpfec_ = true;
Stefan Holmer4654d202015-12-08 09:10:43 +0100507 } else {
brandtr65a1e772016-12-12 01:54:58 -0800508 sent_media_ = true;
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000509 }
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000510 }
511
brandtr20d45472017-01-02 00:34:27 -0800512 if (sent_media_ && sent_ulpfec_) {
513 observation_complete_.Set();
Peter Boström39593972016-02-15 11:27:15 +0100514 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000515
Stefan Holmer4654d202015-12-08 09:10:43 +0100516 prev_header_ = header;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000517
Stefan Holmer4654d202015-12-08 09:10:43 +0100518 return SEND_PACKET;
519 }
520
eladalon413ee9a2017-08-22 04:02:52 -0700521 test::PacketTransport* CreateSendTransport(
522 test::SingleThreadedTaskQueueForTesting* task_queue,
523 Call* sender_call) override {
Peter Boström39593972016-02-15 11:27:15 +0100524 // At low RTT (< kLowRttNackMs) -> NACK only, no FEC.
525 // Configure some network delay.
526 const int kNetworkDelayMs = 100;
527 FakeNetworkPipe::Config config;
brandtr65a1e772016-12-12 01:54:58 -0800528 config.loss_percent = 5;
Peter Boström39593972016-02-15 11:27:15 +0100529 config.queue_delay_ms = kNetworkDelayMs;
minyue20c84cc2017-04-10 16:57:57 -0700530 return new test::PacketTransport(
eladalon413ee9a2017-08-22 04:02:52 -0700531 task_queue, sender_call, this, test::PacketTransport::kSender,
minyue20c84cc2017-04-10 16:57:57 -0700532 VideoSendStreamTest::payload_type_map_, config);
Peter Boström39593972016-02-15 11:27:15 +0100533 }
534
stefanff483612015-12-21 03:14:00 -0800535 void ModifyVideoConfigs(
536 VideoSendStream::Config* send_config,
537 std::vector<VideoReceiveStream::Config>* receive_configs,
538 VideoEncoderConfig* encoder_config) override {
Peter Boström39593972016-02-15 11:27:15 +0100539 if (use_nack_) {
540 send_config->rtp.nack.rtp_history_ms =
541 (*receive_configs)[0].rtp.nack.rtp_history_ms =
542 VideoSendStreamTest::kNackRtpHistoryMs;
543 }
Niels Möller4db138e2018-04-19 09:04:13 +0200544 send_config->encoder_settings.encoder_factory = encoder_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200545 send_config->rtp.payload_name = payload_name_;
brandtrb5f2c3f2016-10-04 23:28:39 -0700546 send_config->rtp.ulpfec.red_payload_type =
547 VideoSendStreamTest::kRedPayloadType;
548 send_config->rtp.ulpfec.ulpfec_payload_type =
549 VideoSendStreamTest::kUlpfecPayloadType;
stefanb77c7162017-02-06 06:29:38 -0800550 EXPECT_FALSE(send_config->rtp.extensions.empty());
551 if (!header_extensions_enabled_) {
552 send_config->rtp.extensions.clear();
553 } else {
Stefan Holmer4654d202015-12-08 09:10:43 +0100554 send_config->rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700555 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Stefan Holmer4654d202015-12-08 09:10:43 +0100556 }
Niels Möller259a4972018-04-05 15:36:51 +0200557 encoder_config->codec_type = PayloadStringToCodecType(payload_name_);
nisse3b3622f2017-09-26 02:49:21 -0700558 (*receive_configs)[0].rtp.red_payload_type =
brandtrb5f2c3f2016-10-04 23:28:39 -0700559 send_config->rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 02:49:21 -0700560 (*receive_configs)[0].rtp.ulpfec_payload_type =
brandtrb5f2c3f2016-10-04 23:28:39 -0700561 send_config->rtp.ulpfec.ulpfec_payload_type;
Stefan Holmer4654d202015-12-08 09:10:43 +0100562 }
563
564 void PerformTest() override {
brandtr20d45472017-01-02 00:34:27 -0800565 EXPECT_EQ(expect_ulpfec_, Wait())
566 << "Timed out waiting for ULPFEC and/or media packets.";
Stefan Holmer4654d202015-12-08 09:10:43 +0100567 }
568
Niels Möller4db138e2018-04-19 09:04:13 +0200569 VideoEncoderFactory* encoder_factory_;
brandtr696c9c62016-12-19 05:47:28 -0800570 std::string payload_name_;
Peter Boström39593972016-02-15 11:27:15 +0100571 const bool use_nack_;
572 const bool expect_red_;
brandtre602f0a2016-10-31 03:40:49 -0700573 const bool expect_ulpfec_;
brandtr65a1e772016-12-12 01:54:58 -0800574 bool sent_media_;
575 bool sent_ulpfec_;
Stefan Holmer4654d202015-12-08 09:10:43 +0100576 bool header_extensions_enabled_;
577 RTPHeader prev_header_;
578};
579
brandtre602f0a2016-10-31 03:40:49 -0700580TEST_F(VideoSendStreamTest, SupportsUlpfecWithExtensions) {
Niels Möller4db138e2018-04-19 09:04:13 +0200581 test::FunctionVideoEncoderFactory encoder_factory(
582 []() { return VP8Encoder::Create(); });
583 UlpfecObserver test(true, false, true, true, "VP8", &encoder_factory);
stefane74eef12016-01-08 06:47:13 -0800584 RunBaseTest(&test);
Stefan Holmer4654d202015-12-08 09:10:43 +0100585}
586
brandtre602f0a2016-10-31 03:40:49 -0700587TEST_F(VideoSendStreamTest, SupportsUlpfecWithoutExtensions) {
Niels Möller4db138e2018-04-19 09:04:13 +0200588 test::FunctionVideoEncoderFactory encoder_factory(
589 []() { return VP8Encoder::Create(); });
590 UlpfecObserver test(false, false, true, true, "VP8", &encoder_factory);
Peter Boström39593972016-02-15 11:27:15 +0100591 RunBaseTest(&test);
592}
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000593
stefan60e10c72017-08-23 10:40:00 -0700594class VideoSendStreamWithoutUlpfecTest : public VideoSendStreamTest {
595 protected:
596 VideoSendStreamWithoutUlpfecTest()
597 : field_trial_("WebRTC-DisableUlpFecExperiment/Enabled/") {}
598
599 test::ScopedFieldTrials field_trial_;
600};
601
602TEST_F(VideoSendStreamWithoutUlpfecTest, NoUlpfecIfDisabledThroughFieldTrial) {
Niels Möller4db138e2018-04-19 09:04:13 +0200603 test::FunctionVideoEncoderFactory encoder_factory(
604 []() { return VP8Encoder::Create(); });
605 UlpfecObserver test(false, false, true, false, "VP8", &encoder_factory);
stefan60e10c72017-08-23 10:40:00 -0700606 RunBaseTest(&test);
607}
608
Peter Boström39593972016-02-15 11:27:15 +0100609// The FEC scheme used is not efficient for H264, so we should not use RED/FEC
610// since we'll still have to re-request FEC packets, effectively wasting
611// bandwidth since the receiver has to wait for FEC retransmissions to determine
612// that the received state is actually decodable.
brandtre602f0a2016-10-31 03:40:49 -0700613TEST_F(VideoSendStreamTest, DoesNotUtilizeUlpfecForH264WithNackEnabled) {
Niels Möller4db138e2018-04-19 09:04:13 +0200614 test::FunctionVideoEncoderFactory encoder_factory([]() {
615 return rtc::MakeUnique<test::FakeH264Encoder>(Clock::GetRealTimeClock());
616 });
617 UlpfecObserver test(false, true, true, false, "H264", &encoder_factory);
Peter Boström39593972016-02-15 11:27:15 +0100618 RunBaseTest(&test);
619}
620
621// Without retransmissions FEC for H264 is fine.
brandtre6f98c72016-11-11 03:28:30 -0800622TEST_F(VideoSendStreamTest, DoesUtilizeUlpfecForH264WithoutNackEnabled) {
Niels Möller4db138e2018-04-19 09:04:13 +0200623 test::FunctionVideoEncoderFactory encoder_factory([]() {
624 return rtc::MakeUnique<test::FakeH264Encoder>(Clock::GetRealTimeClock());
625 });
626 UlpfecObserver test(false, false, true, true, "H264", &encoder_factory);
Peter Boström39593972016-02-15 11:27:15 +0100627 RunBaseTest(&test);
628}
629
danilchap9f5b6222017-03-02 06:22:21 -0800630// Disabled as flaky, see https://crbug.com/webrtc/7285 for details.
631TEST_F(VideoSendStreamTest, DISABLED_DoesUtilizeUlpfecForVp8WithNackEnabled) {
Niels Möller4db138e2018-04-19 09:04:13 +0200632 test::FunctionVideoEncoderFactory encoder_factory(
633 []() { return VP8Encoder::Create(); });
634 UlpfecObserver test(false, true, true, true, "VP8", &encoder_factory);
Peter Boström39593972016-02-15 11:27:15 +0100635 RunBaseTest(&test);
636}
637
Peter Boström12996152016-05-14 02:03:18 +0200638#if !defined(RTC_DISABLE_VP9)
danilchap9f5b6222017-03-02 06:22:21 -0800639// Disabled as flaky, see https://crbug.com/webrtc/7285 for details.
640TEST_F(VideoSendStreamTest, DISABLED_DoesUtilizeUlpfecForVp9WithNackEnabled) {
Niels Möller4db138e2018-04-19 09:04:13 +0200641 test::FunctionVideoEncoderFactory encoder_factory(
642 []() { return VP9Encoder::Create(); });
643 UlpfecObserver test(false, true, true, true, "VP9", &encoder_factory);
stefane74eef12016-01-08 06:47:13 -0800644 RunBaseTest(&test);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000645}
Peter Boström12996152016-05-14 02:03:18 +0200646#endif // !defined(RTC_DISABLE_VP9)
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000647
brandtre78d2662017-01-16 05:57:16 -0800648TEST_F(VideoSendStreamTest, SupportsUlpfecWithMultithreadedH264) {
Niels Möller4db138e2018-04-19 09:04:13 +0200649 test::FunctionVideoEncoderFactory encoder_factory([]() {
650 return rtc::MakeUnique<test::MultithreadedFakeH264Encoder>(
651 Clock::GetRealTimeClock());
652 });
653 UlpfecObserver test(false, false, true, true, "H264", &encoder_factory);
brandtr696c9c62016-12-19 05:47:28 -0800654 RunBaseTest(&test);
655}
656
brandtr39f97292016-11-16 22:57:50 -0800657// TODO(brandtr): Move these FlexFEC tests when we have created
658// FlexfecSendStream.
659class FlexfecObserver : public test::EndToEndTest {
660 public:
661 FlexfecObserver(bool header_extensions_enabled,
662 bool use_nack,
brandtr696c9c62016-12-19 05:47:28 -0800663 const std::string& codec,
Niels Möller4db138e2018-04-19 09:04:13 +0200664 VideoEncoderFactory* encoder_factory,
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100665 size_t num_video_streams)
brandtr39f97292016-11-16 22:57:50 -0800666 : EndToEndTest(VideoSendStreamTest::kDefaultTimeoutMs),
Niels Möller4db138e2018-04-19 09:04:13 +0200667 encoder_factory_(encoder_factory),
brandtr39f97292016-11-16 22:57:50 -0800668 payload_name_(codec),
669 use_nack_(use_nack),
brandtr39f97292016-11-16 22:57:50 -0800670 sent_media_(false),
671 sent_flexfec_(false),
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100672 header_extensions_enabled_(header_extensions_enabled),
673 num_video_streams_(num_video_streams) {}
brandtr39f97292016-11-16 22:57:50 -0800674
675 size_t GetNumFlexfecStreams() const override { return 1; }
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100676 size_t GetNumVideoStreams() const override { return num_video_streams_; }
brandtr39f97292016-11-16 22:57:50 -0800677
678 private:
679 Action OnSendRtp(const uint8_t* packet, size_t length) override {
680 RTPHeader header;
681 EXPECT_TRUE(parser_->Parse(packet, length, &header));
682
brandtr39f97292016-11-16 22:57:50 -0800683 if (header.payloadType == VideoSendStreamTest::kFlexfecPayloadType) {
684 EXPECT_EQ(VideoSendStreamTest::kFlexfecSendSsrc, header.ssrc);
685 sent_flexfec_ = true;
686 } else {
687 EXPECT_EQ(VideoSendStreamTest::kFakeVideoSendPayloadType,
688 header.payloadType);
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100689 EXPECT_THAT(testing::make_tuple(VideoSendStreamTest::kVideoSendSsrcs,
690 num_video_streams_),
691 testing::Contains(header.ssrc));
brandtr39f97292016-11-16 22:57:50 -0800692 sent_media_ = true;
693 }
694
695 if (header_extensions_enabled_) {
696 EXPECT_TRUE(header.extension.hasAbsoluteSendTime);
697 EXPECT_TRUE(header.extension.hasTransmissionTimeOffset);
698 EXPECT_TRUE(header.extension.hasTransportSequenceNumber);
699 }
700
brandtr0c5a1542016-11-23 04:42:26 -0800701 if (sent_media_ && sent_flexfec_) {
brandtr39f97292016-11-16 22:57:50 -0800702 observation_complete_.Set();
703 }
704
705 return SEND_PACKET;
706 }
707
eladalon413ee9a2017-08-22 04:02:52 -0700708 test::PacketTransport* CreateSendTransport(
709 test::SingleThreadedTaskQueueForTesting* task_queue,
710 Call* sender_call) override {
brandtr39f97292016-11-16 22:57:50 -0800711 // At low RTT (< kLowRttNackMs) -> NACK only, no FEC.
712 // Therefore we need some network delay.
713 const int kNetworkDelayMs = 100;
714 FakeNetworkPipe::Config config;
brandtrd654a9b2016-12-05 05:38:19 -0800715 config.loss_percent = 5;
brandtr39f97292016-11-16 22:57:50 -0800716 config.queue_delay_ms = kNetworkDelayMs;
minyue20c84cc2017-04-10 16:57:57 -0700717 return new test::PacketTransport(
eladalon413ee9a2017-08-22 04:02:52 -0700718 task_queue, sender_call, this, test::PacketTransport::kSender,
minyue20c84cc2017-04-10 16:57:57 -0700719 VideoSendStreamTest::payload_type_map_, config);
brandtr39f97292016-11-16 22:57:50 -0800720 }
721
722 void ModifyVideoConfigs(
723 VideoSendStream::Config* send_config,
724 std::vector<VideoReceiveStream::Config>* receive_configs,
725 VideoEncoderConfig* encoder_config) override {
brandtr39f97292016-11-16 22:57:50 -0800726 if (use_nack_) {
727 send_config->rtp.nack.rtp_history_ms =
728 (*receive_configs)[0].rtp.nack.rtp_history_ms =
729 VideoSendStreamTest::kNackRtpHistoryMs;
730 }
Niels Möller4db138e2018-04-19 09:04:13 +0200731 send_config->encoder_settings.encoder_factory = encoder_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200732 send_config->rtp.payload_name = payload_name_;
brandtr39f97292016-11-16 22:57:50 -0800733 if (header_extensions_enabled_) {
734 send_config->rtp.extensions.push_back(RtpExtension(
735 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
736 send_config->rtp.extensions.push_back(RtpExtension(
737 RtpExtension::kTimestampOffsetUri, test::kTOffsetExtensionId));
stefanb77c7162017-02-06 06:29:38 -0800738 } else {
739 send_config->rtp.extensions.clear();
brandtr39f97292016-11-16 22:57:50 -0800740 }
Niels Möller259a4972018-04-05 15:36:51 +0200741 encoder_config->codec_type = PayloadStringToCodecType(payload_name_);
brandtr39f97292016-11-16 22:57:50 -0800742 }
743
744 void PerformTest() override {
745 EXPECT_TRUE(Wait())
746 << "Timed out waiting for FlexFEC and/or media packets.";
747 }
748
Niels Möller4db138e2018-04-19 09:04:13 +0200749 VideoEncoderFactory* encoder_factory_;
brandtr696c9c62016-12-19 05:47:28 -0800750 std::string payload_name_;
brandtr39f97292016-11-16 22:57:50 -0800751 const bool use_nack_;
brandtr39f97292016-11-16 22:57:50 -0800752 bool sent_media_;
753 bool sent_flexfec_;
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100754 const bool header_extensions_enabled_;
755 const size_t num_video_streams_;
brandtr39f97292016-11-16 22:57:50 -0800756};
757
brandtrd654a9b2016-12-05 05:38:19 -0800758TEST_F(VideoSendStreamTest, SupportsFlexfecVp8) {
Niels Möller4db138e2018-04-19 09:04:13 +0200759 test::FunctionVideoEncoderFactory encoder_factory(
760 []() { return VP8Encoder::Create(); });
761 FlexfecObserver test(false, false, "VP8", &encoder_factory, 1);
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100762 RunBaseTest(&test);
763}
764
765TEST_F(VideoSendStreamTest, SupportsFlexfecSimulcastVp8) {
Niels Möller4db138e2018-04-19 09:04:13 +0200766 test::FunctionVideoEncoderFactory encoder_factory(
767 []() { return VP8Encoder::Create(); });
768 FlexfecObserver test(false, false, "VP8", &encoder_factory, 2);
brandtrd654a9b2016-12-05 05:38:19 -0800769 RunBaseTest(&test);
770}
771
772TEST_F(VideoSendStreamTest, SupportsFlexfecWithNackVp8) {
Niels Möller4db138e2018-04-19 09:04:13 +0200773 test::FunctionVideoEncoderFactory encoder_factory(
774 []() { return VP8Encoder::Create(); });
775 FlexfecObserver test(false, true, "VP8", &encoder_factory, 1);
brandtrd654a9b2016-12-05 05:38:19 -0800776 RunBaseTest(&test);
777}
778
brandtr39f97292016-11-16 22:57:50 -0800779TEST_F(VideoSendStreamTest, SupportsFlexfecWithRtpExtensionsVp8) {
Niels Möller4db138e2018-04-19 09:04:13 +0200780 test::FunctionVideoEncoderFactory encoder_factory(
781 []() { return VP8Encoder::Create(); });
782 FlexfecObserver test(true, false, "VP8", &encoder_factory, 1);
brandtr39f97292016-11-16 22:57:50 -0800783 RunBaseTest(&test);
784}
785
brandtr39f97292016-11-16 22:57:50 -0800786#if !defined(RTC_DISABLE_VP9)
brandtrd654a9b2016-12-05 05:38:19 -0800787TEST_F(VideoSendStreamTest, SupportsFlexfecVp9) {
Niels Möller4db138e2018-04-19 09:04:13 +0200788 test::FunctionVideoEncoderFactory encoder_factory(
789 []() { return VP9Encoder::Create(); });
790 FlexfecObserver test(false, false, "VP9", &encoder_factory, 1);
brandtr39f97292016-11-16 22:57:50 -0800791 RunBaseTest(&test);
792}
793
brandtrd654a9b2016-12-05 05:38:19 -0800794TEST_F(VideoSendStreamTest, SupportsFlexfecWithNackVp9) {
Niels Möller4db138e2018-04-19 09:04:13 +0200795 test::FunctionVideoEncoderFactory encoder_factory(
796 []() { return VP9Encoder::Create(); });
797 FlexfecObserver test(false, true, "VP9", &encoder_factory, 1);
brandtr39f97292016-11-16 22:57:50 -0800798 RunBaseTest(&test);
799}
800#endif // defined(RTC_DISABLE_VP9)
801
brandtrd654a9b2016-12-05 05:38:19 -0800802TEST_F(VideoSendStreamTest, SupportsFlexfecH264) {
Niels Möller4db138e2018-04-19 09:04:13 +0200803 test::FunctionVideoEncoderFactory encoder_factory([]() {
804 return rtc::MakeUnique<test::FakeH264Encoder>(Clock::GetRealTimeClock());
805 });
806 FlexfecObserver test(false, false, "H264", &encoder_factory, 1);
brandtr39f97292016-11-16 22:57:50 -0800807 RunBaseTest(&test);
808}
809
brandtrd654a9b2016-12-05 05:38:19 -0800810TEST_F(VideoSendStreamTest, SupportsFlexfecWithNackH264) {
Niels Möller4db138e2018-04-19 09:04:13 +0200811 test::FunctionVideoEncoderFactory encoder_factory([]() {
812 return rtc::MakeUnique<test::FakeH264Encoder>(Clock::GetRealTimeClock());
813 });
814 FlexfecObserver test(false, true, "H264", &encoder_factory, 1);
brandtr696c9c62016-12-19 05:47:28 -0800815 RunBaseTest(&test);
816}
817
brandtre78d2662017-01-16 05:57:16 -0800818TEST_F(VideoSendStreamTest, SupportsFlexfecWithMultithreadedH264) {
Niels Möller4db138e2018-04-19 09:04:13 +0200819 test::FunctionVideoEncoderFactory encoder_factory([]() {
820 return rtc::MakeUnique<test::MultithreadedFakeH264Encoder>(
821 Clock::GetRealTimeClock());
822 });
823
824 FlexfecObserver test(false, false, "H264", &encoder_factory, 1);
brandtr39f97292016-11-16 22:57:50 -0800825 RunBaseTest(&test);
826}
827
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000828void VideoSendStreamTest::TestNackRetransmission(
829 uint32_t retransmit_ssrc,
stefan@webrtc.orgcb254aa2014-06-12 15:12:25 +0000830 uint8_t retransmit_payload_type) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000831 class NackObserver : public test::SendTest {
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000832 public:
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000833 explicit NackObserver(uint32_t retransmit_ssrc,
834 uint8_t retransmit_payload_type)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000835 : SendTest(kDefaultTimeoutMs),
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000836 send_count_(0),
Sebastian Janssond3f38162018-02-28 16:14:44 +0100837 retransmit_count_(0),
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000838 retransmit_ssrc_(retransmit_ssrc),
Sebastian Janssond3f38162018-02-28 16:14:44 +0100839 retransmit_payload_type_(retransmit_payload_type) {}
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000840
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000841 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000842 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000843 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000844 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000845
Sebastian Janssond3f38162018-02-28 16:14:44 +0100846 int kRetransmitTarget = 6;
847 ++send_count_;
848 if (send_count_ == 5 || send_count_ == 25) {
849 nacked_sequence_numbers_.push_back(
850 static_cast<uint16_t>(header.sequenceNumber - 3));
851 nacked_sequence_numbers_.push_back(
852 static_cast<uint16_t>(header.sequenceNumber - 2));
853 nacked_sequence_numbers_.push_back(
854 static_cast<uint16_t>(header.sequenceNumber - 1));
855
danilchap8a1d2a32017-08-01 03:21:37 -0700856 RTCPSender rtcp_sender(false, Clock::GetRealTimeClock(), nullptr,
Jiawei Ou3587b832018-01-31 22:08:26 -0800857 nullptr, nullptr, transport_adapter_.get(),
858 RtcpIntervalConfig{});
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000859
pbosda903ea2015-10-02 02:36:56 -0700860 rtcp_sender.SetRTCPStatus(RtcpMode::kReducedSize);
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100861 rtcp_sender.SetRemoteSSRC(kVideoSendSsrcs[0]);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000862
863 RTCPSender::FeedbackState feedback_state;
864
Sebastian Janssond3f38162018-02-28 16:14:44 +0100865 EXPECT_EQ(0, rtcp_sender.SendRTCP(
866 feedback_state, kRtcpNack,
867 static_cast<int>(nacked_sequence_numbers_.size()),
868 &nacked_sequence_numbers_.front()));
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000869 }
870
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000871 uint16_t sequence_number = header.sequenceNumber;
872
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000873 if (header.ssrc == retransmit_ssrc_ &&
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100874 retransmit_ssrc_ != kVideoSendSsrcs[0]) {
875 // Not kVideoSendSsrcs[0], assume correct RTX packet. Extract sequence
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000876 // number.
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000877 const uint8_t* rtx_header = packet + header.headerLength;
878 sequence_number = (rtx_header[0] << 8) + rtx_header[1];
879 }
Sebastian Janssond3f38162018-02-28 16:14:44 +0100880 auto found = std::find(nacked_sequence_numbers_.begin(),
881 nacked_sequence_numbers_.end(), sequence_number);
882 if (found != nacked_sequence_numbers_.end()) {
883 nacked_sequence_numbers_.erase(found);
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000884
Sebastian Janssond3f38162018-02-28 16:14:44 +0100885 if (++retransmit_count_ == kRetransmitTarget) {
886 EXPECT_EQ(retransmit_ssrc_, header.ssrc);
887 EXPECT_EQ(retransmit_payload_type_, header.payloadType);
888 observation_complete_.Set();
889 }
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000890 }
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000891
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000892 return SEND_PACKET;
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000893 }
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +0000894
stefanff483612015-12-21 03:14:00 -0800895 void ModifyVideoConfigs(
896 VideoSendStream::Config* send_config,
897 std::vector<VideoReceiveStream::Config>* receive_configs,
898 VideoEncoderConfig* encoder_config) override {
stefanf116bd02015-10-27 08:29:42 -0700899 transport_adapter_.reset(
900 new internal::TransportAdapter(send_config->send_transport));
901 transport_adapter_->Enable();
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000902 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000903 send_config->rtp.rtx.payload_type = retransmit_payload_type_;
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100904 if (retransmit_ssrc_ != kVideoSendSsrcs[0])
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000905 send_config->rtp.rtx.ssrcs.push_back(retransmit_ssrc_);
906 }
907
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000908 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100909 EXPECT_TRUE(Wait()) << "Timed out while waiting for NACK retransmission.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000910 }
911
kwiberg27f982b2016-03-01 11:52:33 -0800912 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000913 int send_count_;
Sebastian Janssond3f38162018-02-28 16:14:44 +0100914 int retransmit_count_;
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000915 uint32_t retransmit_ssrc_;
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000916 uint8_t retransmit_payload_type_;
Sebastian Janssond3f38162018-02-28 16:14:44 +0100917 std::vector<uint16_t> nacked_sequence_numbers_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000918 } test(retransmit_ssrc, retransmit_payload_type);
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000919
stefane74eef12016-01-08 06:47:13 -0800920 RunBaseTest(&test);
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000921}
922
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000923TEST_F(VideoSendStreamTest, RetransmitsNack) {
924 // Normal NACKs should use the send SSRC.
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100925 TestNackRetransmission(kVideoSendSsrcs[0], kFakeVideoSendPayloadType);
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000926}
927
928TEST_F(VideoSendStreamTest, RetransmitsNackOverRtx) {
929 // NACKs over RTX should use a separate SSRC.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000930 TestNackRetransmission(kSendRtxSsrcs[0], kSendRtxPayloadType);
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000931}
932
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000933void VideoSendStreamTest::TestPacketFragmentationSize(VideoFormat format,
934 bool with_fec) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000935 // Use a fake encoder to output a frame of every size in the range [90, 290],
936 // for each size making sure that the exact number of payload bytes received
937 // is correct and that packets are fragmented to respect max packet size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000938 static const size_t kMaxPacketSize = 128;
939 static const size_t start = 90;
940 static const size_t stop = 290;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000941
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000942 // Observer that verifies that the expected number of packets and bytes
943 // arrive for each frame size, from start_size to stop_size.
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000944 class FrameFragmentationTest : public test::SendTest,
945 public EncodedFrameObserver {
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000946 public:
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000947 FrameFragmentationTest(size_t max_packet_size,
948 size_t start_size,
949 size_t stop_size,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000950 bool test_generic_packetization,
951 bool use_fec)
952 : SendTest(kLongTimeoutMs),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000953 encoder_(stop),
Niels Möller4db138e2018-04-19 09:04:13 +0200954 encoder_factory_(&encoder_),
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000955 max_packet_size_(max_packet_size),
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000956 stop_size_(stop_size),
957 test_generic_packetization_(test_generic_packetization),
958 use_fec_(use_fec),
959 packet_count_(0),
Sebastian Jansson56fa0502018-02-01 13:00:57 +0100960 packets_lost_(0),
961 last_packet_count_(0),
962 last_packets_lost_(0),
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000963 accumulated_size_(0),
964 accumulated_payload_(0),
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000965 fec_packet_received_(false),
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000966 current_size_rtp_(start_size),
Yuwei Huangd9f99c12017-10-24 15:40:52 -0700967 current_size_frame_(static_cast<int>(start_size)) {
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000968 // Fragmentation required, this test doesn't make sense without it.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000969 encoder_.SetFrameSize(start_size);
henrikg91d6ede2015-09-17 00:24:34 -0700970 RTC_DCHECK_GT(stop_size, max_packet_size);
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000971 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000972
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000973 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000974 Action OnSendRtp(const uint8_t* packet, size_t size) override {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000975 size_t length = size;
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000976 RTPHeader header;
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000977 EXPECT_TRUE(parser_->Parse(packet, length, &header));
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000978
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000979 EXPECT_LE(length, max_packet_size_);
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000980
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000981 if (use_fec_) {
982 uint8_t payload_type = packet[header.headerLength];
983 bool is_fec = header.payloadType == kRedPayloadType &&
984 payload_type == kUlpfecPayloadType;
985 if (is_fec) {
986 fec_packet_received_ = true;
987 return SEND_PACKET;
988 }
989 }
990
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000991 accumulated_size_ += length;
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000992
993 if (use_fec_)
994 TriggerLossReport(header);
995
996 if (test_generic_packetization_) {
Stefan Holmer586b19b2015-09-18 11:14:31 +0200997 size_t overhead = header.headerLength + header.paddingLength;
998 // Only remove payload header and RED header if the packet actually
999 // contains payload.
1000 if (length > overhead) {
1001 overhead += (1 /* Generic header */);
1002 if (use_fec_)
1003 overhead += 1; // RED for FEC header.
1004 }
1005 EXPECT_GE(length, overhead);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001006 accumulated_payload_ += length - overhead;
1007 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001008
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001009 // Marker bit set indicates last packet of a frame.
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001010 if (header.markerBit) {
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001011 if (use_fec_ && accumulated_payload_ == current_size_rtp_ - 1) {
1012 // With FEC enabled, frame size is incremented asynchronously, so
1013 // "old" frames one byte too small may arrive. Accept, but don't
1014 // increase expected frame size.
1015 accumulated_size_ = 0;
1016 accumulated_payload_ = 0;
1017 return SEND_PACKET;
1018 }
1019
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001020 EXPECT_GE(accumulated_size_, current_size_rtp_);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001021 if (test_generic_packetization_) {
1022 EXPECT_EQ(current_size_rtp_, accumulated_payload_);
1023 }
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001024
1025 // Last packet of frame; reset counters.
1026 accumulated_size_ = 0;
1027 accumulated_payload_ = 0;
1028 if (current_size_rtp_ == stop_size_) {
1029 // Done! (Don't increase size again, might arrive more @ stop_size).
Peter Boström5811a392015-12-10 13:02:50 +01001030 observation_complete_.Set();
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001031 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001032 // Increase next expected frame size. If testing with FEC, make sure
1033 // a FEC packet has been received for this frame size before
1034 // proceeding, to make sure that redundancy packets don't exceed
1035 // size limit.
1036 if (!use_fec_) {
1037 ++current_size_rtp_;
1038 } else if (fec_packet_received_) {
1039 fec_packet_received_ = false;
1040 ++current_size_rtp_;
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001041
1042 rtc::CritScope lock(&mutex_);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001043 ++current_size_frame_;
1044 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001045 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001046 }
1047
stefan@webrtc.org69969e22013-11-15 12:32:15 +00001048 return SEND_PACKET;
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001049 }
1050
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001051 void TriggerLossReport(const RTPHeader& header) {
1052 // Send lossy receive reports to trigger FEC enabling.
sprang4847ae62017-06-27 07:06:52 -07001053 const int kLossPercent = 5;
Sebastian Jansson56fa0502018-02-01 13:00:57 +01001054 if (++packet_count_ % (100 / kLossPercent) == 0) {
1055 packets_lost_++;
1056 int loss_delta = packets_lost_ - last_packets_lost_;
1057 int packets_delta = packet_count_ - last_packet_count_;
1058 last_packet_count_ = packet_count_;
1059 last_packets_lost_ = packets_lost_;
1060 uint8_t loss_ratio =
1061 static_cast<uint8_t>(loss_delta * 255 / packets_delta);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001062 FakeReceiveStatistics lossy_receive_stats(
sprang4847ae62017-06-27 07:06:52 -07001063 kVideoSendSsrcs[0], header.sequenceNumber,
Sebastian Jansson56fa0502018-02-01 13:00:57 +01001064 packets_lost_, // Cumulative lost.
1065 loss_ratio); // Loss percent.
Peter Boströmac547a62015-09-17 23:03:57 +02001066 RTCPSender rtcp_sender(false, Clock::GetRealTimeClock(),
terelius429c3452016-01-21 05:42:04 -08001067 &lossy_receive_stats, nullptr, nullptr,
Jiawei Ou3587b832018-01-31 22:08:26 -08001068 transport_adapter_.get(), RtcpIntervalConfig{});
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001069
pbosda903ea2015-10-02 02:36:56 -07001070 rtcp_sender.SetRTCPStatus(RtcpMode::kReducedSize);
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001071 rtcp_sender.SetRemoteSSRC(kVideoSendSsrcs[0]);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001072
1073 RTCPSender::FeedbackState feedback_state;
1074
1075 EXPECT_EQ(0, rtcp_sender.SendRTCP(feedback_state, kRtcpRr));
1076 }
1077 }
1078
nisseef8b61e2016-04-29 06:09:15 -07001079 void EncodedFrameCallback(const EncodedFrame& encoded_frame) override {
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001080 rtc::CritScope lock(&mutex_);
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001081 // Increase frame size for next encoded frame, in the context of the
1082 // encoder thread.
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001083 if (!use_fec_ && current_size_frame_ < static_cast<int32_t>(stop_size_)) {
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001084 ++current_size_frame_;
1085 }
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001086 encoder_.SetFrameSize(static_cast<size_t>(current_size_frame_));
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001087 }
1088
Stefan Holmere5904162015-03-26 11:11:06 +01001089 Call::Config GetSenderCallConfig() override {
philipel4fb651d2017-04-10 03:54:05 -07001090 Call::Config config(event_log_.get());
Stefan Holmere5904162015-03-26 11:11:06 +01001091 const int kMinBitrateBps = 30000;
1092 config.bitrate_config.min_bitrate_bps = kMinBitrateBps;
1093 return config;
1094 }
1095
stefanff483612015-12-21 03:14:00 -08001096 void ModifyVideoConfigs(
1097 VideoSendStream::Config* send_config,
1098 std::vector<VideoReceiveStream::Config>* receive_configs,
1099 VideoEncoderConfig* encoder_config) override {
stefanf116bd02015-10-27 08:29:42 -07001100 transport_adapter_.reset(
1101 new internal::TransportAdapter(send_config->send_transport));
1102 transport_adapter_->Enable();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001103 if (use_fec_) {
brandtrb5f2c3f2016-10-04 23:28:39 -07001104 send_config->rtp.ulpfec.red_payload_type = kRedPayloadType;
1105 send_config->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001106 }
1107
1108 if (!test_generic_packetization_)
Niels Möller259a4972018-04-05 15:36:51 +02001109 send_config->rtp.payload_name = "VP8";
Niels Möller4db138e2018-04-19 09:04:13 +02001110 send_config->encoder_settings.encoder_factory = &encoder_factory_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001111 send_config->rtp.max_packet_size = kMaxPacketSize;
1112 send_config->post_encode_callback = this;
1113
Erik Språng95261872015-04-10 11:58:49 +02001114 // Make sure there is at least one extension header, to make the RTP
1115 // header larger than the base length of 12 bytes.
1116 EXPECT_FALSE(send_config->rtp.extensions.empty());
sprang4847ae62017-06-27 07:06:52 -07001117
1118 // Setup screen content disables frame dropping which makes this easier.
1119 class VideoStreamFactory
1120 : public VideoEncoderConfig::VideoStreamFactoryInterface {
1121 public:
1122 explicit VideoStreamFactory(size_t num_temporal_layers)
1123 : num_temporal_layers_(num_temporal_layers) {
1124 EXPECT_GT(num_temporal_layers, 0u);
1125 }
1126
1127 private:
1128 std::vector<VideoStream> CreateEncoderStreams(
1129 int width,
1130 int height,
1131 const VideoEncoderConfig& encoder_config) override {
1132 std::vector<VideoStream> streams =
1133 test::CreateVideoStreams(width, height, encoder_config);
1134 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +01001135 stream.num_temporal_layers = num_temporal_layers_;
sprang4847ae62017-06-27 07:06:52 -07001136 }
1137 return streams;
1138 }
1139 const size_t num_temporal_layers_;
1140 };
1141
1142 encoder_config->video_stream_factory =
1143 new rtc::RefCountedObject<VideoStreamFactory>(2);
1144 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001145 }
1146
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001147 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001148 EXPECT_TRUE(Wait()) << "Timed out while observing incoming RTP packets.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001149 }
1150
kwiberg27f982b2016-03-01 11:52:33 -08001151 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001152 test::ConfigurableFrameSizeEncoder encoder_;
Niels Möller4db138e2018-04-19 09:04:13 +02001153 test::EncoderProxyFactory encoder_factory_;
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001154
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001155 const size_t max_packet_size_;
1156 const size_t stop_size_;
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001157 const bool test_generic_packetization_;
1158 const bool use_fec_;
1159
1160 uint32_t packet_count_;
Sebastian Jansson56fa0502018-02-01 13:00:57 +01001161 uint32_t packets_lost_;
1162 uint32_t last_packet_count_;
1163 uint32_t last_packets_lost_;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001164 size_t accumulated_size_;
1165 size_t accumulated_payload_;
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001166 bool fec_packet_received_;
1167
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001168 size_t current_size_rtp_;
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001169 rtc::CriticalSection mutex_;
1170 int current_size_frame_ RTC_GUARDED_BY(mutex_);
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001171 };
1172
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001173 // Don't auto increment if FEC is used; continue sending frame size until
1174 // a FEC packet has been received.
Yves Gerey665174f2018-06-19 15:03:05 +02001175 FrameFragmentationTest test(kMaxPacketSize, start, stop, format == kGeneric,
1176 with_fec);
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001177
stefane74eef12016-01-08 06:47:13 -08001178 RunBaseTest(&test);
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001179}
1180
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001181// TODO(sprang): Is there any way of speeding up these tests?
1182TEST_F(VideoSendStreamTest, FragmentsGenericAccordingToMaxPacketSize) {
1183 TestPacketFragmentationSize(kGeneric, false);
1184}
1185
1186TEST_F(VideoSendStreamTest, FragmentsGenericAccordingToMaxPacketSizeWithFec) {
1187 TestPacketFragmentationSize(kGeneric, true);
1188}
1189
1190TEST_F(VideoSendStreamTest, FragmentsVp8AccordingToMaxPacketSize) {
1191 TestPacketFragmentationSize(kVP8, false);
1192}
1193
1194TEST_F(VideoSendStreamTest, FragmentsVp8AccordingToMaxPacketSizeWithFec) {
1195 TestPacketFragmentationSize(kVP8, true);
1196}
1197
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001198// The test will go through a number of phases.
1199// 1. Start sending packets.
1200// 2. As soon as the RTP stream has been detected, signal a low REMB value to
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001201// suspend the stream.
1202// 3. Wait until |kSuspendTimeFrames| have been captured without seeing any RTP
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001203// packets.
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001204// 4. Signal a high REMB and then wait for the RTP stream to start again.
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001205// When the stream is detected again, and the stats show that the stream
1206// is no longer suspended, the test ends.
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001207TEST_F(VideoSendStreamTest, SuspendBelowMinBitrate) {
1208 static const int kSuspendTimeFrames = 60; // Suspend for 2 seconds @ 30 fps.
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001209
nissed30a1112016-04-18 05:15:22 -07001210 class RembObserver : public test::SendTest,
1211 public rtc::VideoSinkInterface<VideoFrame> {
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001212 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001213 RembObserver()
1214 : SendTest(kDefaultTimeoutMs),
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001215 clock_(Clock::GetRealTimeClock()),
Erik Språng737336d2016-07-29 12:59:36 +02001216 stream_(nullptr),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001217 test_state_(kBeforeSuspend),
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001218 rtp_count_(0),
1219 last_sequence_number_(0),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001220 suspended_frame_count_(0),
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001221 low_remb_bps_(0),
Erik Språng737336d2016-07-29 12:59:36 +02001222 high_remb_bps_(0) {}
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001223
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001224 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001225 Action OnSendRtp(const uint8_t* packet, size_t length) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001226 rtc::CritScope lock(&crit_);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001227 ++rtp_count_;
1228 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001229 EXPECT_TRUE(parser_->Parse(packet, length, &header));
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001230 last_sequence_number_ = header.sequenceNumber;
1231
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001232 if (test_state_ == kBeforeSuspend) {
1233 // The stream has started. Try to suspend it.
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001234 SendRtcpFeedback(low_remb_bps_);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001235 test_state_ = kDuringSuspend;
1236 } else if (test_state_ == kDuringSuspend) {
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001237 if (header.paddingLength == 0) {
1238 // Received non-padding packet during suspension period. Reset the
1239 // counter.
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001240 suspended_frame_count_ = 0;
1241 }
stefanf116bd02015-10-27 08:29:42 -07001242 SendRtcpFeedback(0); // REMB is only sent if value is > 0.
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001243 } else if (test_state_ == kWaitingForPacket) {
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001244 if (header.paddingLength == 0) {
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001245 // Non-padding packet observed. Test is almost complete. Will just
1246 // have to wait for the stats to change.
1247 test_state_ = kWaitingForStats;
1248 }
stefanf116bd02015-10-27 08:29:42 -07001249 SendRtcpFeedback(0); // REMB is only sent if value is > 0.
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001250 } else if (test_state_ == kWaitingForStats) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001251 VideoSendStream::Stats stats = stream_->GetStats();
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001252 if (stats.suspended == false) {
1253 // Stats flipped to false. Test is complete.
Peter Boström5811a392015-12-10 13:02:50 +01001254 observation_complete_.Set();
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001255 }
stefanf116bd02015-10-27 08:29:42 -07001256 SendRtcpFeedback(0); // REMB is only sent if value is > 0.
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001257 }
1258
stefan@webrtc.org69969e22013-11-15 12:32:15 +00001259 return SEND_PACKET;
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001260 }
1261
perkj26091b12016-09-01 01:17:40 -07001262 // This method implements the rtc::VideoSinkInterface. This is called when
1263 // a frame is provided to the VideoSendStream.
nissed30a1112016-04-18 05:15:22 -07001264 void OnFrame(const VideoFrame& video_frame) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001265 rtc::CritScope lock(&crit_);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001266 if (test_state_ == kDuringSuspend &&
1267 ++suspended_frame_count_ > kSuspendTimeFrames) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001268 VideoSendStream::Stats stats = stream_->GetStats();
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +00001269 EXPECT_TRUE(stats.suspended);
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001270 SendRtcpFeedback(high_remb_bps_);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001271 test_state_ = kWaitingForPacket;
1272 }
1273 }
1274
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001275 void set_low_remb_bps(int value) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001276 rtc::CritScope lock(&crit_);
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001277 low_remb_bps_ = value;
1278 }
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001279
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001280 void set_high_remb_bps(int value) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001281 rtc::CritScope lock(&crit_);
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001282 high_remb_bps_ = value;
1283 }
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001284
stefanff483612015-12-21 03:14:00 -08001285 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +00001286 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001287 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001288 stream_ = send_stream;
1289 }
1290
stefanff483612015-12-21 03:14:00 -08001291 void ModifyVideoConfigs(
1292 VideoSendStream::Config* send_config,
1293 std::vector<VideoReceiveStream::Config>* receive_configs,
1294 VideoEncoderConfig* encoder_config) override {
kwibergaf476c72016-11-28 15:21:39 -08001295 RTC_DCHECK_EQ(1, encoder_config->number_of_streams);
stefanf116bd02015-10-27 08:29:42 -07001296 transport_adapter_.reset(
1297 new internal::TransportAdapter(send_config->send_transport));
1298 transport_adapter_->Enable();
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +00001299 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001300 send_config->pre_encode_callback = this;
1301 send_config->suspend_below_min_bitrate = true;
perkjfa10b552016-10-02 23:45:26 -07001302 int min_bitrate_bps =
1303 test::DefaultVideoStreamFactory::kDefaultMinBitratePerStream[0];
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001304 set_low_remb_bps(min_bitrate_bps - 10000);
mflodman101f2502016-06-09 17:21:19 +02001305 int threshold_window = std::max(min_bitrate_bps / 10, 20000);
perkjfa10b552016-10-02 23:45:26 -07001306 ASSERT_GT(encoder_config->max_bitrate_bps,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001307 min_bitrate_bps + threshold_window + 5000);
1308 set_high_remb_bps(min_bitrate_bps + threshold_window + 5000);
1309 }
1310
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001311 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001312 EXPECT_TRUE(Wait()) << "Timed out during suspend-below-min-bitrate test.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001313 }
1314
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001315 enum TestState {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001316 kBeforeSuspend,
1317 kDuringSuspend,
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001318 kWaitingForPacket,
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001319 kWaitingForStats
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001320 };
1321
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001322 virtual void SendRtcpFeedback(int remb_value)
danilchapa37de392017-09-09 04:17:22 -07001323 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001324 FakeReceiveStatistics receive_stats(kVideoSendSsrcs[0],
1325 last_sequence_number_, rtp_count_, 0);
terelius429c3452016-01-21 05:42:04 -08001326 RTCPSender rtcp_sender(false, clock_, &receive_stats, nullptr, nullptr,
Jiawei Ou3587b832018-01-31 22:08:26 -08001327 transport_adapter_.get(), RtcpIntervalConfig{});
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001328
pbosda903ea2015-10-02 02:36:56 -07001329 rtcp_sender.SetRTCPStatus(RtcpMode::kReducedSize);
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001330 rtcp_sender.SetRemoteSSRC(kVideoSendSsrcs[0]);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001331 if (remb_value > 0) {
Danil Chapovalovf74d6412017-10-18 13:32:57 +02001332 rtcp_sender.SetRemb(remb_value, std::vector<uint32_t>());
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001333 }
1334 RTCPSender::FeedbackState feedback_state;
1335 EXPECT_EQ(0, rtcp_sender.SendRTCP(feedback_state, kRtcpRr));
1336 }
1337
kwiberg27f982b2016-03-01 11:52:33 -08001338 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001339 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001340 VideoSendStream* stream_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001341
Peter Boströmf2f82832015-05-01 13:00:41 +02001342 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001343 TestState test_state_ RTC_GUARDED_BY(crit_);
1344 int rtp_count_ RTC_GUARDED_BY(crit_);
1345 int last_sequence_number_ RTC_GUARDED_BY(crit_);
1346 int suspended_frame_count_ RTC_GUARDED_BY(crit_);
1347 int low_remb_bps_ RTC_GUARDED_BY(crit_);
1348 int high_remb_bps_ RTC_GUARDED_BY(crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001349 } test;
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001350
stefane74eef12016-01-08 06:47:13 -08001351 RunBaseTest(&test);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001352}
1353
perkj71ee44c2016-06-15 00:47:53 -07001354// This test that padding stops being send after a while if the Camera stops
1355// producing video frames and that padding resumes if the camera restarts.
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001356TEST_F(VideoSendStreamTest, NoPaddingWhenVideoIsMuted) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001357 class NoPaddingWhenVideoIsMuted : public test::SendTest {
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001358 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001359 NoPaddingWhenVideoIsMuted()
1360 : SendTest(kDefaultTimeoutMs),
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001361 clock_(Clock::GetRealTimeClock()),
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001362 last_packet_time_ms_(-1),
Yves Gerey665174f2018-06-19 15:03:05 +02001363 capturer_(nullptr) {}
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001364
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001365 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001366 Action OnSendRtp(const uint8_t* packet, size_t length) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001367 rtc::CritScope lock(&crit_);
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001368 last_packet_time_ms_ = clock_->TimeInMilliseconds();
perkj71ee44c2016-06-15 00:47:53 -07001369
1370 RTPHeader header;
1371 parser_->Parse(packet, length, &header);
1372 const bool only_padding =
1373 header.headerLength + header.paddingLength == length;
1374
1375 if (test_state_ == kBeforeStopCapture) {
1376 capturer_->Stop();
1377 test_state_ = kWaitingForPadding;
1378 } else if (test_state_ == kWaitingForPadding && only_padding) {
1379 test_state_ = kWaitingForNoPackets;
1380 } else if (test_state_ == kWaitingForPaddingAfterCameraRestart &&
1381 only_padding) {
1382 observation_complete_.Set();
1383 }
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001384 return SEND_PACKET;
1385 }
1386
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001387 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001388 rtc::CritScope lock(&crit_);
perkj71ee44c2016-06-15 00:47:53 -07001389 const int kNoPacketsThresholdMs = 2000;
1390 if (test_state_ == kWaitingForNoPackets &&
1391 (last_packet_time_ms_ > 0 &&
1392 clock_->TimeInMilliseconds() - last_packet_time_ms_ >
1393 kNoPacketsThresholdMs)) {
1394 capturer_->Start();
1395 test_state_ = kWaitingForPaddingAfterCameraRestart;
1396 }
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001397 return SEND_PACKET;
1398 }
1399
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001400 size_t GetNumVideoStreams() const override { return 3; }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001401
nisseef8b61e2016-04-29 06:09:15 -07001402 void OnFrameGeneratorCapturerCreated(
1403 test::FrameGeneratorCapturer* frame_generator_capturer) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001404 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001405 capturer_ = frame_generator_capturer;
1406 }
1407
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001408 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001409 EXPECT_TRUE(Wait())
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001410 << "Timed out while waiting for RTP packets to stop being sent.";
1411 }
1412
perkj71ee44c2016-06-15 00:47:53 -07001413 enum TestState {
1414 kBeforeStopCapture,
1415 kWaitingForPadding,
1416 kWaitingForNoPackets,
1417 kWaitingForPaddingAfterCameraRestart
1418 };
1419
1420 TestState test_state_ = kBeforeStopCapture;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001421 Clock* const clock_;
kwiberg27f982b2016-03-01 11:52:33 -08001422 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
Peter Boströmf2f82832015-05-01 13:00:41 +02001423 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001424 int64_t last_packet_time_ms_ RTC_GUARDED_BY(crit_);
1425 test::FrameGeneratorCapturer* capturer_ RTC_GUARDED_BY(crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001426 } test;
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001427
stefane74eef12016-01-08 06:47:13 -08001428 RunBaseTest(&test);
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001429}
1430
isheriffcc5903e2016-10-04 08:29:38 -07001431TEST_F(VideoSendStreamTest, PaddingIsPrimarilyRetransmissions) {
1432 const int kCapacityKbps = 10000; // 10 Mbps
1433 class PaddingIsPrimarilyRetransmissions : public test::EndToEndTest {
1434 public:
1435 PaddingIsPrimarilyRetransmissions()
1436 : EndToEndTest(kDefaultTimeoutMs),
1437 clock_(Clock::GetRealTimeClock()),
1438 padding_length_(0),
1439 total_length_(0),
1440 call_(nullptr) {}
1441
1442 private:
1443 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1444 call_ = sender_call;
1445 }
1446
1447 Action OnSendRtp(const uint8_t* packet, size_t length) override {
1448 rtc::CritScope lock(&crit_);
1449
1450 RTPHeader header;
1451 parser_->Parse(packet, length, &header);
1452 padding_length_ += header.paddingLength;
1453 total_length_ += length;
1454 return SEND_PACKET;
1455 }
1456
eladalon413ee9a2017-08-22 04:02:52 -07001457 test::PacketTransport* CreateSendTransport(
1458 test::SingleThreadedTaskQueueForTesting* task_queue,
1459 Call* sender_call) override {
isheriffcc5903e2016-10-04 08:29:38 -07001460 const int kNetworkDelayMs = 50;
1461 FakeNetworkPipe::Config config;
1462 config.loss_percent = 10;
1463 config.link_capacity_kbps = kCapacityKbps;
1464 config.queue_delay_ms = kNetworkDelayMs;
eladalon413ee9a2017-08-22 04:02:52 -07001465 return new test::PacketTransport(task_queue, sender_call, this,
nissee5ad5ca2017-03-29 23:57:43 -07001466 test::PacketTransport::kSender,
minyue20c84cc2017-04-10 16:57:57 -07001467 payload_type_map_, config);
isheriffcc5903e2016-10-04 08:29:38 -07001468 }
1469
1470 void ModifyVideoConfigs(
1471 VideoSendStream::Config* send_config,
1472 std::vector<VideoReceiveStream::Config>* receive_configs,
1473 VideoEncoderConfig* encoder_config) override {
isheriffcc5903e2016-10-04 08:29:38 -07001474 // Turn on RTX.
1475 send_config->rtp.rtx.payload_type = kFakeVideoSendPayloadType;
1476 send_config->rtp.rtx.ssrcs.push_back(kVideoSendSsrcs[0]);
isheriffcc5903e2016-10-04 08:29:38 -07001477 }
1478
1479 void PerformTest() override {
1480 // TODO(isheriff): Some platforms do not ramp up as expected to full
1481 // capacity due to packet scheduling delays. Fix that before getting
1482 // rid of this.
1483 SleepMs(5000);
1484 {
1485 rtc::CritScope lock(&crit_);
1486 // Expect padding to be a small percentage of total bytes sent.
1487 EXPECT_LT(padding_length_, .1 * total_length_);
1488 }
1489 }
1490
1491 rtc::CriticalSection crit_;
1492 Clock* const clock_;
danilchapa37de392017-09-09 04:17:22 -07001493 size_t padding_length_ RTC_GUARDED_BY(crit_);
1494 size_t total_length_ RTC_GUARDED_BY(crit_);
isheriffcc5903e2016-10-04 08:29:38 -07001495 Call* call_;
1496 } test;
1497
1498 RunBaseTest(&test);
1499}
1500
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001501// This test first observes "high" bitrate use at which point it sends a REMB to
1502// indicate that it should be lowered significantly. The test then observes that
1503// the bitrate observed is sinking well below the min-transmit-bitrate threshold
1504// to verify that the min-transmit bitrate respects incoming REMB.
andresp@webrtc.org44caf012014-03-26 21:00:21 +00001505//
1506// Note that the test starts at "high" bitrate and does not ramp up to "higher"
1507// bitrate since no receiver block or remb is sent in the initial phase.
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001508TEST_F(VideoSendStreamTest, MinTransmitBitrateRespectsRemb) {
1509 static const int kMinTransmitBitrateBps = 400000;
andresp@webrtc.org44caf012014-03-26 21:00:21 +00001510 static const int kHighBitrateBps = 150000;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001511 static const int kRembBitrateBps = 80000;
1512 static const int kRembRespectedBitrateBps = 100000;
stefanf116bd02015-10-27 08:29:42 -07001513 class BitrateObserver : public test::SendTest {
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001514 public:
1515 BitrateObserver()
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001516 : SendTest(kDefaultTimeoutMs),
Erik Språng737336d2016-07-29 12:59:36 +02001517 retranmission_rate_limiter_(Clock::GetRealTimeClock(), 1000),
1518 stream_(nullptr),
1519 bitrate_capped_(false) {}
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001520
1521 private:
nisseef8b61e2016-04-29 06:09:15 -07001522 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001523 if (RtpHeaderParser::IsRtcp(packet, length))
stefanf116bd02015-10-27 08:29:42 -07001524 return DROP_PACKET;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001525
1526 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001527 if (!parser_->Parse(packet, length, &header))
stefanf116bd02015-10-27 08:29:42 -07001528 return DROP_PACKET;
Peter Boström74f6e9e2016-04-04 17:56:10 +02001529 RTC_DCHECK(stream_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001530 VideoSendStream::Stats stats = stream_->GetStats();
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001531 if (!stats.substreams.empty()) {
1532 EXPECT_EQ(1u, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001533 int total_bitrate_bps =
1534 stats.substreams.begin()->second.total_bitrate_bps;
Yves Gerey665174f2018-06-19 15:03:05 +02001535 test::PrintResult("bitrate_stats_", "min_transmit_bitrate_low_remb",
1536 "bitrate_bps", static_cast<size_t>(total_bitrate_bps),
1537 "bps", false);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001538 if (total_bitrate_bps > kHighBitrateBps) {
Danil Chapovalov51e21aa2017-10-10 17:46:26 +02001539 rtp_rtcp_->SetRemb(kRembBitrateBps,
1540 std::vector<uint32_t>(1, header.ssrc));
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001541 rtp_rtcp_->Process();
1542 bitrate_capped_ = true;
1543 } else if (bitrate_capped_ &&
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001544 total_bitrate_bps < kRembRespectedBitrateBps) {
Peter Boström5811a392015-12-10 13:02:50 +01001545 observation_complete_.Set();
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001546 }
1547 }
stefanf116bd02015-10-27 08:29:42 -07001548 // Packets don't have to be delivered since the test is the receiver.
1549 return DROP_PACKET;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001550 }
1551
stefanff483612015-12-21 03:14:00 -08001552 void OnVideoStreamsCreated(
stefanf116bd02015-10-27 08:29:42 -07001553 VideoSendStream* send_stream,
1554 const std::vector<VideoReceiveStream*>& receive_streams) override {
1555 stream_ = send_stream;
1556 RtpRtcp::Configuration config;
1557 config.outgoing_transport = feedback_transport_.get();
Erik Språng737336d2016-07-29 12:59:36 +02001558 config.retransmission_rate_limiter = &retranmission_rate_limiter_;
stefanf116bd02015-10-27 08:29:42 -07001559 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
stefanf116bd02015-10-27 08:29:42 -07001560 rtp_rtcp_->SetRTCPStatus(RtcpMode::kReducedSize);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001561 }
1562
stefanff483612015-12-21 03:14:00 -08001563 void ModifyVideoConfigs(
1564 VideoSendStream::Config* send_config,
1565 std::vector<VideoReceiveStream::Config>* receive_configs,
1566 VideoEncoderConfig* encoder_config) override {
stefanf116bd02015-10-27 08:29:42 -07001567 feedback_transport_.reset(
1568 new internal::TransportAdapter(send_config->send_transport));
1569 feedback_transport_->Enable();
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +00001570 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001571 }
1572
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001573 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001574 EXPECT_TRUE(Wait())
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001575 << "Timeout while waiting for low bitrate stats after REMB.";
1576 }
1577
kwiberg27f982b2016-03-01 11:52:33 -08001578 std::unique_ptr<RtpRtcp> rtp_rtcp_;
1579 std::unique_ptr<internal::TransportAdapter> feedback_transport_;
Erik Språng737336d2016-07-29 12:59:36 +02001580 RateLimiter retranmission_rate_limiter_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001581 VideoSendStream* stream_;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001582 bool bitrate_capped_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001583 } test;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001584
stefane74eef12016-01-08 06:47:13 -08001585 RunBaseTest(&test);
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001586}
1587
Stefan Holmer280de9e2016-09-30 10:06:51 +02001588TEST_F(VideoSendStreamTest, ChangingNetworkRoute) {
1589 static const int kStartBitrateBps = 300000;
1590 static const int kNewMaxBitrateBps = 1234567;
danilchap42ca68a2016-10-31 03:34:40 -07001591 static const uint8_t kExtensionId = test::kTransportSequenceNumberExtensionId;
Stefan Holmerbe402962016-07-08 16:16:41 +02001592 class ChangingNetworkRouteTest : public test::EndToEndTest {
1593 public:
eladalon413ee9a2017-08-22 04:02:52 -07001594 explicit ChangingNetworkRouteTest(
1595 test::SingleThreadedTaskQueueForTesting* task_queue)
1596 : EndToEndTest(test::CallTest::kDefaultTimeoutMs),
1597 task_queue_(task_queue),
1598 call_(nullptr) {
Stefan Holmer280de9e2016-09-30 10:06:51 +02001599 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
1600 kRtpExtensionTransportSequenceNumber, kExtensionId));
1601 }
Stefan Holmerbe402962016-07-08 16:16:41 +02001602
1603 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1604 call_ = sender_call;
1605 }
1606
Stefan Holmer280de9e2016-09-30 10:06:51 +02001607 void ModifyVideoConfigs(
1608 VideoSendStream::Config* send_config,
1609 std::vector<VideoReceiveStream::Config>* receive_configs,
1610 VideoEncoderConfig* encoder_config) override {
1611 send_config->rtp.extensions.clear();
1612 send_config->rtp.extensions.push_back(RtpExtension(
1613 RtpExtension::kTransportSequenceNumberUri, kExtensionId));
1614 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions;
1615 (*receive_configs)[0].rtp.transport_cc = true;
1616 }
1617
1618 void ModifyAudioConfigs(
1619 AudioSendStream::Config* send_config,
1620 std::vector<AudioReceiveStream::Config>* receive_configs) override {
1621 send_config->rtp.extensions.clear();
1622 send_config->rtp.extensions.push_back(RtpExtension(
1623 RtpExtension::kTransportSequenceNumberUri, kExtensionId));
1624 (*receive_configs)[0].rtp.extensions.clear();
1625 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions;
1626 (*receive_configs)[0].rtp.transport_cc = true;
1627 }
1628
Stefan Holmerbe402962016-07-08 16:16:41 +02001629 Action OnSendRtp(const uint8_t* packet, size_t length) override {
1630 if (call_->GetStats().send_bandwidth_bps > kStartBitrateBps) {
1631 observation_complete_.Set();
1632 }
1633
1634 return SEND_PACKET;
1635 }
1636
1637 void PerformTest() override {
1638 rtc::NetworkRoute new_route(true, 10, 20, -1);
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01001639 BitrateConstraints bitrate_config;
eladalon413ee9a2017-08-22 04:02:52 -07001640
1641 task_queue_->SendTask([this, &new_route, &bitrate_config]() {
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001642 call_->GetTransportControllerSend()->OnNetworkRouteChanged("transport",
1643 new_route);
eladalon413ee9a2017-08-22 04:02:52 -07001644 bitrate_config.start_bitrate_bps = kStartBitrateBps;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001645 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
1646 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07001647 });
1648
Stefan Holmerbe402962016-07-08 16:16:41 +02001649 EXPECT_TRUE(Wait())
1650 << "Timed out while waiting for start bitrate to be exceeded.";
1651
eladalon413ee9a2017-08-22 04:02:52 -07001652 task_queue_->SendTask([this, &new_route, &bitrate_config]() {
1653 bitrate_config.start_bitrate_bps = -1;
1654 bitrate_config.max_bitrate_bps = kNewMaxBitrateBps;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001655 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
1656 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07001657 // TODO(holmer): We should set the last sent packet id here and verify
1658 // that we correctly ignore any packet loss reported prior to that id.
1659 ++new_route.local_network_id;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001660 call_->GetTransportControllerSend()->OnNetworkRouteChanged("transport",
1661 new_route);
eladalon413ee9a2017-08-22 04:02:52 -07001662 EXPECT_GE(call_->GetStats().send_bandwidth_bps, kStartBitrateBps);
1663 });
Stefan Holmerbe402962016-07-08 16:16:41 +02001664 }
1665
1666 private:
eladalon413ee9a2017-08-22 04:02:52 -07001667 test::SingleThreadedTaskQueueForTesting* const task_queue_;
Stefan Holmerbe402962016-07-08 16:16:41 +02001668 Call* call_;
eladalon413ee9a2017-08-22 04:02:52 -07001669 } test(&task_queue_);
Stefan Holmerbe402962016-07-08 16:16:41 +02001670
1671 RunBaseTest(&test);
1672}
1673
michaelt79e05882016-11-08 02:50:09 -08001674TEST_F(VideoSendStreamTest, ChangingTransportOverhead) {
1675 class ChangingTransportOverheadTest : public test::EndToEndTest {
1676 public:
eladalon413ee9a2017-08-22 04:02:52 -07001677 explicit ChangingTransportOverheadTest(
1678 test::SingleThreadedTaskQueueForTesting* task_queue)
michaelt79e05882016-11-08 02:50:09 -08001679 : EndToEndTest(test::CallTest::kDefaultTimeoutMs),
eladalon413ee9a2017-08-22 04:02:52 -07001680 task_queue_(task_queue),
michaelt79e05882016-11-08 02:50:09 -08001681 call_(nullptr),
Erik Språng08127a92016-11-16 16:41:30 +01001682 packets_sent_(0),
1683 transport_overhead_(0) {}
michaelt79e05882016-11-08 02:50:09 -08001684
1685 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1686 call_ = sender_call;
1687 }
1688
1689 Action OnSendRtp(const uint8_t* packet, size_t length) override {
michaelta3328772016-11-29 09:25:03 -08001690 EXPECT_LE(length, kMaxRtpPacketSize);
sprang21253fc2017-02-27 03:35:47 -08001691 rtc::CritScope cs(&lock_);
michaelt79e05882016-11-08 02:50:09 -08001692 if (++packets_sent_ < 100)
1693 return SEND_PACKET;
1694 observation_complete_.Set();
1695 return SEND_PACKET;
1696 }
1697
michaelta3328772016-11-29 09:25:03 -08001698 void ModifyVideoConfigs(
1699 VideoSendStream::Config* send_config,
1700 std::vector<VideoReceiveStream::Config>* receive_configs,
1701 VideoEncoderConfig* encoder_config) override {
1702 send_config->rtp.max_packet_size = kMaxRtpPacketSize;
1703 }
1704
michaelt79e05882016-11-08 02:50:09 -08001705 void PerformTest() override {
eladalon413ee9a2017-08-22 04:02:52 -07001706 task_queue_->SendTask([this]() {
1707 transport_overhead_ = 100;
1708 call_->OnTransportOverheadChanged(webrtc::MediaType::VIDEO,
1709 transport_overhead_);
1710 });
1711
michaelt79e05882016-11-08 02:50:09 -08001712 EXPECT_TRUE(Wait());
eladalon413ee9a2017-08-22 04:02:52 -07001713
sprang21253fc2017-02-27 03:35:47 -08001714 {
1715 rtc::CritScope cs(&lock_);
1716 packets_sent_ = 0;
1717 }
eladalon413ee9a2017-08-22 04:02:52 -07001718
1719 task_queue_->SendTask([this]() {
1720 transport_overhead_ = 500;
1721 call_->OnTransportOverheadChanged(webrtc::MediaType::VIDEO,
1722 transport_overhead_);
1723 });
1724
michaelt79e05882016-11-08 02:50:09 -08001725 EXPECT_TRUE(Wait());
1726 }
1727
1728 private:
eladalon413ee9a2017-08-22 04:02:52 -07001729 test::SingleThreadedTaskQueueForTesting* const task_queue_;
michaelt79e05882016-11-08 02:50:09 -08001730 Call* call_;
sprang21253fc2017-02-27 03:35:47 -08001731 rtc::CriticalSection lock_;
danilchapa37de392017-09-09 04:17:22 -07001732 int packets_sent_ RTC_GUARDED_BY(lock_);
michaelt79e05882016-11-08 02:50:09 -08001733 int transport_overhead_;
michaelta3328772016-11-29 09:25:03 -08001734 const size_t kMaxRtpPacketSize = 1000;
eladalon413ee9a2017-08-22 04:02:52 -07001735 } test(&task_queue_);
michaelt79e05882016-11-08 02:50:09 -08001736
1737 RunBaseTest(&test);
1738}
1739
sprangf24a0642017-02-28 13:23:26 -08001740// Test class takes takes as argument a switch selecting if type switch should
1741// occur and a function pointer to reset the send stream. This is necessary
1742// since you cannot change the content type of a VideoSendStream, you need to
1743// recreate it. Stopping and recreating the stream can only be done on the main
1744// thread and in the context of VideoSendStreamTest (not BaseTest).
1745template <typename T>
sprang9c0b5512016-07-06 00:54:28 -07001746class MaxPaddingSetTest : public test::SendTest {
1747 public:
1748 static const uint32_t kMinTransmitBitrateBps = 400000;
1749 static const uint32_t kActualEncodeBitrateBps = 40000;
1750 static const uint32_t kMinPacketsToSend = 50;
1751
sprangf24a0642017-02-28 13:23:26 -08001752 explicit MaxPaddingSetTest(bool test_switch_content_type, T* stream_reset_fun)
sprang9c0b5512016-07-06 00:54:28 -07001753 : SendTest(test::CallTest::kDefaultTimeoutMs),
sprangf24a0642017-02-28 13:23:26 -08001754 content_switch_event_(false, false),
sprang9c0b5512016-07-06 00:54:28 -07001755 call_(nullptr),
1756 send_stream_(nullptr),
sprangf24a0642017-02-28 13:23:26 -08001757 send_stream_config_(nullptr),
sprang9c0b5512016-07-06 00:54:28 -07001758 packets_sent_(0),
sprangf24a0642017-02-28 13:23:26 -08001759 running_without_padding_(test_switch_content_type),
tommi39e12892017-03-13 05:15:14 -07001760 stream_resetter_(stream_reset_fun) {
1761 RTC_DCHECK(stream_resetter_);
1762 }
sprang9c0b5512016-07-06 00:54:28 -07001763
1764 void OnVideoStreamsCreated(
1765 VideoSendStream* send_stream,
1766 const std::vector<VideoReceiveStream*>& receive_streams) override {
sprangf24a0642017-02-28 13:23:26 -08001767 rtc::CritScope lock(&crit_);
sprang9c0b5512016-07-06 00:54:28 -07001768 send_stream_ = send_stream;
1769 }
1770
1771 void ModifyVideoConfigs(
1772 VideoSendStream::Config* send_config,
1773 std::vector<VideoReceiveStream::Config>* receive_configs,
1774 VideoEncoderConfig* encoder_config) override {
kwibergaf476c72016-11-28 15:21:39 -08001775 RTC_DCHECK_EQ(1, encoder_config->number_of_streams);
sprangf24a0642017-02-28 13:23:26 -08001776 if (RunningWithoutPadding()) {
sprang9c0b5512016-07-06 00:54:28 -07001777 encoder_config->min_transmit_bitrate_bps = 0;
1778 encoder_config->content_type =
1779 VideoEncoderConfig::ContentType::kRealtimeVideo;
1780 } else {
1781 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
1782 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
1783 }
sprangf24a0642017-02-28 13:23:26 -08001784 send_stream_config_ = send_config->Copy();
perkj26091b12016-09-01 01:17:40 -07001785 encoder_config_ = encoder_config->Copy();
sprang9c0b5512016-07-06 00:54:28 -07001786 }
1787
1788 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1789 call_ = sender_call;
1790 }
1791
1792 Action OnSendRtp(const uint8_t* packet, size_t length) override {
1793 rtc::CritScope lock(&crit_);
sprang9c0b5512016-07-06 00:54:28 -07001794 if (running_without_padding_)
1795 EXPECT_EQ(0, call_->GetStats().max_padding_bitrate_bps);
1796
1797 // Wait until at least kMinPacketsToSend frames have been encoded, so that
1798 // we have reliable data.
1799 if (++packets_sent_ < kMinPacketsToSend)
1800 return SEND_PACKET;
1801
1802 if (running_without_padding_) {
1803 // We've sent kMinPacketsToSend packets with default configuration, switch
1804 // to enabling screen content and setting min transmit bitrate.
sprangf24a0642017-02-28 13:23:26 -08001805 // Note that we need to recreate the stream if changing content type.
sprang9c0b5512016-07-06 00:54:28 -07001806 packets_sent_ = 0;
1807 encoder_config_.min_transmit_bitrate_bps = kMinTransmitBitrateBps;
1808 encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
sprang9c0b5512016-07-06 00:54:28 -07001809 running_without_padding_ = false;
sprangf24a0642017-02-28 13:23:26 -08001810 content_switch_event_.Set();
sprang9c0b5512016-07-06 00:54:28 -07001811 return SEND_PACKET;
1812 }
1813
1814 // Make sure the pacer has been configured with a min transmit bitrate.
1815 if (call_->GetStats().max_padding_bitrate_bps > 0)
1816 observation_complete_.Set();
1817
1818 return SEND_PACKET;
1819 }
1820
1821 void PerformTest() override {
sprangf24a0642017-02-28 13:23:26 -08001822 if (RunningWithoutPadding()) {
1823 ASSERT_TRUE(
1824 content_switch_event_.Wait(test::CallTest::kDefaultTimeoutMs));
sprangf24a0642017-02-28 13:23:26 -08001825 (*stream_resetter_)(send_stream_config_, encoder_config_);
1826 }
1827
sprang9c0b5512016-07-06 00:54:28 -07001828 ASSERT_TRUE(Wait()) << "Timed out waiting for a valid padding bitrate.";
1829 }
1830
1831 private:
sprangf24a0642017-02-28 13:23:26 -08001832 bool RunningWithoutPadding() const {
1833 rtc::CritScope lock(&crit_);
1834 return running_without_padding_;
1835 }
1836
sprang9c0b5512016-07-06 00:54:28 -07001837 rtc::CriticalSection crit_;
sprangf24a0642017-02-28 13:23:26 -08001838 rtc::Event content_switch_event_;
sprang9c0b5512016-07-06 00:54:28 -07001839 Call* call_;
danilchapa37de392017-09-09 04:17:22 -07001840 VideoSendStream* send_stream_ RTC_GUARDED_BY(crit_);
sprangf24a0642017-02-28 13:23:26 -08001841 VideoSendStream::Config send_stream_config_;
sprang9c0b5512016-07-06 00:54:28 -07001842 VideoEncoderConfig encoder_config_;
danilchapa37de392017-09-09 04:17:22 -07001843 uint32_t packets_sent_ RTC_GUARDED_BY(crit_);
sprang9c0b5512016-07-06 00:54:28 -07001844 bool running_without_padding_;
sprangf24a0642017-02-28 13:23:26 -08001845 T* const stream_resetter_;
sprang9c0b5512016-07-06 00:54:28 -07001846};
1847
1848TEST_F(VideoSendStreamTest, RespectsMinTransmitBitrate) {
sprangf24a0642017-02-28 13:23:26 -08001849 auto reset_fun = [](const VideoSendStream::Config& send_stream_config,
1850 const VideoEncoderConfig& encoder_config) {};
1851 MaxPaddingSetTest<decltype(reset_fun)> test(false, &reset_fun);
sprang9c0b5512016-07-06 00:54:28 -07001852 RunBaseTest(&test);
1853}
1854
1855TEST_F(VideoSendStreamTest, RespectsMinTransmitBitrateAfterContentSwitch) {
sprangf24a0642017-02-28 13:23:26 -08001856 // Function for removing and recreating the send stream with a new config.
1857 auto reset_fun = [this](const VideoSendStream::Config& send_stream_config,
1858 const VideoEncoderConfig& encoder_config) {
eladalon413ee9a2017-08-22 04:02:52 -07001859 task_queue_.SendTask([this, &send_stream_config, &encoder_config]() {
1860 Stop();
1861 sender_call_->DestroyVideoSendStream(video_send_stream_);
1862 video_send_config_ = send_stream_config.Copy();
1863 video_encoder_config_ = encoder_config.Copy();
1864 video_send_stream_ = sender_call_->CreateVideoSendStream(
1865 video_send_config_.Copy(), video_encoder_config_.Copy());
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07001866 video_send_stream_->SetSource(frame_generator_capturer_.get(),
1867 DegradationPreference::MAINTAIN_RESOLUTION);
eladalon413ee9a2017-08-22 04:02:52 -07001868 Start();
1869 });
sprangf24a0642017-02-28 13:23:26 -08001870 };
1871 MaxPaddingSetTest<decltype(reset_fun)> test(true, &reset_fun);
sprang9c0b5512016-07-06 00:54:28 -07001872 RunBaseTest(&test);
1873}
1874
perkjfa10b552016-10-02 23:45:26 -07001875// This test verifies that new frame sizes reconfigures encoders even though not
1876// (yet) sending. The purpose of this is to permit encoding as quickly as
1877// possible once we start sending. Likely the frames being input are from the
1878// same source that will be sent later, which just means that we're ready
1879// earlier.
1880TEST_F(VideoSendStreamTest,
1881 EncoderReconfigureOnResolutionChangeWhenNotSending) {
1882 class EncoderObserver : public test::FakeEncoder {
1883 public:
1884 EncoderObserver()
1885 : FakeEncoder(Clock::GetRealTimeClock()),
1886 init_encode_called_(false, false),
1887 number_of_initializations_(0),
1888 last_initialized_frame_width_(0),
1889 last_initialized_frame_height_(0) {}
1890
1891 void WaitForResolution(int width, int height) {
1892 {
1893 rtc::CritScope lock(&crit_);
1894 if (last_initialized_frame_width_ == width &&
1895 last_initialized_frame_height_ == height) {
1896 return;
1897 }
1898 }
Erik Språng08127a92016-11-16 16:41:30 +01001899 EXPECT_TRUE(
1900 init_encode_called_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
perkjfa10b552016-10-02 23:45:26 -07001901 {
1902 rtc::CritScope lock(&crit_);
1903 EXPECT_EQ(width, last_initialized_frame_width_);
1904 EXPECT_EQ(height, last_initialized_frame_height_);
1905 }
1906 }
1907
1908 private:
1909 int32_t InitEncode(const VideoCodec* config,
1910 int32_t number_of_cores,
1911 size_t max_payload_size) override {
1912 rtc::CritScope lock(&crit_);
1913 last_initialized_frame_width_ = config->width;
1914 last_initialized_frame_height_ = config->height;
1915 ++number_of_initializations_;
Erik Språng08127a92016-11-16 16:41:30 +01001916 init_encode_called_.Set();
perkjfa10b552016-10-02 23:45:26 -07001917 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
1918 }
1919
1920 int32_t Encode(const VideoFrame& input_image,
1921 const CodecSpecificInfo* codec_specific_info,
1922 const std::vector<FrameType>* frame_types) override {
1923 ADD_FAILURE()
1924 << "Unexpected Encode call since the send stream is not started";
1925 return 0;
1926 }
1927
1928 rtc::CriticalSection crit_;
1929 rtc::Event init_encode_called_;
danilchapa37de392017-09-09 04:17:22 -07001930 size_t number_of_initializations_ RTC_GUARDED_BY(&crit_);
1931 int last_initialized_frame_width_ RTC_GUARDED_BY(&crit_);
1932 int last_initialized_frame_height_ RTC_GUARDED_BY(&crit_);
perkjfa10b552016-10-02 23:45:26 -07001933 };
1934
perkjfa10b552016-10-02 23:45:26 -07001935 test::NullTransport transport;
perkjfa10b552016-10-02 23:45:26 -07001936 EncoderObserver encoder;
Niels Möller4db138e2018-04-19 09:04:13 +02001937 test::EncoderProxyFactory encoder_factory(&encoder);
eladalon413ee9a2017-08-22 04:02:52 -07001938
Niels Möller4db138e2018-04-19 09:04:13 +02001939 task_queue_.SendTask([this, &transport, &encoder_factory]() {
eladalon413ee9a2017-08-22 04:02:52 -07001940 CreateSenderCall(Call::Config(event_log_.get()));
1941 CreateSendConfig(1, 0, 0, &transport);
Niels Möller4db138e2018-04-19 09:04:13 +02001942 video_send_config_.encoder_settings.encoder_factory = &encoder_factory;
eladalon413ee9a2017-08-22 04:02:52 -07001943 CreateVideoStreams();
1944 CreateFrameGeneratorCapturer(kDefaultFramerate, kDefaultWidth,
1945 kDefaultHeight);
1946 frame_generator_capturer_->Start();
1947 });
perkjfa10b552016-10-02 23:45:26 -07001948
1949 encoder.WaitForResolution(kDefaultWidth, kDefaultHeight);
eladalon413ee9a2017-08-22 04:02:52 -07001950
1951 task_queue_.SendTask([this]() {
1952 frame_generator_capturer_->ChangeResolution(kDefaultWidth * 2,
1953 kDefaultHeight * 2);
1954 });
1955
perkjfa10b552016-10-02 23:45:26 -07001956 encoder.WaitForResolution(kDefaultWidth * 2, kDefaultHeight * 2);
eladalon413ee9a2017-08-22 04:02:52 -07001957
1958 task_queue_.SendTask([this]() {
1959 DestroyStreams();
1960 DestroyCalls();
1961 });
perkjfa10b552016-10-02 23:45:26 -07001962}
1963
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001964TEST_F(VideoSendStreamTest, CanReconfigureToUseStartBitrateAbovePreviousMax) {
1965 class StartBitrateObserver : public test::FakeEncoder {
1966 public:
1967 StartBitrateObserver()
pbos14fe7082016-04-20 06:35:56 -07001968 : FakeEncoder(Clock::GetRealTimeClock()),
1969 start_bitrate_changed_(false, false),
1970 start_bitrate_kbps_(0) {}
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001971 int32_t InitEncode(const VideoCodec* config,
1972 int32_t number_of_cores,
1973 size_t max_payload_size) override {
1974 rtc::CritScope lock(&crit_);
1975 start_bitrate_kbps_ = config->startBitrate;
pbos14fe7082016-04-20 06:35:56 -07001976 start_bitrate_changed_.Set();
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001977 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
1978 }
1979
1980 int32_t SetRates(uint32_t new_target_bitrate, uint32_t framerate) override {
1981 rtc::CritScope lock(&crit_);
1982 start_bitrate_kbps_ = new_target_bitrate;
pbos14fe7082016-04-20 06:35:56 -07001983 start_bitrate_changed_.Set();
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001984 return FakeEncoder::SetRates(new_target_bitrate, framerate);
1985 }
1986
1987 int GetStartBitrateKbps() const {
1988 rtc::CritScope lock(&crit_);
1989 return start_bitrate_kbps_;
1990 }
1991
pbos14fe7082016-04-20 06:35:56 -07001992 bool WaitForStartBitrate() {
1993 return start_bitrate_changed_.Wait(
1994 VideoSendStreamTest::kDefaultTimeoutMs);
1995 }
1996
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001997 private:
pbos5ad935c2016-01-25 03:52:44 -08001998 rtc::CriticalSection crit_;
pbos14fe7082016-04-20 06:35:56 -07001999 rtc::Event start_bitrate_changed_;
danilchapa37de392017-09-09 04:17:22 -07002000 int start_bitrate_kbps_ RTC_GUARDED_BY(crit_);
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002001 };
2002
philipel4fb651d2017-04-10 03:54:05 -07002003 CreateSenderCall(Call::Config(event_log_.get()));
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002004
solenberg4fbae2b2015-08-28 04:07:10 -07002005 test::NullTransport transport;
brandtr841de6a2016-11-15 07:10:52 -08002006 CreateSendConfig(1, 0, 0, &transport);
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002007
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01002008 BitrateConstraints bitrate_config;
perkjfa10b552016-10-02 23:45:26 -07002009 bitrate_config.start_bitrate_bps = 2 * video_encoder_config_.max_bitrate_bps;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01002010 sender_call_->GetTransportControllerSend()->SetSdpBitrateParameters(
2011 bitrate_config);
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002012
2013 StartBitrateObserver encoder;
Niels Möller4db138e2018-04-19 09:04:13 +02002014 test::EncoderProxyFactory encoder_factory(&encoder);
perkjfa10b552016-10-02 23:45:26 -07002015 // Since this test does not use a capturer, set |internal_source| = true.
2016 // Encoder configuration is otherwise updated on the next video frame.
Niels Möller4db138e2018-04-19 09:04:13 +02002017 encoder_factory.SetHasInternalSource(true);
2018 video_send_config_.encoder_settings.encoder_factory = &encoder_factory;
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002019
Stefan Holmer9fea80f2016-01-07 17:43:18 +01002020 CreateVideoStreams();
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002021
pbos14fe7082016-04-20 06:35:56 -07002022 EXPECT_TRUE(encoder.WaitForStartBitrate());
perkjfa10b552016-10-02 23:45:26 -07002023 EXPECT_EQ(video_encoder_config_.max_bitrate_bps / 1000,
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002024 encoder.GetStartBitrateKbps());
2025
perkjfa10b552016-10-02 23:45:26 -07002026 video_encoder_config_.max_bitrate_bps = 2 * bitrate_config.start_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -07002027 video_send_stream_->ReconfigureVideoEncoder(video_encoder_config_.Copy());
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002028
2029 // New bitrate should be reconfigured above the previous max. As there's no
2030 // network connection this shouldn't be flaky, as no bitrate should've been
2031 // reported in between.
pbos14fe7082016-04-20 06:35:56 -07002032 EXPECT_TRUE(encoder.WaitForStartBitrate());
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002033 EXPECT_EQ(bitrate_config.start_bitrate_bps / 1000,
2034 encoder.GetStartBitrateKbps());
2035
2036 DestroyStreams();
2037}
2038
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002039class StartStopBitrateObserver : public test::FakeEncoder {
2040 public:
2041 StartStopBitrateObserver()
2042 : FakeEncoder(Clock::GetRealTimeClock()),
2043 encoder_init_(false, false),
2044 bitrate_changed_(false, false) {}
2045 int32_t InitEncode(const VideoCodec* config,
2046 int32_t number_of_cores,
2047 size_t max_payload_size) override {
2048 rtc::CritScope lock(&crit_);
2049 encoder_init_.Set();
2050 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
2051 }
2052
Erik Språng566124a2018-04-23 12:32:22 +02002053 int32_t SetRateAllocation(const VideoBitrateAllocation& bitrate,
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002054 uint32_t framerate) override {
2055 rtc::CritScope lock(&crit_);
2056 bitrate_kbps_ = bitrate.get_sum_kbps();
2057 bitrate_changed_.Set();
2058 return FakeEncoder::SetRateAllocation(bitrate, framerate);
2059 }
2060
2061 bool WaitForEncoderInit() {
2062 return encoder_init_.Wait(VideoSendStreamTest::kDefaultTimeoutMs);
2063 }
2064
2065 bool WaitBitrateChanged(bool non_zero) {
2066 do {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +02002067 absl::optional<int> bitrate_kbps;
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002068 {
2069 rtc::CritScope lock(&crit_);
2070 bitrate_kbps = bitrate_kbps_;
2071 }
2072 if (!bitrate_kbps)
2073 continue;
2074
2075 if ((non_zero && *bitrate_kbps > 0) ||
2076 (!non_zero && *bitrate_kbps == 0)) {
2077 return true;
2078 }
2079 } while (bitrate_changed_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
2080 return false;
2081 }
2082
2083 private:
2084 rtc::CriticalSection crit_;
2085 rtc::Event encoder_init_;
2086 rtc::Event bitrate_changed_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +02002087 absl::optional<int> bitrate_kbps_ RTC_GUARDED_BY(crit_);
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002088};
2089
perkj57c21f92016-06-17 07:27:16 -07002090// This test that if the encoder use an internal source, VideoEncoder::SetRates
2091// will be called with zero bitrate during initialization and that
2092// VideoSendStream::Stop also triggers VideoEncoder::SetRates Start to be called
2093// with zero bitrate.
2094TEST_F(VideoSendStreamTest, VideoSendStreamStopSetEncoderRateToZero) {
perkj57c21f92016-06-17 07:27:16 -07002095 test::NullTransport transport;
perkj57c21f92016-06-17 07:27:16 -07002096 StartStopBitrateObserver encoder;
Niels Möller4db138e2018-04-19 09:04:13 +02002097 test::EncoderProxyFactory encoder_factory(&encoder);
2098 encoder_factory.SetHasInternalSource(true);
2099 test::FrameForwarder forwarder;
perkj57c21f92016-06-17 07:27:16 -07002100
Niels Möller4db138e2018-04-19 09:04:13 +02002101 task_queue_.SendTask([this, &transport, &encoder_factory, &forwarder]() {
eladalon413ee9a2017-08-22 04:02:52 -07002102 CreateSenderCall(Call::Config(event_log_.get()));
2103 CreateSendConfig(1, 0, 0, &transport);
2104
2105 sender_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
Niels Möller4db138e2018-04-19 09:04:13 +02002106 video_send_config_.encoder_settings.encoder_factory = &encoder_factory;
eladalon413ee9a2017-08-22 04:02:52 -07002107
2108 CreateVideoStreams();
Niels Möller4db138e2018-04-19 09:04:13 +02002109 // Inject a frame, to force encoder creation.
2110 video_send_stream_->Start();
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002111 video_send_stream_->SetSource(&forwarder, DegradationPreference::DISABLED);
Niels Möller4db138e2018-04-19 09:04:13 +02002112 forwarder.IncomingCapturedFrame(CreateVideoFrame(640, 480, 4));
eladalon413ee9a2017-08-22 04:02:52 -07002113 });
perkj57c21f92016-06-17 07:27:16 -07002114
2115 EXPECT_TRUE(encoder.WaitForEncoderInit());
Erik Språng08127a92016-11-16 16:41:30 +01002116
Yves Gerey665174f2018-06-19 15:03:05 +02002117 task_queue_.SendTask([this]() { video_send_stream_->Start(); });
Erik Språng08127a92016-11-16 16:41:30 +01002118 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
2119
Yves Gerey665174f2018-06-19 15:03:05 +02002120 task_queue_.SendTask([this]() { video_send_stream_->Stop(); });
Erik Språng08127a92016-11-16 16:41:30 +01002121 EXPECT_TRUE(encoder.WaitBitrateChanged(false));
2122
Yves Gerey665174f2018-06-19 15:03:05 +02002123 task_queue_.SendTask([this]() { video_send_stream_->Start(); });
Erik Språng08127a92016-11-16 16:41:30 +01002124 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
perkj57c21f92016-06-17 07:27:16 -07002125
eladalon413ee9a2017-08-22 04:02:52 -07002126 task_queue_.SendTask([this]() {
2127 DestroyStreams();
2128 DestroyCalls();
2129 });
perkj57c21f92016-06-17 07:27:16 -07002130}
2131
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002132// Tests that when the encoder uses an internal source, the VideoEncoder will
2133// be updated with a new bitrate when turning the VideoSendStream on/off with
2134// VideoSendStream::UpdateActiveSimulcastLayers, and when the VideoStreamEncoder
2135// is reconfigured with new active layers.
2136TEST_F(VideoSendStreamTest, VideoSendStreamUpdateActiveSimulcastLayers) {
2137 test::NullTransport transport;
2138 StartStopBitrateObserver encoder;
Niels Möller4db138e2018-04-19 09:04:13 +02002139 test::EncoderProxyFactory encoder_factory(&encoder);
2140 encoder_factory.SetHasInternalSource(true);
2141 test::FrameForwarder forwarder;
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002142
Niels Möller4db138e2018-04-19 09:04:13 +02002143 task_queue_.SendTask([this, &transport, &encoder_factory, &forwarder]() {
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002144 CreateSenderCall(Call::Config(event_log_.get()));
2145 // Create two simulcast streams.
2146 CreateSendConfig(2, 0, 0, &transport);
2147
2148 sender_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
Niels Möller4db138e2018-04-19 09:04:13 +02002149 video_send_config_.encoder_settings.encoder_factory = &encoder_factory;
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002150
Niels Möller259a4972018-04-05 15:36:51 +02002151 video_send_config_.rtp.payload_name = "VP8";
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002152
2153 CreateVideoStreams();
Niels Möller4db138e2018-04-19 09:04:13 +02002154
2155 // Inject a frame, to force encoder creation.
2156 video_send_stream_->Start();
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002157 video_send_stream_->SetSource(&forwarder, DegradationPreference::DISABLED);
Niels Möller4db138e2018-04-19 09:04:13 +02002158 forwarder.IncomingCapturedFrame(CreateVideoFrame(640, 480, 4));
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002159 });
2160
2161 EXPECT_TRUE(encoder.WaitForEncoderInit());
2162
2163 // When we turn on the simulcast layers it will update the BitrateAllocator,
2164 // which in turn updates the VideoEncoder's bitrate.
2165 task_queue_.SendTask([this]() {
2166 video_send_stream_->UpdateActiveSimulcastLayers({true, true});
2167 });
2168 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
2169
2170 video_encoder_config_.simulcast_layers[0].active = true;
2171 video_encoder_config_.simulcast_layers[1].active = false;
2172 task_queue_.SendTask([this]() {
2173 video_send_stream_->ReconfigureVideoEncoder(video_encoder_config_.Copy());
2174 });
2175 // TODO(bugs.webrtc.org/8807): Currently we require a hard reconfiguration to
2176 // update the VideoBitrateAllocator and BitrateAllocator of which layers are
2177 // active. Once the change is made for a "soft" reconfiguration we can remove
2178 // the expecation for an encoder init. We can also test that bitrate changes
2179 // when just updating individual active layers, which should change the
2180 // bitrate set to the video encoder.
2181 EXPECT_TRUE(encoder.WaitForEncoderInit());
2182 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
2183
2184 // Turning off both simulcast layers should trigger a bitrate change of 0.
2185 video_encoder_config_.simulcast_layers[0].active = false;
2186 video_encoder_config_.simulcast_layers[1].active = false;
2187 task_queue_.SendTask([this]() {
2188 video_send_stream_->UpdateActiveSimulcastLayers({false, false});
2189 });
2190 EXPECT_TRUE(encoder.WaitBitrateChanged(false));
2191
2192 task_queue_.SendTask([this]() {
2193 DestroyStreams();
2194 DestroyCalls();
2195 });
2196}
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002197TEST_F(VideoSendStreamTest, CapturesTextureAndVideoFrames) {
nissed30a1112016-04-18 05:15:22 -07002198 class FrameObserver : public rtc::VideoSinkInterface<VideoFrame> {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002199 public:
Peter Boström5811a392015-12-10 13:02:50 +01002200 FrameObserver() : output_frame_event_(false, false) {}
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002201
nissed30a1112016-04-18 05:15:22 -07002202 void OnFrame(const VideoFrame& video_frame) override {
2203 output_frames_.push_back(video_frame);
Peter Boström5811a392015-12-10 13:02:50 +01002204 output_frame_event_.Set();
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002205 }
2206
2207 void WaitOutputFrame() {
Peter Boström5811a392015-12-10 13:02:50 +01002208 const int kWaitFrameTimeoutMs = 3000;
2209 EXPECT_TRUE(output_frame_event_.Wait(kWaitFrameTimeoutMs))
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002210 << "Timeout while waiting for output frames.";
2211 }
2212
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002213 const std::vector<VideoFrame>& output_frames() const {
magjed@webrtc.org2d2a30c2015-03-24 12:37:36 +00002214 return output_frames_;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002215 }
2216
2217 private:
2218 // Delivered output frames.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002219 std::vector<VideoFrame> output_frames_;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002220
2221 // Indicate an output frame has arrived.
Peter Boström5811a392015-12-10 13:02:50 +01002222 rtc::Event output_frame_event_;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002223 };
2224
solenberg4fbae2b2015-08-28 04:07:10 -07002225 test::NullTransport transport;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002226 FrameObserver observer;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002227 std::vector<VideoFrame> input_frames;
perkjfa10b552016-10-02 23:45:26 -07002228
eladalon413ee9a2017-08-22 04:02:52 -07002229 task_queue_.SendTask([this, &transport, &observer, &input_frames]() {
2230 // Initialize send stream.
2231 CreateSenderCall(Call::Config(event_log_.get()));
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002232
eladalon413ee9a2017-08-22 04:02:52 -07002233 CreateSendConfig(1, 0, 0, &transport);
2234 video_send_config_.pre_encode_callback = &observer;
2235 CreateVideoStreams();
2236
2237 // Prepare five input frames. Send ordinary VideoFrame and texture frames
2238 // alternatively.
2239 int width = 168;
2240 int height = 132;
2241
2242 input_frames.push_back(test::FakeNativeBuffer::CreateFrame(
2243 width, height, 1, 1, kVideoRotation_0));
2244 input_frames.push_back(test::FakeNativeBuffer::CreateFrame(
2245 width, height, 2, 2, kVideoRotation_0));
2246 input_frames.push_back(CreateVideoFrame(width, height, 3));
2247 input_frames.push_back(CreateVideoFrame(width, height, 4));
2248 input_frames.push_back(test::FakeNativeBuffer::CreateFrame(
2249 width, height, 5, 5, kVideoRotation_0));
2250
2251 video_send_stream_->Start();
2252 test::FrameForwarder forwarder;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002253 video_send_stream_->SetSource(&forwarder,
2254 DegradationPreference::MAINTAIN_FRAMERATE);
eladalon413ee9a2017-08-22 04:02:52 -07002255 for (size_t i = 0; i < input_frames.size(); i++) {
2256 forwarder.IncomingCapturedFrame(input_frames[i]);
2257 // Wait until the output frame is received before sending the next input
2258 // frame. Or the previous input frame may be replaced without delivering.
2259 observer.WaitOutputFrame();
2260 }
2261 video_send_stream_->Stop();
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07002262 video_send_stream_->SetSource(nullptr,
2263 DegradationPreference::MAINTAIN_FRAMERATE);
eladalon413ee9a2017-08-22 04:02:52 -07002264 });
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002265
2266 // Test if the input and output frames are the same. render_time_ms and
2267 // timestamp are not compared because capturer sets those values.
magjed@webrtc.org2d2a30c2015-03-24 12:37:36 +00002268 ExpectEqualFramesVector(input_frames, observer.output_frames());
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002269
eladalon413ee9a2017-08-22 04:02:52 -07002270 task_queue_.SendTask([this]() {
2271 DestroyStreams();
2272 DestroyCalls();
2273 });
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002274}
2275
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002276void ExpectEqualFramesVector(const std::vector<VideoFrame>& frames1,
2277 const std::vector<VideoFrame>& frames2) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002278 EXPECT_EQ(frames1.size(), frames2.size());
2279 for (size_t i = 0; i < std::min(frames1.size(), frames2.size()); ++i)
nisse26acec42016-04-15 03:43:39 -07002280 // Compare frame buffers, since we don't care about differing timestamps.
2281 EXPECT_TRUE(test::FrameBufsEqual(frames1[i].video_frame_buffer(),
2282 frames2[i].video_frame_buffer()));
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002283}
2284
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002285VideoFrame CreateVideoFrame(int width, int height, uint8_t data) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002286 const int kSizeY = width * height * 2;
kwiberg27f982b2016-03-01 11:52:33 -08002287 std::unique_ptr<uint8_t[]> buffer(new uint8_t[kSizeY]);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002288 memset(buffer.get(), data, kSizeY);
Magnus Jedvert90e31902017-06-07 11:32:50 +02002289 VideoFrame frame(I420Buffer::Create(width, height), kVideoRotation_0, data);
magjed@webrtc.org2d2a30c2015-03-24 12:37:36 +00002290 frame.set_timestamp(data);
nisse1c0dea82017-01-30 02:43:18 -08002291 // Use data as a ms timestamp.
2292 frame.set_timestamp_us(data * rtc::kNumMicrosecsPerMillisec);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002293 return frame;
2294}
2295
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002296TEST_F(VideoSendStreamTest, EncoderIsProperlyInitializedAndDestroyed) {
2297 class EncoderStateObserver : public test::SendTest, public VideoEncoder {
2298 public:
eladalon413ee9a2017-08-22 04:02:52 -07002299 explicit EncoderStateObserver(
2300 test::SingleThreadedTaskQueueForTesting* task_queue)
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002301 : SendTest(kDefaultTimeoutMs),
eladalon413ee9a2017-08-22 04:02:52 -07002302 task_queue_(task_queue),
Erik Språng737336d2016-07-29 12:59:36 +02002303 stream_(nullptr),
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002304 initialized_(false),
2305 callback_registered_(false),
2306 num_releases_(0),
Niels Möller4db138e2018-04-19 09:04:13 +02002307 released_(false),
2308 encoder_factory_(this) {}
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002309
2310 bool IsReleased() {
Peter Boströmf2f82832015-05-01 13:00:41 +02002311 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002312 return released_;
2313 }
2314
2315 bool IsReadyForEncode() {
Peter Boströmf2f82832015-05-01 13:00:41 +02002316 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002317 return initialized_ && callback_registered_;
2318 }
2319
2320 size_t num_releases() {
Peter Boströmf2f82832015-05-01 13:00:41 +02002321 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002322 return num_releases_;
2323 }
2324
2325 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002326 int32_t InitEncode(const VideoCodec* codecSettings,
2327 int32_t numberOfCores,
2328 size_t maxPayloadSize) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02002329 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002330 EXPECT_FALSE(initialized_);
2331 initialized_ = true;
2332 released_ = false;
2333 return 0;
2334 }
2335
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002336 int32_t Encode(const VideoFrame& inputImage,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002337 const CodecSpecificInfo* codecSpecificInfo,
pbos22993e12015-10-19 02:39:06 -07002338 const std::vector<FrameType>* frame_types) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002339 EXPECT_TRUE(IsReadyForEncode());
2340
Peter Boström5811a392015-12-10 13:02:50 +01002341 observation_complete_.Set();
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002342 return 0;
2343 }
2344
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002345 int32_t RegisterEncodeCompleteCallback(
2346 EncodedImageCallback* callback) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02002347 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002348 EXPECT_TRUE(initialized_);
2349 callback_registered_ = true;
2350 return 0;
2351 }
2352
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002353 int32_t Release() override {
Peter Boströmf2f82832015-05-01 13:00:41 +02002354 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002355 EXPECT_TRUE(IsReadyForEncode());
2356 EXPECT_FALSE(released_);
2357 initialized_ = false;
2358 callback_registered_ = false;
2359 released_ = true;
2360 ++num_releases_;
2361 return 0;
2362 }
2363
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002364 int32_t SetChannelParameters(uint32_t packetLoss, int64_t rtt) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002365 EXPECT_TRUE(IsReadyForEncode());
2366 return 0;
2367 }
2368
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002369 int32_t SetRates(uint32_t newBitRate, uint32_t frameRate) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002370 EXPECT_TRUE(IsReadyForEncode());
2371 return 0;
2372 }
2373
stefanff483612015-12-21 03:14:00 -08002374 void OnVideoStreamsCreated(
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002375 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002376 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002377 stream_ = send_stream;
2378 }
2379
stefanff483612015-12-21 03:14:00 -08002380 void ModifyVideoConfigs(
2381 VideoSendStream::Config* send_config,
2382 std::vector<VideoReceiveStream::Config>* receive_configs,
2383 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +02002384 send_config->encoder_settings.encoder_factory = &encoder_factory_;
perkj26091b12016-09-01 01:17:40 -07002385 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002386 }
2387
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002388 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01002389 EXPECT_TRUE(Wait()) << "Timed out while waiting for Encode.";
eladalon413ee9a2017-08-22 04:02:52 -07002390
2391 task_queue_->SendTask([this]() {
2392 EXPECT_EQ(0u, num_releases());
2393 stream_->ReconfigureVideoEncoder(std::move(encoder_config_));
2394 EXPECT_EQ(0u, num_releases());
2395 stream_->Stop();
2396 // Encoder should not be released before destroying the VideoSendStream.
2397 EXPECT_FALSE(IsReleased());
2398 EXPECT_TRUE(IsReadyForEncode());
2399 stream_->Start();
2400 });
2401
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002402 // Sanity check, make sure we still encode frames with this encoder.
Peter Boström5811a392015-12-10 13:02:50 +01002403 EXPECT_TRUE(Wait()) << "Timed out while waiting for Encode.";
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002404 }
2405
eladalon413ee9a2017-08-22 04:02:52 -07002406 test::SingleThreadedTaskQueueForTesting* const task_queue_;
Peter Boströmf2f82832015-05-01 13:00:41 +02002407 rtc::CriticalSection crit_;
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002408 VideoSendStream* stream_;
danilchapa37de392017-09-09 04:17:22 -07002409 bool initialized_ RTC_GUARDED_BY(crit_);
2410 bool callback_registered_ RTC_GUARDED_BY(crit_);
2411 size_t num_releases_ RTC_GUARDED_BY(crit_);
2412 bool released_ RTC_GUARDED_BY(crit_);
Niels Möller4db138e2018-04-19 09:04:13 +02002413 test::EncoderProxyFactory encoder_factory_;
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002414 VideoEncoderConfig encoder_config_;
eladalon413ee9a2017-08-22 04:02:52 -07002415 } test_encoder(&task_queue_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002416
stefane74eef12016-01-08 06:47:13 -08002417 RunBaseTest(&test_encoder);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002418
2419 EXPECT_TRUE(test_encoder.IsReleased());
Per21d45d22016-10-30 21:37:57 +01002420 EXPECT_EQ(1u, test_encoder.num_releases());
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002421}
2422
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002423TEST_F(VideoSendStreamTest, EncoderSetupPropagatesCommonEncoderConfigValues) {
2424 class VideoCodecConfigObserver : public test::SendTest,
2425 public test::FakeEncoder {
2426 public:
2427 VideoCodecConfigObserver()
2428 : SendTest(kDefaultTimeoutMs),
2429 FakeEncoder(Clock::GetRealTimeClock()),
pbos14fe7082016-04-20 06:35:56 -07002430 init_encode_event_(false, false),
Erik Språng737336d2016-07-29 12:59:36 +02002431 num_initializations_(0),
Niels Möller4db138e2018-04-19 09:04:13 +02002432 stream_(nullptr),
2433 encoder_factory_(this) {}
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002434
2435 private:
stefanff483612015-12-21 03:14:00 -08002436 void ModifyVideoConfigs(
2437 VideoSendStream::Config* send_config,
2438 std::vector<VideoReceiveStream::Config>* receive_configs,
2439 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +02002440 send_config->encoder_settings.encoder_factory = &encoder_factory_;
sprangf24a0642017-02-28 13:23:26 -08002441 encoder_config->max_bitrate_bps = kFirstMaxBitrateBps;
perkj26091b12016-09-01 01:17:40 -07002442 encoder_config_ = encoder_config->Copy();
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002443 }
2444
stefanff483612015-12-21 03:14:00 -08002445 void OnVideoStreamsCreated(
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002446 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002447 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002448 stream_ = send_stream;
2449 }
2450
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002451 int32_t InitEncode(const VideoCodec* config,
2452 int32_t number_of_cores,
2453 size_t max_payload_size) override {
Per21d45d22016-10-30 21:37:57 +01002454 if (num_initializations_ == 0) {
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002455 // Verify default values.
sprangf24a0642017-02-28 13:23:26 -08002456 EXPECT_EQ(kFirstMaxBitrateBps / 1000, config->maxBitrate);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002457 } else {
2458 // Verify that changed values are propagated.
sprangf24a0642017-02-28 13:23:26 -08002459 EXPECT_EQ(kSecondMaxBitrateBps / 1000, config->maxBitrate);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002460 }
2461 ++num_initializations_;
Per21d45d22016-10-30 21:37:57 +01002462 init_encode_event_.Set();
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002463 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
2464 }
2465
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002466 void PerformTest() override {
pbos14fe7082016-04-20 06:35:56 -07002467 EXPECT_TRUE(init_encode_event_.Wait(kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002468 EXPECT_EQ(1u, num_initializations_) << "VideoEncoder not initialized.";
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002469
sprangf24a0642017-02-28 13:23:26 -08002470 encoder_config_.max_bitrate_bps = kSecondMaxBitrateBps;
perkj26091b12016-09-01 01:17:40 -07002471 stream_->ReconfigureVideoEncoder(std::move(encoder_config_));
pbos14fe7082016-04-20 06:35:56 -07002472 EXPECT_TRUE(init_encode_event_.Wait(kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002473 EXPECT_EQ(2u, num_initializations_)
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002474 << "ReconfigureVideoEncoder did not reinitialize the encoder with "
2475 "new encoder settings.";
2476 }
2477
sprangf24a0642017-02-28 13:23:26 -08002478 const uint32_t kFirstMaxBitrateBps = 1000000;
2479 const uint32_t kSecondMaxBitrateBps = 2000000;
2480
pbos14fe7082016-04-20 06:35:56 -07002481 rtc::Event init_encode_event_;
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002482 size_t num_initializations_;
2483 VideoSendStream* stream_;
Niels Möller4db138e2018-04-19 09:04:13 +02002484 test::EncoderProxyFactory encoder_factory_;
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002485 VideoEncoderConfig encoder_config_;
2486 } test;
2487
stefane74eef12016-01-08 06:47:13 -08002488 RunBaseTest(&test);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002489}
2490
Sergey Silkin571e6c92018-04-03 10:03:31 +02002491static const size_t kVideoCodecConfigObserverNumberOfTemporalLayers = 3;
Peter Boström53eda3d2015-03-27 15:53:18 +01002492template <typename T>
2493class VideoCodecConfigObserver : public test::SendTest,
2494 public test::FakeEncoder {
Peter Boström53eda3d2015-03-27 15:53:18 +01002495 public:
2496 VideoCodecConfigObserver(VideoCodecType video_codec_type,
2497 const char* codec_name)
2498 : SendTest(VideoSendStreamTest::kDefaultTimeoutMs),
2499 FakeEncoder(Clock::GetRealTimeClock()),
2500 video_codec_type_(video_codec_type),
2501 codec_name_(codec_name),
pbos14fe7082016-04-20 06:35:56 -07002502 init_encode_event_(false, false),
Erik Språng737336d2016-07-29 12:59:36 +02002503 num_initializations_(0),
Niels Möller4db138e2018-04-19 09:04:13 +02002504 stream_(nullptr),
2505 encoder_factory_(this) {
Sergey Silkin86684962018-03-28 19:32:37 +02002506 InitCodecSpecifics();
Peter Boström53eda3d2015-03-27 15:53:18 +01002507 }
2508
2509 private:
perkjfa10b552016-10-02 23:45:26 -07002510 class VideoStreamFactory
2511 : public VideoEncoderConfig::VideoStreamFactoryInterface {
2512 public:
2513 VideoStreamFactory() {}
2514
2515 private:
2516 std::vector<VideoStream> CreateEncoderStreams(
2517 int width,
2518 int height,
2519 const VideoEncoderConfig& encoder_config) override {
2520 std::vector<VideoStream> streams =
2521 test::CreateVideoStreams(width, height, encoder_config);
2522 for (size_t i = 0; i < streams.size(); ++i) {
Sergey Silkina796a7e2018-03-01 15:11:29 +01002523 streams[i].num_temporal_layers =
2524 kVideoCodecConfigObserverNumberOfTemporalLayers;
perkjfa10b552016-10-02 23:45:26 -07002525 }
2526 return streams;
2527 }
2528 };
2529
stefanff483612015-12-21 03:14:00 -08002530 void ModifyVideoConfigs(
2531 VideoSendStream::Config* send_config,
2532 std::vector<VideoReceiveStream::Config>* receive_configs,
2533 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +02002534 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Niels Möller259a4972018-04-05 15:36:51 +02002535 send_config->rtp.payload_name = codec_name_;
Peter Boström53eda3d2015-03-27 15:53:18 +01002536
Niels Möller259a4972018-04-05 15:36:51 +02002537 encoder_config->codec_type = video_codec_type_;
kthelgason29a44e32016-09-27 03:52:02 -07002538 encoder_config->encoder_specific_settings = GetEncoderSpecificSettings();
perkjfa10b552016-10-02 23:45:26 -07002539 encoder_config->video_stream_factory =
2540 new rtc::RefCountedObject<VideoStreamFactory>();
perkj26091b12016-09-01 01:17:40 -07002541 encoder_config_ = encoder_config->Copy();
Peter Boström53eda3d2015-03-27 15:53:18 +01002542 }
2543
stefanff483612015-12-21 03:14:00 -08002544 void OnVideoStreamsCreated(
Peter Boström53eda3d2015-03-27 15:53:18 +01002545 VideoSendStream* send_stream,
2546 const std::vector<VideoReceiveStream*>& receive_streams) override {
2547 stream_ = send_stream;
2548 }
2549
2550 int32_t InitEncode(const VideoCodec* config,
2551 int32_t number_of_cores,
2552 size_t max_payload_size) override {
2553 EXPECT_EQ(video_codec_type_, config->codecType);
2554 VerifyCodecSpecifics(*config);
2555 ++num_initializations_;
Per21d45d22016-10-30 21:37:57 +01002556 init_encode_event_.Set();
Peter Boström53eda3d2015-03-27 15:53:18 +01002557 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
2558 }
2559
Sergey Silkin86684962018-03-28 19:32:37 +02002560 void InitCodecSpecifics();
Peter Boström53eda3d2015-03-27 15:53:18 +01002561 void VerifyCodecSpecifics(const VideoCodec& config) const;
kthelgason29a44e32016-09-27 03:52:02 -07002562 rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2563 GetEncoderSpecificSettings() const;
Peter Boström53eda3d2015-03-27 15:53:18 +01002564
2565 void PerformTest() override {
pbos14fe7082016-04-20 06:35:56 -07002566 EXPECT_TRUE(
2567 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002568 ASSERT_EQ(1u, num_initializations_) << "VideoEncoder not initialized.";
Peter Boström53eda3d2015-03-27 15:53:18 +01002569
Sergey Silkin86684962018-03-28 19:32:37 +02002570 // Change encoder settings to actually trigger reconfiguration.
2571 encoder_settings_.frameDroppingOn = !encoder_settings_.frameDroppingOn;
kthelgason29a44e32016-09-27 03:52:02 -07002572 encoder_config_.encoder_specific_settings = GetEncoderSpecificSettings();
perkj26091b12016-09-01 01:17:40 -07002573 stream_->ReconfigureVideoEncoder(std::move(encoder_config_));
pbos14fe7082016-04-20 06:35:56 -07002574 ASSERT_TRUE(
2575 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002576 EXPECT_EQ(2u, num_initializations_)
Peter Boström53eda3d2015-03-27 15:53:18 +01002577 << "ReconfigureVideoEncoder did not reinitialize the encoder with "
2578 "new encoder settings.";
2579 }
2580
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002581 int32_t Encode(const VideoFrame& input_image,
Peter Boström53eda3d2015-03-27 15:53:18 +01002582 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -07002583 const std::vector<FrameType>* frame_types) override {
Peter Boström53eda3d2015-03-27 15:53:18 +01002584 // Silently skip the encode, FakeEncoder::Encode doesn't produce VP8.
2585 return 0;
2586 }
2587
2588 T encoder_settings_;
2589 const VideoCodecType video_codec_type_;
2590 const char* const codec_name_;
pbos14fe7082016-04-20 06:35:56 -07002591 rtc::Event init_encode_event_;
Peter Boström53eda3d2015-03-27 15:53:18 +01002592 size_t num_initializations_;
2593 VideoSendStream* stream_;
Niels Möller4db138e2018-04-19 09:04:13 +02002594 test::EncoderProxyFactory encoder_factory_;
Peter Boström53eda3d2015-03-27 15:53:18 +01002595 VideoEncoderConfig encoder_config_;
2596};
2597
2598template <>
Sergey Silkin86684962018-03-28 19:32:37 +02002599void VideoCodecConfigObserver<VideoCodecH264>::InitCodecSpecifics() {
2600 encoder_settings_ = VideoEncoder::GetDefaultH264Settings();
2601}
2602
2603template <>
Peter Boström53eda3d2015-03-27 15:53:18 +01002604void VideoCodecConfigObserver<VideoCodecH264>::VerifyCodecSpecifics(
2605 const VideoCodec& config) const {
hta257dc392016-10-25 09:05:06 -07002606 EXPECT_EQ(
2607 0, memcmp(&config.H264(), &encoder_settings_, sizeof(encoder_settings_)));
Peter Boström53eda3d2015-03-27 15:53:18 +01002608}
kthelgason29a44e32016-09-27 03:52:02 -07002609
2610template <>
2611rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2612VideoCodecConfigObserver<VideoCodecH264>::GetEncoderSpecificSettings() const {
2613 return new rtc::RefCountedObject<
2614 VideoEncoderConfig::H264EncoderSpecificSettings>(encoder_settings_);
2615}
2616
Peter Boström53eda3d2015-03-27 15:53:18 +01002617template <>
Sergey Silkin86684962018-03-28 19:32:37 +02002618void VideoCodecConfigObserver<VideoCodecVP8>::InitCodecSpecifics() {
2619 encoder_settings_ = VideoEncoder::GetDefaultVp8Settings();
2620}
2621
2622template <>
Peter Boström53eda3d2015-03-27 15:53:18 +01002623void VideoCodecConfigObserver<VideoCodecVP8>::VerifyCodecSpecifics(
2624 const VideoCodec& config) const {
2625 // Check that the number of temporal layers has propagated properly to
2626 // VideoCodec.
2627 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
hta257dc392016-10-25 09:05:06 -07002628 config.VP8().numberOfTemporalLayers);
Peter Boström53eda3d2015-03-27 15:53:18 +01002629
2630 for (unsigned char i = 0; i < config.numberOfSimulcastStreams; ++i) {
2631 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
2632 config.simulcastStream[i].numberOfTemporalLayers);
2633 }
2634
2635 // Set expected temporal layers as they should have been set when
Erik Språng82fad3d2018-03-21 09:57:23 +01002636 // reconfiguring the encoder and not match the set config.
Peter Boström53eda3d2015-03-27 15:53:18 +01002637 VideoCodecVP8 encoder_settings = encoder_settings_;
2638 encoder_settings.numberOfTemporalLayers =
2639 kVideoCodecConfigObserverNumberOfTemporalLayers;
hta257dc392016-10-25 09:05:06 -07002640 EXPECT_EQ(
2641 0, memcmp(&config.VP8(), &encoder_settings, sizeof(encoder_settings_)));
Peter Boström53eda3d2015-03-27 15:53:18 +01002642}
kthelgason29a44e32016-09-27 03:52:02 -07002643
2644template <>
2645rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2646VideoCodecConfigObserver<VideoCodecVP8>::GetEncoderSpecificSettings() const {
2647 return new rtc::RefCountedObject<
2648 VideoEncoderConfig::Vp8EncoderSpecificSettings>(encoder_settings_);
2649}
2650
Peter Boström53eda3d2015-03-27 15:53:18 +01002651template <>
Sergey Silkin86684962018-03-28 19:32:37 +02002652void VideoCodecConfigObserver<VideoCodecVP9>::InitCodecSpecifics() {
2653 encoder_settings_ = VideoEncoder::GetDefaultVp9Settings();
2654}
2655
2656template <>
Peter Boström53eda3d2015-03-27 15:53:18 +01002657void VideoCodecConfigObserver<VideoCodecVP9>::VerifyCodecSpecifics(
2658 const VideoCodec& config) const {
2659 // Check that the number of temporal layers has propagated properly to
2660 // VideoCodec.
2661 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
hta257dc392016-10-25 09:05:06 -07002662 config.VP9().numberOfTemporalLayers);
Peter Boström53eda3d2015-03-27 15:53:18 +01002663
2664 for (unsigned char i = 0; i < config.numberOfSimulcastStreams; ++i) {
2665 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
2666 config.simulcastStream[i].numberOfTemporalLayers);
2667 }
2668
2669 // Set expected temporal layers as they should have been set when
2670 // reconfiguring the encoder and not match the set config.
2671 VideoCodecVP9 encoder_settings = encoder_settings_;
2672 encoder_settings.numberOfTemporalLayers =
2673 kVideoCodecConfigObserverNumberOfTemporalLayers;
hta257dc392016-10-25 09:05:06 -07002674 EXPECT_EQ(
2675 0, memcmp(&(config.VP9()), &encoder_settings, sizeof(encoder_settings_)));
Peter Boström53eda3d2015-03-27 15:53:18 +01002676}
2677
kthelgason29a44e32016-09-27 03:52:02 -07002678template <>
2679rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2680VideoCodecConfigObserver<VideoCodecVP9>::GetEncoderSpecificSettings() const {
2681 return new rtc::RefCountedObject<
2682 VideoEncoderConfig::Vp9EncoderSpecificSettings>(encoder_settings_);
2683}
2684
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002685TEST_F(VideoSendStreamTest, EncoderSetupPropagatesVp8Config) {
Peter Boström53eda3d2015-03-27 15:53:18 +01002686 VideoCodecConfigObserver<VideoCodecVP8> test(kVideoCodecVP8, "VP8");
stefane74eef12016-01-08 06:47:13 -08002687 RunBaseTest(&test);
Peter Boström53eda3d2015-03-27 15:53:18 +01002688}
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002689
Peter Boström53eda3d2015-03-27 15:53:18 +01002690TEST_F(VideoSendStreamTest, EncoderSetupPropagatesVp9Config) {
2691 VideoCodecConfigObserver<VideoCodecVP9> test(kVideoCodecVP9, "VP9");
stefane74eef12016-01-08 06:47:13 -08002692 RunBaseTest(&test);
Peter Boström53eda3d2015-03-27 15:53:18 +01002693}
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002694
Peter Boström53eda3d2015-03-27 15:53:18 +01002695TEST_F(VideoSendStreamTest, EncoderSetupPropagatesH264Config) {
2696 VideoCodecConfigObserver<VideoCodecH264> test(kVideoCodecH264, "H264");
stefane74eef12016-01-08 06:47:13 -08002697 RunBaseTest(&test);
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002698}
2699
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002700TEST_F(VideoSendStreamTest, RtcpSenderReportContainsMediaBytesSent) {
asapersson@webrtc.org14674212015-02-23 08:14:07 +00002701 class RtcpSenderReportTest : public test::SendTest {
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002702 public:
Yves Gerey665174f2018-06-19 15:03:05 +02002703 RtcpSenderReportTest()
2704 : SendTest(kDefaultTimeoutMs),
2705 rtp_packets_sent_(0),
2706 media_bytes_sent_(0) {}
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002707
2708 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002709 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefan4b569042015-11-11 06:39:57 -08002710 rtc::CritScope lock(&crit_);
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002711 RTPHeader header;
2712 EXPECT_TRUE(parser_->Parse(packet, length, &header));
asapersson@webrtc.org14674212015-02-23 08:14:07 +00002713 ++rtp_packets_sent_;
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002714 media_bytes_sent_ += length - header.headerLength - header.paddingLength;
2715 return SEND_PACKET;
2716 }
2717
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002718 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
stefan4b569042015-11-11 06:39:57 -08002719 rtc::CritScope lock(&crit_);
danilchap3dc929e2016-11-02 08:21:59 -07002720 test::RtcpPacketParser parser;
2721 EXPECT_TRUE(parser.Parse(packet, length));
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002722
danilchap3dc929e2016-11-02 08:21:59 -07002723 if (parser.sender_report()->num_packets() > 0) {
2724 // Only compare sent media bytes if SenderPacketCount matches the
2725 // number of sent rtp packets (a new rtp packet could be sent before
2726 // the rtcp packet).
2727 if (parser.sender_report()->sender_octet_count() > 0 &&
2728 parser.sender_report()->sender_packet_count() ==
2729 rtp_packets_sent_) {
2730 EXPECT_EQ(media_bytes_sent_,
2731 parser.sender_report()->sender_octet_count());
2732 observation_complete_.Set();
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002733 }
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002734 }
2735
2736 return SEND_PACKET;
2737 }
2738
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002739 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01002740 EXPECT_TRUE(Wait()) << "Timed out while waiting for RTCP sender report.";
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002741 }
2742
stefan4b569042015-11-11 06:39:57 -08002743 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07002744 size_t rtp_packets_sent_ RTC_GUARDED_BY(&crit_);
2745 size_t media_bytes_sent_ RTC_GUARDED_BY(&crit_);
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002746 } test;
2747
stefane74eef12016-01-08 06:47:13 -08002748 RunBaseTest(&test);
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002749}
2750
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002751TEST_F(VideoSendStreamTest, TranslatesTwoLayerScreencastToTargetBitrate) {
Sergey Silkina796a7e2018-03-01 15:11:29 +01002752 static const int kScreencastMaxTargetBitrateDeltaKbps = 1;
perkjfa10b552016-10-02 23:45:26 -07002753
2754 class VideoStreamFactory
2755 : public VideoEncoderConfig::VideoStreamFactoryInterface {
2756 public:
2757 VideoStreamFactory() {}
2758
2759 private:
2760 std::vector<VideoStream> CreateEncoderStreams(
2761 int width,
2762 int height,
2763 const VideoEncoderConfig& encoder_config) override {
2764 std::vector<VideoStream> streams =
2765 test::CreateVideoStreams(width, height, encoder_config);
Sergey Silkina796a7e2018-03-01 15:11:29 +01002766 EXPECT_FALSE(streams[0].num_temporal_layers.has_value());
2767 streams[0].num_temporal_layers = 2;
2768 RTC_CHECK_GT(streams[0].max_bitrate_bps,
2769 kScreencastMaxTargetBitrateDeltaKbps);
2770 streams[0].target_bitrate_bps =
2771 streams[0].max_bitrate_bps -
2772 kScreencastMaxTargetBitrateDeltaKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -07002773 return streams;
2774 }
2775 };
2776
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002777 class ScreencastTargetBitrateTest : public test::SendTest,
2778 public test::FakeEncoder {
2779 public:
2780 ScreencastTargetBitrateTest()
2781 : SendTest(kDefaultTimeoutMs),
Niels Möller4db138e2018-04-19 09:04:13 +02002782 test::FakeEncoder(Clock::GetRealTimeClock()),
2783 encoder_factory_(this) {}
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002784
2785 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002786 int32_t InitEncode(const VideoCodec* config,
2787 int32_t number_of_cores,
2788 size_t max_payload_size) override {
Sergey Silkina796a7e2018-03-01 15:11:29 +01002789 EXPECT_EQ(static_cast<unsigned int>(kScreencastMaxTargetBitrateDeltaKbps),
2790 config->maxBitrate - config->targetBitrate);
Peter Boström5811a392015-12-10 13:02:50 +01002791 observation_complete_.Set();
Yves Gerey665174f2018-06-19 15:03:05 +02002792 return test::FakeEncoder::InitEncode(config, number_of_cores,
2793 max_payload_size);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002794 }
stefanff483612015-12-21 03:14:00 -08002795 void ModifyVideoConfigs(
2796 VideoSendStream::Config* send_config,
2797 std::vector<VideoReceiveStream::Config>* receive_configs,
2798 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +02002799 send_config->encoder_settings.encoder_factory = &encoder_factory_;
perkjfa10b552016-10-02 23:45:26 -07002800 EXPECT_EQ(1u, encoder_config->number_of_streams);
2801 encoder_config->video_stream_factory =
2802 new rtc::RefCountedObject<VideoStreamFactory>();
Erik Språng143cec12015-04-28 10:01:41 +02002803 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002804 }
2805
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002806 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01002807 EXPECT_TRUE(Wait())
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002808 << "Timed out while waiting for the encoder to be initialized.";
2809 }
Niels Möller4db138e2018-04-19 09:04:13 +02002810 test::EncoderProxyFactory encoder_factory_;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002811 } test;
2812
stefane74eef12016-01-08 06:47:13 -08002813 RunBaseTest(&test);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002814}
pbos@webrtc.org00873182014-11-25 14:03:34 +00002815
philipelc6957c72016-04-28 15:52:49 +02002816TEST_F(VideoSendStreamTest, ReconfigureBitratesSetsEncoderBitratesCorrectly) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002817 // These are chosen to be "kind of odd" to not be accidentally checked against
2818 // default values.
2819 static const int kMinBitrateKbps = 137;
2820 static const int kStartBitrateKbps = 345;
2821 static const int kLowerMaxBitrateKbps = 312;
2822 static const int kMaxBitrateKbps = 413;
2823 static const int kIncreasedStartBitrateKbps = 451;
2824 static const int kIncreasedMaxBitrateKbps = 597;
2825 class EncoderBitrateThresholdObserver : public test::SendTest,
2826 public test::FakeEncoder {
2827 public:
eladalon413ee9a2017-08-22 04:02:52 -07002828 explicit EncoderBitrateThresholdObserver(
2829 test::SingleThreadedTaskQueueForTesting* task_queue)
pbos@webrtc.org00873182014-11-25 14:03:34 +00002830 : SendTest(kDefaultTimeoutMs),
2831 FakeEncoder(Clock::GetRealTimeClock()),
eladalon413ee9a2017-08-22 04:02:52 -07002832 task_queue_(task_queue),
pbos14fe7082016-04-20 06:35:56 -07002833 init_encode_event_(false, false),
perkj26091b12016-09-01 01:17:40 -07002834 bitrate_changed_event_(false, false),
2835 target_bitrate_(0),
Erik Språng737336d2016-07-29 12:59:36 +02002836 num_initializations_(0),
2837 call_(nullptr),
Niels Möller4db138e2018-04-19 09:04:13 +02002838 send_stream_(nullptr),
2839 encoder_factory_(this) {}
pbos@webrtc.org00873182014-11-25 14:03:34 +00002840
2841 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002842 int32_t InitEncode(const VideoCodec* codecSettings,
2843 int32_t numberOfCores,
2844 size_t maxPayloadSize) override {
perkj26091b12016-09-01 01:17:40 -07002845 EXPECT_GE(codecSettings->startBitrate, codecSettings->minBitrate);
2846 EXPECT_LE(codecSettings->startBitrate, codecSettings->maxBitrate);
Per21d45d22016-10-30 21:37:57 +01002847 if (num_initializations_ == 0) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002848 EXPECT_EQ(static_cast<unsigned int>(kMinBitrateKbps),
2849 codecSettings->minBitrate);
2850 EXPECT_EQ(static_cast<unsigned int>(kStartBitrateKbps),
2851 codecSettings->startBitrate);
2852 EXPECT_EQ(static_cast<unsigned int>(kMaxBitrateKbps),
2853 codecSettings->maxBitrate);
Peter Boström5811a392015-12-10 13:02:50 +01002854 observation_complete_.Set();
Per21d45d22016-10-30 21:37:57 +01002855 } else if (num_initializations_ == 1) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002856 EXPECT_EQ(static_cast<unsigned int>(kLowerMaxBitrateKbps),
2857 codecSettings->maxBitrate);
2858 // The start bitrate should be kept (-1) and capped to the max bitrate.
2859 // Since this is not an end-to-end call no receiver should have been
2860 // returning a REMB that could lower this estimate.
2861 EXPECT_EQ(codecSettings->startBitrate, codecSettings->maxBitrate);
Per21d45d22016-10-30 21:37:57 +01002862 } else if (num_initializations_ == 2) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002863 EXPECT_EQ(static_cast<unsigned int>(kIncreasedMaxBitrateKbps),
2864 codecSettings->maxBitrate);
perkj26091b12016-09-01 01:17:40 -07002865 // The start bitrate will be whatever the rate BitRateController
2866 // has currently configured but in the span of the set max and min
2867 // bitrate.
pbos@webrtc.org00873182014-11-25 14:03:34 +00002868 }
2869 ++num_initializations_;
Per21d45d22016-10-30 21:37:57 +01002870 init_encode_event_.Set();
2871
pbos@webrtc.org00873182014-11-25 14:03:34 +00002872 return FakeEncoder::InitEncode(codecSettings, numberOfCores,
2873 maxPayloadSize);
2874 }
2875
Erik Språng566124a2018-04-23 12:32:22 +02002876 int32_t SetRateAllocation(const VideoBitrateAllocation& bitrate,
Erik Språng08127a92016-11-16 16:41:30 +01002877 uint32_t frameRate) override {
perkj26091b12016-09-01 01:17:40 -07002878 {
2879 rtc::CritScope lock(&crit_);
Erik Språng08127a92016-11-16 16:41:30 +01002880 if (target_bitrate_ == bitrate.get_sum_kbps()) {
2881 return FakeEncoder::SetRateAllocation(bitrate, frameRate);
perkjfa10b552016-10-02 23:45:26 -07002882 }
Erik Språng08127a92016-11-16 16:41:30 +01002883 target_bitrate_ = bitrate.get_sum_kbps();
perkj26091b12016-09-01 01:17:40 -07002884 }
2885 bitrate_changed_event_.Set();
Erik Språng08127a92016-11-16 16:41:30 +01002886 return FakeEncoder::SetRateAllocation(bitrate, frameRate);
perkj26091b12016-09-01 01:17:40 -07002887 }
2888
2889 void WaitForSetRates(uint32_t expected_bitrate) {
2890 EXPECT_TRUE(
2891 bitrate_changed_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs))
2892 << "Timed out while waiting encoder rate to be set.";
2893 rtc::CritScope lock(&crit_);
2894 EXPECT_EQ(expected_bitrate, target_bitrate_);
2895 }
2896
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002897 Call::Config GetSenderCallConfig() override {
philipel4fb651d2017-04-10 03:54:05 -07002898 Call::Config config(event_log_.get());
Stefan Holmere5904162015-03-26 11:11:06 +01002899 config.bitrate_config.min_bitrate_bps = kMinBitrateKbps * 1000;
2900 config.bitrate_config.start_bitrate_bps = kStartBitrateKbps * 1000;
2901 config.bitrate_config.max_bitrate_bps = kMaxBitrateKbps * 1000;
pbos@webrtc.org00873182014-11-25 14:03:34 +00002902 return config;
2903 }
2904
perkjfa10b552016-10-02 23:45:26 -07002905 class VideoStreamFactory
2906 : public VideoEncoderConfig::VideoStreamFactoryInterface {
2907 public:
2908 explicit VideoStreamFactory(int min_bitrate_bps)
2909 : min_bitrate_bps_(min_bitrate_bps) {}
2910
2911 private:
2912 std::vector<VideoStream> CreateEncoderStreams(
2913 int width,
2914 int height,
2915 const VideoEncoderConfig& encoder_config) override {
2916 std::vector<VideoStream> streams =
2917 test::CreateVideoStreams(width, height, encoder_config);
2918 streams[0].min_bitrate_bps = min_bitrate_bps_;
2919 return streams;
2920 }
2921
2922 const int min_bitrate_bps_;
2923 };
2924
stefanff483612015-12-21 03:14:00 -08002925 void ModifyVideoConfigs(
2926 VideoSendStream::Config* send_config,
2927 std::vector<VideoReceiveStream::Config>* receive_configs,
2928 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +02002929 send_config->encoder_settings.encoder_factory = &encoder_factory_;
pbos@webrtc.org00873182014-11-25 14:03:34 +00002930 // Set bitrates lower/higher than min/max to make sure they are properly
2931 // capped.
perkjfa10b552016-10-02 23:45:26 -07002932 encoder_config->max_bitrate_bps = kMaxBitrateKbps * 1000;
2933 // Create a new StreamFactory to be able to set
2934 // |VideoStream.min_bitrate_bps|.
2935 encoder_config->video_stream_factory =
2936 new rtc::RefCountedObject<VideoStreamFactory>(kMinBitrateKbps * 1000);
perkj26091b12016-09-01 01:17:40 -07002937 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org00873182014-11-25 14:03:34 +00002938 }
2939
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002940 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002941 call_ = sender_call;
2942 }
2943
stefanff483612015-12-21 03:14:00 -08002944 void OnVideoStreamsCreated(
Stefan Holmere5904162015-03-26 11:11:06 +01002945 VideoSendStream* send_stream,
2946 const std::vector<VideoReceiveStream*>& receive_streams) override {
2947 send_stream_ = send_stream;
2948 }
2949
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002950 void PerformTest() override {
pbos14fe7082016-04-20 06:35:56 -07002951 ASSERT_TRUE(
2952 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs))
perkj26091b12016-09-01 01:17:40 -07002953 << "Timed out while waiting for encoder to be configured.";
2954 WaitForSetRates(kStartBitrateKbps);
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01002955 BitrateConstraints bitrate_config;
pbos@webrtc.org00873182014-11-25 14:03:34 +00002956 bitrate_config.start_bitrate_bps = kIncreasedStartBitrateKbps * 1000;
2957 bitrate_config.max_bitrate_bps = kIncreasedMaxBitrateKbps * 1000;
eladalon413ee9a2017-08-22 04:02:52 -07002958 task_queue_->SendTask([this, &bitrate_config]() {
Sebastian Jansson8f83b422018-02-21 13:07:13 +01002959 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
2960 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07002961 });
perkj26091b12016-09-01 01:17:40 -07002962 // Encoder rate is capped by EncoderConfig max_bitrate_bps.
2963 WaitForSetRates(kMaxBitrateKbps);
perkjfa10b552016-10-02 23:45:26 -07002964 encoder_config_.max_bitrate_bps = kLowerMaxBitrateKbps * 1000;
perkj26091b12016-09-01 01:17:40 -07002965 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
pbos14fe7082016-04-20 06:35:56 -07002966 ASSERT_TRUE(
2967 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002968 EXPECT_EQ(2, num_initializations_)
pbos@webrtc.org00873182014-11-25 14:03:34 +00002969 << "Encoder should have been reconfigured with the new value.";
perkjfa10b552016-10-02 23:45:26 -07002970 WaitForSetRates(kLowerMaxBitrateKbps);
2971
2972 encoder_config_.max_bitrate_bps = kIncreasedMaxBitrateKbps * 1000;
2973 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
2974 ASSERT_TRUE(
2975 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002976 EXPECT_EQ(3, num_initializations_)
perkjfa10b552016-10-02 23:45:26 -07002977 << "Encoder should have been reconfigured with the new value.";
perkj26091b12016-09-01 01:17:40 -07002978 // Expected target bitrate is the start bitrate set in the call to
Sebastian Jansson8f83b422018-02-21 13:07:13 +01002979 // call_->GetTransportControllerSend()->SetSdpBitrateParameters.
perkj26091b12016-09-01 01:17:40 -07002980 WaitForSetRates(kIncreasedStartBitrateKbps);
pbos@webrtc.org00873182014-11-25 14:03:34 +00002981 }
2982
eladalon413ee9a2017-08-22 04:02:52 -07002983 test::SingleThreadedTaskQueueForTesting* const task_queue_;
pbos14fe7082016-04-20 06:35:56 -07002984 rtc::Event init_encode_event_;
perkj26091b12016-09-01 01:17:40 -07002985 rtc::Event bitrate_changed_event_;
2986 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07002987 uint32_t target_bitrate_ RTC_GUARDED_BY(&crit_);
perkjfa10b552016-10-02 23:45:26 -07002988
pbos@webrtc.org00873182014-11-25 14:03:34 +00002989 int num_initializations_;
2990 webrtc::Call* call_;
Stefan Holmere5904162015-03-26 11:11:06 +01002991 webrtc::VideoSendStream* send_stream_;
Niels Möller4db138e2018-04-19 09:04:13 +02002992 test::EncoderProxyFactory encoder_factory_;
Stefan Holmere5904162015-03-26 11:11:06 +01002993 webrtc::VideoEncoderConfig encoder_config_;
eladalon413ee9a2017-08-22 04:02:52 -07002994 } test(&task_queue_);
pbos@webrtc.org00873182014-11-25 14:03:34 +00002995
stefane74eef12016-01-08 06:47:13 -08002996 RunBaseTest(&test);
pbos@webrtc.org00873182014-11-25 14:03:34 +00002997}
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002998
2999TEST_F(VideoSendStreamTest, ReportsSentResolution) {
3000 static const size_t kNumStreams = 3;
3001 // Unusual resolutions to make sure that they are the ones being reported.
3002 static const struct {
3003 int width;
3004 int height;
Yves Gerey665174f2018-06-19 15:03:05 +02003005 } kEncodedResolution[kNumStreams] = {{241, 181}, {300, 121}, {121, 221}};
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003006 class ScreencastTargetBitrateTest : public test::SendTest,
3007 public test::FakeEncoder {
3008 public:
3009 ScreencastTargetBitrateTest()
3010 : SendTest(kDefaultTimeoutMs),
Erik Språng737336d2016-07-29 12:59:36 +02003011 test::FakeEncoder(Clock::GetRealTimeClock()),
Niels Möller4db138e2018-04-19 09:04:13 +02003012 send_stream_(nullptr),
3013 encoder_factory_(this) {}
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003014
3015 private:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07003016 int32_t Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00003017 const CodecSpecificInfo* codecSpecificInfo,
pbos22993e12015-10-19 02:39:06 -07003018 const std::vector<FrameType>* frame_types) override {
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003019 CodecSpecificInfo specifics;
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003020 specifics.codecType = kVideoCodecGeneric;
3021
3022 uint8_t buffer[16] = {0};
3023 EncodedImage encoded(buffer, sizeof(buffer), sizeof(buffer));
3024 encoded._timeStamp = input_image.timestamp();
3025 encoded.capture_time_ms_ = input_image.render_time_ms();
3026
3027 for (size_t i = 0; i < kNumStreams; ++i) {
3028 specifics.codecSpecific.generic.simulcast_idx = static_cast<uint8_t>(i);
3029 encoded._frameType = (*frame_types)[i];
3030 encoded._encodedWidth = kEncodedResolution[i].width;
3031 encoded._encodedHeight = kEncodedResolution[i].height;
brandtre78d2662017-01-16 05:57:16 -08003032 EncodedImageCallback* callback;
3033 {
3034 rtc::CritScope cs(&crit_sect_);
3035 callback = callback_;
3036 }
3037 RTC_DCHECK(callback);
3038 if (callback->OnEncodedImage(encoded, &specifics, nullptr).error !=
sergeyu2cb155a2016-11-04 11:39:29 -07003039 EncodedImageCallback::Result::OK) {
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003040 return -1;
sergeyu2cb155a2016-11-04 11:39:29 -07003041 }
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003042 }
3043
Peter Boström5811a392015-12-10 13:02:50 +01003044 observation_complete_.Set();
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003045 return 0;
3046 }
stefanff483612015-12-21 03:14:00 -08003047 void ModifyVideoConfigs(
3048 VideoSendStream::Config* send_config,
3049 std::vector<VideoReceiveStream::Config>* receive_configs,
3050 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +02003051 send_config->encoder_settings.encoder_factory = &encoder_factory_;
perkjfa10b552016-10-02 23:45:26 -07003052 EXPECT_EQ(kNumStreams, encoder_config->number_of_streams);
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003053 }
3054
Stefan Holmer9fea80f2016-01-07 17:43:18 +01003055 size_t GetNumVideoStreams() const override { return kNumStreams; }
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003056
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00003057 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01003058 EXPECT_TRUE(Wait())
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003059 << "Timed out while waiting for the encoder to send one frame.";
3060 VideoSendStream::Stats stats = send_stream_->GetStats();
3061
3062 for (size_t i = 0; i < kNumStreams; ++i) {
Stefan Holmer9fea80f2016-01-07 17:43:18 +01003063 ASSERT_TRUE(stats.substreams.find(kVideoSendSsrcs[i]) !=
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003064 stats.substreams.end())
Stefan Holmer9fea80f2016-01-07 17:43:18 +01003065 << "No stats for SSRC: " << kVideoSendSsrcs[i]
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003066 << ", stats should exist as soon as frames have been encoded.";
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00003067 VideoSendStream::StreamStats ssrc_stats =
Stefan Holmer9fea80f2016-01-07 17:43:18 +01003068 stats.substreams[kVideoSendSsrcs[i]];
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00003069 EXPECT_EQ(kEncodedResolution[i].width, ssrc_stats.width);
3070 EXPECT_EQ(kEncodedResolution[i].height, ssrc_stats.height);
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003071 }
3072 }
3073
stefanff483612015-12-21 03:14:00 -08003074 void OnVideoStreamsCreated(
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003075 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00003076 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003077 send_stream_ = send_stream;
3078 }
3079
3080 VideoSendStream* send_stream_;
Niels Möller4db138e2018-04-19 09:04:13 +02003081 test::EncoderProxyFactory encoder_factory_;
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003082 } test;
3083
stefane74eef12016-01-08 06:47:13 -08003084 RunBaseTest(&test);
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003085}
philipel0f9af012015-09-01 07:01:51 -07003086
Peter Boström12996152016-05-14 02:03:18 +02003087#if !defined(RTC_DISABLE_VP9)
Åsa Perssonff24c042015-12-04 10:58:08 +01003088class Vp9HeaderObserver : public test::SendTest {
philipel0f9af012015-09-01 07:01:51 -07003089 public:
Åsa Perssonff24c042015-12-04 10:58:08 +01003090 Vp9HeaderObserver()
3091 : SendTest(VideoSendStreamTest::kLongTimeoutMs),
Niels Möller4db138e2018-04-19 09:04:13 +02003092 encoder_factory_([]() { return VP9Encoder::Create(); }),
Åsa Perssonff24c042015-12-04 10:58:08 +01003093 vp9_settings_(VideoEncoder::GetDefaultVp9Settings()),
3094 packets_sent_(0),
perkjfa10b552016-10-02 23:45:26 -07003095 frames_sent_(0),
3096 expected_width_(0),
3097 expected_height_(0) {}
philipel7fabd462015-09-03 04:42:32 -07003098
stefanff483612015-12-21 03:14:00 -08003099 virtual void ModifyVideoConfigsHook(
philipel0f9af012015-09-01 07:01:51 -07003100 VideoSendStream::Config* send_config,
3101 std::vector<VideoReceiveStream::Config>* receive_configs,
3102 VideoEncoderConfig* encoder_config) {}
3103
Åsa Perssonff24c042015-12-04 10:58:08 +01003104 virtual void InspectHeader(const RTPVideoHeaderVP9& vp9) = 0;
philipel0f9af012015-09-01 07:01:51 -07003105
3106 private:
minyue20c84cc2017-04-10 16:57:57 -07003107 const int kVp9PayloadType = test::CallTest::kVideoSendPayloadType;
philipel0f9af012015-09-01 07:01:51 -07003108
perkjfa10b552016-10-02 23:45:26 -07003109 class VideoStreamFactory
3110 : public VideoEncoderConfig::VideoStreamFactoryInterface {
3111 public:
3112 explicit VideoStreamFactory(size_t number_of_temporal_layers)
3113 : number_of_temporal_layers_(number_of_temporal_layers) {}
3114
3115 private:
3116 std::vector<VideoStream> CreateEncoderStreams(
3117 int width,
3118 int height,
3119 const VideoEncoderConfig& encoder_config) override {
3120 std::vector<VideoStream> streams =
3121 test::CreateVideoStreams(width, height, encoder_config);
Sergey Silkina796a7e2018-03-01 15:11:29 +01003122 streams.back().num_temporal_layers = number_of_temporal_layers_;
perkjfa10b552016-10-02 23:45:26 -07003123 return streams;
3124 }
3125
3126 const size_t number_of_temporal_layers_;
3127 };
3128
stefanff483612015-12-21 03:14:00 -08003129 void ModifyVideoConfigs(
3130 VideoSendStream::Config* send_config,
3131 std::vector<VideoReceiveStream::Config>* receive_configs,
3132 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +02003133 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Niels Möller259a4972018-04-05 15:36:51 +02003134 send_config->rtp.payload_name = "VP9";
3135 send_config->rtp.payload_type = kVp9PayloadType;
stefanff483612015-12-21 03:14:00 -08003136 ModifyVideoConfigsHook(send_config, receive_configs, encoder_config);
kthelgason29a44e32016-09-27 03:52:02 -07003137 encoder_config->encoder_specific_settings = new rtc::RefCountedObject<
Yves Gerey665174f2018-06-19 15:03:05 +02003138 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings_);
perkjfa10b552016-10-02 23:45:26 -07003139 EXPECT_EQ(1u, encoder_config->number_of_streams);
3140 encoder_config->video_stream_factory =
3141 new rtc::RefCountedObject<VideoStreamFactory>(
3142 vp9_settings_.numberOfTemporalLayers);
perkj26091b12016-09-01 01:17:40 -07003143 encoder_config_ = encoder_config->Copy();
philipel0f9af012015-09-01 07:01:51 -07003144 }
3145
perkjfa10b552016-10-02 23:45:26 -07003146 void ModifyVideoCaptureStartResolution(int* width,
3147 int* height,
3148 int* frame_rate) override {
3149 expected_width_ = *width;
3150 expected_height_ = *height;
3151 }
3152
philipel0f9af012015-09-01 07:01:51 -07003153 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01003154 EXPECT_TRUE(Wait()) << "Test timed out waiting for VP9 packet, num frames "
3155 << frames_sent_;
philipel0f9af012015-09-01 07:01:51 -07003156 }
3157
3158 Action OnSendRtp(const uint8_t* packet, size_t length) override {
3159 RTPHeader header;
3160 EXPECT_TRUE(parser_->Parse(packet, length, &header));
3161
Åsa Perssonff24c042015-12-04 10:58:08 +01003162 EXPECT_EQ(kVp9PayloadType, header.payloadType);
3163 const uint8_t* payload = packet + header.headerLength;
3164 size_t payload_length = length - header.headerLength - header.paddingLength;
philipel0f9af012015-09-01 07:01:51 -07003165
Åsa Perssonff24c042015-12-04 10:58:08 +01003166 bool new_packet = packets_sent_ == 0 ||
3167 IsNewerSequenceNumber(header.sequenceNumber,
3168 last_header_.sequenceNumber);
3169 if (payload_length > 0 && new_packet) {
3170 RtpDepacketizer::ParsedPayload parsed;
3171 RtpDepacketizerVp9 depacketizer;
3172 EXPECT_TRUE(depacketizer.Parse(&parsed, payload, payload_length));
philipelcb96ad82018-07-02 14:41:58 +02003173 EXPECT_EQ(VideoCodecType::kVideoCodecVP9, parsed.video_header().codec);
Åsa Perssonff24c042015-12-04 10:58:08 +01003174 // Verify common fields for all configurations.
philipelcb96ad82018-07-02 14:41:58 +02003175 VerifyCommonHeader(parsed.video_header().codecHeader.VP9);
3176 CompareConsecutiveFrames(header, parsed.video_header());
Åsa Perssonff24c042015-12-04 10:58:08 +01003177 // Verify configuration specific settings.
philipelcb96ad82018-07-02 14:41:58 +02003178 InspectHeader(parsed.video_header().codecHeader.VP9);
philipel0f9af012015-09-01 07:01:51 -07003179
Åsa Perssonff24c042015-12-04 10:58:08 +01003180 ++packets_sent_;
3181 if (header.markerBit) {
3182 ++frames_sent_;
philipel0f9af012015-09-01 07:01:51 -07003183 }
Åsa Perssonff24c042015-12-04 10:58:08 +01003184 last_header_ = header;
philipelcb96ad82018-07-02 14:41:58 +02003185 last_vp9_ = parsed.video_header().codecHeader.VP9;
philipel0f9af012015-09-01 07:01:51 -07003186 }
philipel0f9af012015-09-01 07:01:51 -07003187 return SEND_PACKET;
3188 }
3189
philipel7fabd462015-09-03 04:42:32 -07003190 protected:
Åsa Perssonff24c042015-12-04 10:58:08 +01003191 bool ContinuousPictureId(const RTPVideoHeaderVP9& vp9) const {
3192 if (last_vp9_.picture_id > vp9.picture_id) {
3193 return vp9.picture_id == 0; // Wrap.
3194 } else {
3195 return vp9.picture_id == last_vp9_.picture_id + 1;
3196 }
3197 }
3198
3199 void VerifySpatialIdxWithinFrame(const RTPVideoHeaderVP9& vp9) const {
Åsa Perssonff24c042015-12-04 10:58:08 +01003200 bool new_layer = vp9.spatial_idx != last_vp9_.spatial_idx;
3201 EXPECT_EQ(new_layer, vp9.beginning_of_frame);
3202 EXPECT_EQ(new_layer, last_vp9_.end_of_frame);
3203 EXPECT_EQ(new_layer ? last_vp9_.spatial_idx + 1 : last_vp9_.spatial_idx,
3204 vp9.spatial_idx);
3205 }
3206
3207 void VerifyFixedTemporalLayerStructure(const RTPVideoHeaderVP9& vp9,
3208 uint8_t num_layers) const {
3209 switch (num_layers) {
3210 case 0:
3211 VerifyTemporalLayerStructure0(vp9);
3212 break;
3213 case 1:
3214 VerifyTemporalLayerStructure1(vp9);
3215 break;
3216 case 2:
3217 VerifyTemporalLayerStructure2(vp9);
3218 break;
3219 case 3:
3220 VerifyTemporalLayerStructure3(vp9);
3221 break;
3222 default:
3223 RTC_NOTREACHED();
3224 }
3225 }
3226
3227 void VerifyTemporalLayerStructure0(const RTPVideoHeaderVP9& vp9) const {
3228 EXPECT_EQ(kNoTl0PicIdx, vp9.tl0_pic_idx);
3229 EXPECT_EQ(kNoTemporalIdx, vp9.temporal_idx); // no tid
3230 EXPECT_FALSE(vp9.temporal_up_switch);
3231 }
3232
3233 void VerifyTemporalLayerStructure1(const RTPVideoHeaderVP9& vp9) const {
3234 EXPECT_NE(kNoTl0PicIdx, vp9.tl0_pic_idx);
3235 EXPECT_EQ(0, vp9.temporal_idx); // 0,0,0,...
3236 EXPECT_FALSE(vp9.temporal_up_switch);
3237 }
3238
3239 void VerifyTemporalLayerStructure2(const RTPVideoHeaderVP9& vp9) const {
3240 EXPECT_NE(kNoTl0PicIdx, vp9.tl0_pic_idx);
3241 EXPECT_GE(vp9.temporal_idx, 0); // 0,1,0,1,... (tid reset on I-frames).
3242 EXPECT_LE(vp9.temporal_idx, 1);
3243 EXPECT_EQ(vp9.temporal_idx > 0, vp9.temporal_up_switch);
3244 if (IsNewPictureId(vp9)) {
3245 uint8_t expected_tid =
3246 (!vp9.inter_pic_predicted || last_vp9_.temporal_idx == 1) ? 0 : 1;
3247 EXPECT_EQ(expected_tid, vp9.temporal_idx);
3248 }
3249 }
3250
3251 void VerifyTemporalLayerStructure3(const RTPVideoHeaderVP9& vp9) const {
3252 EXPECT_NE(kNoTl0PicIdx, vp9.tl0_pic_idx);
3253 EXPECT_GE(vp9.temporal_idx, 0); // 0,2,1,2,... (tid reset on I-frames).
3254 EXPECT_LE(vp9.temporal_idx, 2);
3255 if (IsNewPictureId(vp9) && vp9.inter_pic_predicted) {
3256 EXPECT_NE(vp9.temporal_idx, last_vp9_.temporal_idx);
3257 switch (vp9.temporal_idx) {
3258 case 0:
3259 EXPECT_EQ(2, last_vp9_.temporal_idx);
3260 EXPECT_FALSE(vp9.temporal_up_switch);
3261 break;
3262 case 1:
3263 EXPECT_EQ(2, last_vp9_.temporal_idx);
3264 EXPECT_TRUE(vp9.temporal_up_switch);
3265 break;
3266 case 2:
Sergey Silkin377ef242018-05-07 09:17:12 +02003267 EXPECT_LT(last_vp9_.temporal_idx, 2);
3268 EXPECT_TRUE(vp9.temporal_up_switch);
Åsa Perssonff24c042015-12-04 10:58:08 +01003269 break;
3270 }
3271 }
3272 }
3273
3274 void VerifyTl0Idx(const RTPVideoHeaderVP9& vp9) const {
3275 if (vp9.tl0_pic_idx == kNoTl0PicIdx)
3276 return;
3277
3278 uint8_t expected_tl0_idx = last_vp9_.tl0_pic_idx;
3279 if (vp9.temporal_idx == 0)
3280 ++expected_tl0_idx;
3281 EXPECT_EQ(expected_tl0_idx, vp9.tl0_pic_idx);
3282 }
3283
3284 bool IsNewPictureId(const RTPVideoHeaderVP9& vp9) const {
3285 return frames_sent_ > 0 && (vp9.picture_id != last_vp9_.picture_id);
3286 }
3287
3288 // Flexible mode (F=1): Non-flexible mode (F=0):
3289 //
3290 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3291 // |I|P|L|F|B|E|V|-| |I|P|L|F|B|E|V|-|
3292 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3293 // I: |M| PICTURE ID | I: |M| PICTURE ID |
3294 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3295 // M: | EXTENDED PID | M: | EXTENDED PID |
3296 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3297 // L: | T |U| S |D| L: | T |U| S |D|
3298 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3299 // P,F: | P_DIFF |X|N| | TL0PICIDX |
3300 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3301 // X: |EXTENDED P_DIFF| V: | SS .. |
3302 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3303 // V: | SS .. |
3304 // +-+-+-+-+-+-+-+-+
3305 void VerifyCommonHeader(const RTPVideoHeaderVP9& vp9) const {
3306 EXPECT_EQ(kMaxTwoBytePictureId, vp9.max_picture_id); // M:1
3307 EXPECT_NE(kNoPictureId, vp9.picture_id); // I:1
3308 EXPECT_EQ(vp9_settings_.flexibleMode, vp9.flexible_mode); // F
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003309
3310 if (vp9_settings_.numberOfSpatialLayers > 1) {
3311 EXPECT_LT(vp9.spatial_idx, vp9_settings_.numberOfSpatialLayers);
3312 } else if (vp9_settings_.numberOfTemporalLayers > 1) {
3313 EXPECT_EQ(vp9.spatial_idx, 0);
3314 } else {
3315 EXPECT_EQ(vp9.spatial_idx, kNoSpatialIdx);
3316 }
3317
3318 if (vp9_settings_.numberOfTemporalLayers > 1) {
3319 EXPECT_LT(vp9.temporal_idx, vp9_settings_.numberOfTemporalLayers);
3320 } else if (vp9_settings_.numberOfSpatialLayers > 1) {
3321 EXPECT_EQ(vp9.temporal_idx, 0);
3322 } else {
3323 EXPECT_EQ(vp9.temporal_idx, kNoTemporalIdx);
3324 }
3325
Åsa Perssonff24c042015-12-04 10:58:08 +01003326 if (vp9.ss_data_available) // V
3327 VerifySsData(vp9);
3328
3329 if (frames_sent_ == 0)
3330 EXPECT_FALSE(vp9.inter_pic_predicted); // P
3331
3332 if (!vp9.inter_pic_predicted) {
3333 EXPECT_TRUE(vp9.temporal_idx == 0 || vp9.temporal_idx == kNoTemporalIdx);
3334 EXPECT_FALSE(vp9.temporal_up_switch);
3335 }
3336 }
3337
3338 // Scalability structure (SS).
3339 //
3340 // +-+-+-+-+-+-+-+-+
3341 // V: | N_S |Y|G|-|-|-|
3342 // +-+-+-+-+-+-+-+-+
3343 // Y: | WIDTH | N_S + 1 times
3344 // +-+-+-+-+-+-+-+-+
3345 // | HEIGHT |
3346 // +-+-+-+-+-+-+-+-+
3347 // G: | N_G |
3348 // +-+-+-+-+-+-+-+-+
3349 // N_G: | T |U| R |-|-| N_G times
3350 // +-+-+-+-+-+-+-+-+
3351 // | P_DIFF | R times
3352 // +-+-+-+-+-+-+-+-+
3353 void VerifySsData(const RTPVideoHeaderVP9& vp9) const {
3354 EXPECT_TRUE(vp9.ss_data_available); // V
3355 EXPECT_EQ(vp9_settings_.numberOfSpatialLayers, // N_S + 1
3356 vp9.num_spatial_layers);
3357 EXPECT_TRUE(vp9.spatial_layer_resolution_present); // Y:1
perkjfa10b552016-10-02 23:45:26 -07003358 int expected_width = expected_width_;
3359 int expected_height = expected_height_;
Peter Boström02083222016-06-14 12:52:54 +02003360 for (int i = static_cast<int>(vp9.num_spatial_layers) - 1; i >= 0; --i) {
Åsa Perssonff24c042015-12-04 10:58:08 +01003361 EXPECT_EQ(expected_width, vp9.width[i]); // WIDTH
3362 EXPECT_EQ(expected_height, vp9.height[i]); // HEIGHT
3363 expected_width /= 2;
3364 expected_height /= 2;
3365 }
3366 }
3367
3368 void CompareConsecutiveFrames(const RTPHeader& header,
3369 const RTPVideoHeader& video) const {
3370 const RTPVideoHeaderVP9& vp9 = video.codecHeader.VP9;
3371
3372 bool new_frame = packets_sent_ == 0 ||
3373 IsNewerTimestamp(header.timestamp, last_header_.timestamp);
johan0d1b2b62017-01-10 04:21:35 -08003374 EXPECT_EQ(new_frame, video.is_first_packet_in_frame);
Åsa Perssonff24c042015-12-04 10:58:08 +01003375 if (!new_frame) {
3376 EXPECT_FALSE(last_header_.markerBit);
3377 EXPECT_EQ(last_header_.timestamp, header.timestamp);
3378 EXPECT_EQ(last_vp9_.picture_id, vp9.picture_id);
3379 EXPECT_EQ(last_vp9_.temporal_idx, vp9.temporal_idx);
3380 EXPECT_EQ(last_vp9_.tl0_pic_idx, vp9.tl0_pic_idx);
3381 VerifySpatialIdxWithinFrame(vp9);
3382 return;
3383 }
3384 // New frame.
3385 EXPECT_TRUE(vp9.beginning_of_frame);
3386
3387 // Compare with last packet in previous frame.
3388 if (frames_sent_ == 0)
3389 return;
3390 EXPECT_TRUE(last_vp9_.end_of_frame);
3391 EXPECT_TRUE(last_header_.markerBit);
3392 EXPECT_TRUE(ContinuousPictureId(vp9));
3393 VerifyTl0Idx(vp9);
3394 }
3395
Niels Möller4db138e2018-04-19 09:04:13 +02003396 test::FunctionVideoEncoderFactory encoder_factory_;
philipel0f9af012015-09-01 07:01:51 -07003397 VideoCodecVP9 vp9_settings_;
Åsa Perssonff24c042015-12-04 10:58:08 +01003398 webrtc::VideoEncoderConfig encoder_config_;
3399 RTPHeader last_header_;
3400 RTPVideoHeaderVP9 last_vp9_;
3401 size_t packets_sent_;
3402 size_t frames_sent_;
perkjfa10b552016-10-02 23:45:26 -07003403 int expected_width_;
3404 int expected_height_;
philipel0f9af012015-09-01 07:01:51 -07003405};
3406
Åsa Perssonff24c042015-12-04 10:58:08 +01003407TEST_F(VideoSendStreamTest, Vp9NonFlexMode_1Tl1SLayers) {
3408 const uint8_t kNumTemporalLayers = 1;
3409 const uint8_t kNumSpatialLayers = 1;
3410 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3411}
3412
3413TEST_F(VideoSendStreamTest, Vp9NonFlexMode_2Tl1SLayers) {
3414 const uint8_t kNumTemporalLayers = 2;
3415 const uint8_t kNumSpatialLayers = 1;
3416 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3417}
3418
3419TEST_F(VideoSendStreamTest, Vp9NonFlexMode_3Tl1SLayers) {
3420 const uint8_t kNumTemporalLayers = 3;
3421 const uint8_t kNumSpatialLayers = 1;
3422 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3423}
3424
3425TEST_F(VideoSendStreamTest, Vp9NonFlexMode_1Tl2SLayers) {
3426 const uint8_t kNumTemporalLayers = 1;
3427 const uint8_t kNumSpatialLayers = 2;
3428 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3429}
3430
3431TEST_F(VideoSendStreamTest, Vp9NonFlexMode_2Tl2SLayers) {
3432 const uint8_t kNumTemporalLayers = 2;
3433 const uint8_t kNumSpatialLayers = 2;
3434 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3435}
3436
3437TEST_F(VideoSendStreamTest, Vp9NonFlexMode_3Tl2SLayers) {
3438 const uint8_t kNumTemporalLayers = 3;
3439 const uint8_t kNumSpatialLayers = 2;
3440 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3441}
3442
3443void VideoSendStreamTest::TestVp9NonFlexMode(uint8_t num_temporal_layers,
3444 uint8_t num_spatial_layers) {
3445 static const size_t kNumFramesToSend = 100;
3446 // Set to < kNumFramesToSend and coprime to length of temporal layer
3447 // structures to verify temporal id reset on key frame.
3448 static const int kKeyFrameInterval = 31;
Sergey Silkin86684962018-03-28 19:32:37 +02003449
3450 static const int kWidth = kMinVp9SpatialLayerWidth;
3451 static const int kHeight = kMinVp9SpatialLayerHeight;
3452 static const float kGoodBitsPerPixel = 0.1f;
Åsa Perssonff24c042015-12-04 10:58:08 +01003453 class NonFlexibleMode : public Vp9HeaderObserver {
3454 public:
3455 NonFlexibleMode(uint8_t num_temporal_layers, uint8_t num_spatial_layers)
3456 : num_temporal_layers_(num_temporal_layers),
3457 num_spatial_layers_(num_spatial_layers),
3458 l_field_(num_temporal_layers > 1 || num_spatial_layers > 1) {}
Sergey Silkin86684962018-03-28 19:32:37 +02003459
stefanff483612015-12-21 03:14:00 -08003460 void ModifyVideoConfigsHook(
philipel0f9af012015-09-01 07:01:51 -07003461 VideoSendStream::Config* send_config,
3462 std::vector<VideoReceiveStream::Config>* receive_configs,
3463 VideoEncoderConfig* encoder_config) override {
Niels Möller04dd1762018-03-23 16:05:22 +01003464 encoder_config->codec_type = kVideoCodecVP9;
Sergey Silkin86684962018-03-28 19:32:37 +02003465 int bitrate_bps = 0;
3466 for (int sl_idx = 0; sl_idx < num_spatial_layers_; ++sl_idx) {
3467 const int width = kWidth << sl_idx;
3468 const int height = kHeight << sl_idx;
3469 const float bpp = kGoodBitsPerPixel / (1 << sl_idx);
3470 bitrate_bps += static_cast<int>(width * height * bpp * 30);
3471 }
3472 encoder_config->max_bitrate_bps = bitrate_bps * 2;
3473
Åsa Perssonff24c042015-12-04 10:58:08 +01003474 vp9_settings_.flexibleMode = false;
3475 vp9_settings_.frameDroppingOn = false;
3476 vp9_settings_.keyFrameInterval = kKeyFrameInterval;
3477 vp9_settings_.numberOfTemporalLayers = num_temporal_layers_;
3478 vp9_settings_.numberOfSpatialLayers = num_spatial_layers_;
philipel0f9af012015-09-01 07:01:51 -07003479 }
3480
Sergey Silkin86684962018-03-28 19:32:37 +02003481 void ModifyVideoCaptureStartResolution(int* width,
3482 int* height,
3483 int* frame_rate) override {
3484 expected_width_ = kWidth << (num_spatial_layers_ - 1);
3485 expected_height_ = kHeight << (num_spatial_layers_ - 1);
3486 *width = expected_width_;
3487 *height = expected_height_;
3488 }
3489
Åsa Perssonff24c042015-12-04 10:58:08 +01003490 void InspectHeader(const RTPVideoHeaderVP9& vp9) override {
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003491 bool ss_data_expected =
3492 !vp9.inter_pic_predicted && vp9.beginning_of_frame &&
3493 (vp9.spatial_idx == 0 || vp9.spatial_idx == kNoSpatialIdx);
Åsa Perssonff24c042015-12-04 10:58:08 +01003494 EXPECT_EQ(ss_data_expected, vp9.ss_data_available);
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003495 if (num_spatial_layers_ > 1) {
3496 EXPECT_EQ(vp9.spatial_idx > 0, vp9.inter_layer_predicted);
3497 } else {
3498 EXPECT_FALSE(vp9.inter_layer_predicted);
3499 }
3500
asapersson38bb8ad2015-12-14 01:41:19 -08003501 EXPECT_EQ(!vp9.inter_pic_predicted,
3502 frames_sent_ % kKeyFrameInterval == 0);
Åsa Perssonff24c042015-12-04 10:58:08 +01003503
3504 if (IsNewPictureId(vp9)) {
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003505 if (num_temporal_layers_ == 1 && num_spatial_layers_ == 1) {
3506 EXPECT_EQ(kNoSpatialIdx, vp9.spatial_idx);
3507 } else {
3508 EXPECT_EQ(0, vp9.spatial_idx);
3509 }
3510 if (num_spatial_layers_ > 1)
3511 EXPECT_EQ(num_spatial_layers_ - 1, last_vp9_.spatial_idx);
Åsa Perssonff24c042015-12-04 10:58:08 +01003512 }
3513
3514 VerifyFixedTemporalLayerStructure(vp9,
3515 l_field_ ? num_temporal_layers_ : 0);
3516
3517 if (frames_sent_ > kNumFramesToSend)
Peter Boström5811a392015-12-10 13:02:50 +01003518 observation_complete_.Set();
philipel0f9af012015-09-01 07:01:51 -07003519 }
Åsa Perssonff24c042015-12-04 10:58:08 +01003520 const uint8_t num_temporal_layers_;
3521 const uint8_t num_spatial_layers_;
3522 const bool l_field_;
3523 } test(num_temporal_layers, num_spatial_layers);
philipelcfc319b2015-11-10 07:17:23 -08003524
stefane74eef12016-01-08 06:47:13 -08003525 RunBaseTest(&test);
philipelcfc319b2015-11-10 07:17:23 -08003526}
3527
asaperssond9f641e2016-01-21 01:11:35 -08003528TEST_F(VideoSendStreamTest, Vp9NonFlexModeSmallResolution) {
3529 static const size_t kNumFramesToSend = 50;
3530 static const int kWidth = 4;
3531 static const int kHeight = 4;
3532 class NonFlexibleModeResolution : public Vp9HeaderObserver {
3533 void ModifyVideoConfigsHook(
3534 VideoSendStream::Config* send_config,
3535 std::vector<VideoReceiveStream::Config>* receive_configs,
3536 VideoEncoderConfig* encoder_config) override {
Niels Möller259a4972018-04-05 15:36:51 +02003537 encoder_config->codec_type = kVideoCodecVP9;
asaperssond9f641e2016-01-21 01:11:35 -08003538 vp9_settings_.flexibleMode = false;
3539 vp9_settings_.numberOfTemporalLayers = 1;
3540 vp9_settings_.numberOfSpatialLayers = 1;
3541
perkjfa10b552016-10-02 23:45:26 -07003542 EXPECT_EQ(1u, encoder_config->number_of_streams);
asaperssond9f641e2016-01-21 01:11:35 -08003543 }
3544
3545 void InspectHeader(const RTPVideoHeaderVP9& vp9_header) override {
3546 if (frames_sent_ > kNumFramesToSend)
3547 observation_complete_.Set();
3548 }
perkjfa10b552016-10-02 23:45:26 -07003549
3550 void ModifyVideoCaptureStartResolution(int* width,
3551 int* height,
3552 int* frame_rate) override {
3553 expected_width_ = kWidth;
3554 expected_height_ = kHeight;
3555 *width = kWidth;
3556 *height = kHeight;
3557 }
asaperssond9f641e2016-01-21 01:11:35 -08003558 } test;
3559
3560 RunBaseTest(&test);
3561}
3562
kjellanderf9e2a362017-03-24 12:17:33 -07003563#if defined(WEBRTC_ANDROID)
3564// Crashes on Android; bugs.webrtc.org/7401
3565#define MAYBE_Vp9FlexModeRefCount DISABLED_Vp9FlexModeRefCount
3566#else
Sergey Silkinbe71a1e2018-05-17 16:46:43 +02003567// TODO(webrtc:9270): Support of flexible mode is temporarily disabled. Enable
3568// the test after webrtc:9270 is implemented.
3569#define MAYBE_Vp9FlexModeRefCount DISABLED_Vp9FlexModeRefCount
3570// #define MAYBE_Vp9FlexModeRefCount Vp9FlexModeRefCount
kjellanderf9e2a362017-03-24 12:17:33 -07003571#endif
3572TEST_F(VideoSendStreamTest, MAYBE_Vp9FlexModeRefCount) {
Åsa Perssonff24c042015-12-04 10:58:08 +01003573 class FlexibleMode : public Vp9HeaderObserver {
stefanff483612015-12-21 03:14:00 -08003574 void ModifyVideoConfigsHook(
philipelcfc319b2015-11-10 07:17:23 -08003575 VideoSendStream::Config* send_config,
3576 std::vector<VideoReceiveStream::Config>* receive_configs,
3577 VideoEncoderConfig* encoder_config) override {
Niels Möller259a4972018-04-05 15:36:51 +02003578 encoder_config->codec_type = kVideoCodecVP9;
Åsa Perssonff24c042015-12-04 10:58:08 +01003579 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
philipelcfc319b2015-11-10 07:17:23 -08003580 vp9_settings_.flexibleMode = true;
Åsa Perssonff24c042015-12-04 10:58:08 +01003581 vp9_settings_.numberOfTemporalLayers = 1;
3582 vp9_settings_.numberOfSpatialLayers = 2;
philipelcfc319b2015-11-10 07:17:23 -08003583 }
3584
Åsa Perssonff24c042015-12-04 10:58:08 +01003585 void InspectHeader(const RTPVideoHeaderVP9& vp9_header) override {
3586 EXPECT_TRUE(vp9_header.flexible_mode);
3587 EXPECT_EQ(kNoTl0PicIdx, vp9_header.tl0_pic_idx);
3588 if (vp9_header.inter_pic_predicted) {
3589 EXPECT_GT(vp9_header.num_ref_pics, 0u);
Peter Boström5811a392015-12-10 13:02:50 +01003590 observation_complete_.Set();
philipelcfc319b2015-11-10 07:17:23 -08003591 }
3592 }
3593 } test;
3594
stefane74eef12016-01-08 06:47:13 -08003595 RunBaseTest(&test);
philipelcfc319b2015-11-10 07:17:23 -08003596}
Peter Boström12996152016-05-14 02:03:18 +02003597#endif // !defined(RTC_DISABLE_VP9)
philipelcfc319b2015-11-10 07:17:23 -08003598
perkj803d97f2016-11-01 11:45:46 -07003599void VideoSendStreamTest::TestRequestSourceRotateVideo(
3600 bool support_orientation_ext) {
philipel4fb651d2017-04-10 03:54:05 -07003601 CreateSenderCall(Call::Config(event_log_.get()));
perkj803d97f2016-11-01 11:45:46 -07003602
3603 test::NullTransport transport;
brandtr841de6a2016-11-15 07:10:52 -08003604 CreateSendConfig(1, 0, 0, &transport);
perkj803d97f2016-11-01 11:45:46 -07003605 video_send_config_.rtp.extensions.clear();
3606 if (support_orientation_ext) {
3607 video_send_config_.rtp.extensions.push_back(
3608 RtpExtension(RtpExtension::kVideoRotationUri, 1));
3609 }
3610
3611 CreateVideoStreams();
3612 test::FrameForwarder forwarder;
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003613 video_send_stream_->SetSource(&forwarder,
3614 DegradationPreference::MAINTAIN_FRAMERATE);
perkj803d97f2016-11-01 11:45:46 -07003615
3616 EXPECT_TRUE(forwarder.sink_wants().rotation_applied !=
3617 support_orientation_ext);
3618
3619 DestroyStreams();
3620}
3621
3622TEST_F(VideoSendStreamTest,
3623 RequestSourceRotateIfVideoOrientationExtensionNotSupported) {
3624 TestRequestSourceRotateVideo(false);
3625}
3626
3627TEST_F(VideoSendStreamTest,
3628 DoNotRequestsRotationIfVideoOrientationExtensionSupported) {
3629 TestRequestSourceRotateVideo(true);
3630}
3631
michaelta3328772016-11-29 09:25:03 -08003632// This test verifies that overhead is removed from the bandwidth estimate by
3633// testing that the maximum possible target payload rate is smaller than the
3634// maximum bandwidth estimate by the overhead rate.
michaelt273f31b2017-02-08 08:21:52 -08003635TEST_F(VideoSendStreamTest, RemoveOverheadFromBandwidth) {
michaelta3328772016-11-29 09:25:03 -08003636 test::ScopedFieldTrials override_field_trials(
3637 "WebRTC-SendSideBwe-WithOverhead/Enabled/");
3638 class RemoveOverheadFromBandwidthTest : public test::EndToEndTest,
3639 public test::FakeEncoder {
3640 public:
eladalon413ee9a2017-08-22 04:02:52 -07003641 explicit RemoveOverheadFromBandwidthTest(
3642 test::SingleThreadedTaskQueueForTesting* task_queue)
michaelta3328772016-11-29 09:25:03 -08003643 : EndToEndTest(test::CallTest::kDefaultTimeoutMs),
3644 FakeEncoder(Clock::GetRealTimeClock()),
eladalon413ee9a2017-08-22 04:02:52 -07003645 task_queue_(task_queue),
Niels Möller4db138e2018-04-19 09:04:13 +02003646 encoder_factory_(this),
michaelta3328772016-11-29 09:25:03 -08003647 call_(nullptr),
michaelt192132e2017-01-26 09:05:27 -08003648 max_bitrate_bps_(0),
3649 first_packet_sent_(false),
3650 bitrate_changed_event_(false, false) {}
michaelta3328772016-11-29 09:25:03 -08003651
Erik Språng566124a2018-04-23 12:32:22 +02003652 int32_t SetRateAllocation(const VideoBitrateAllocation& bitrate,
michaelta3328772016-11-29 09:25:03 -08003653 uint32_t frameRate) override {
3654 rtc::CritScope lock(&crit_);
michaelt192132e2017-01-26 09:05:27 -08003655 // Wait for the first sent packet so that videosendstream knows
3656 // rtp_overhead.
3657 if (first_packet_sent_) {
3658 max_bitrate_bps_ = bitrate.get_sum_bps();
3659 bitrate_changed_event_.Set();
3660 }
michaelta3328772016-11-29 09:25:03 -08003661 return FakeEncoder::SetRateAllocation(bitrate, frameRate);
3662 }
3663
3664 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
3665 call_ = sender_call;
3666 }
3667
3668 void ModifyVideoConfigs(
3669 VideoSendStream::Config* send_config,
3670 std::vector<VideoReceiveStream::Config>* receive_configs,
3671 VideoEncoderConfig* encoder_config) override {
3672 send_config->rtp.max_packet_size = 1200;
Niels Möller4db138e2018-04-19 09:04:13 +02003673 send_config->encoder_settings.encoder_factory = &encoder_factory_;
michaelta3328772016-11-29 09:25:03 -08003674 EXPECT_FALSE(send_config->rtp.extensions.empty());
3675 }
3676
michaelt192132e2017-01-26 09:05:27 -08003677 Action OnSendRtp(const uint8_t* packet, size_t length) override {
3678 rtc::CritScope lock(&crit_);
3679 first_packet_sent_ = true;
3680 return SEND_PACKET;
3681 }
3682
michaelta3328772016-11-29 09:25:03 -08003683 void PerformTest() override {
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003684 BitrateConstraints bitrate_config;
michaelt192132e2017-01-26 09:05:27 -08003685 constexpr int kStartBitrateBps = 60000;
michaelta3328772016-11-29 09:25:03 -08003686 constexpr int kMaxBitrateBps = 60000;
michaelt192132e2017-01-26 09:05:27 -08003687 constexpr int kMinBitrateBps = 10000;
michaelta3328772016-11-29 09:25:03 -08003688 bitrate_config.start_bitrate_bps = kStartBitrateBps;
3689 bitrate_config.max_bitrate_bps = kMaxBitrateBps;
michaelt192132e2017-01-26 09:05:27 -08003690 bitrate_config.min_bitrate_bps = kMinBitrateBps;
eladalon413ee9a2017-08-22 04:02:52 -07003691 task_queue_->SendTask([this, &bitrate_config]() {
Sebastian Jansson8f83b422018-02-21 13:07:13 +01003692 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
3693 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07003694 call_->OnTransportOverheadChanged(webrtc::MediaType::VIDEO, 40);
3695 });
michaelta3328772016-11-29 09:25:03 -08003696
3697 // At a bitrate of 60kbps with a packet size of 1200B video and an
michaelt192132e2017-01-26 09:05:27 -08003698 // overhead of 40B per packet video produces 2240bps overhead.
3699 // So the encoder BW should be set to 57760bps.
Niels Möller4db138e2018-04-19 09:04:13 +02003700 EXPECT_TRUE(
3701 bitrate_changed_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
michaelta3328772016-11-29 09:25:03 -08003702 {
3703 rtc::CritScope lock(&crit_);
michaelt273f31b2017-02-08 08:21:52 -08003704 EXPECT_LE(max_bitrate_bps_, 57760u);
michaelta3328772016-11-29 09:25:03 -08003705 }
3706 }
3707
3708 private:
eladalon413ee9a2017-08-22 04:02:52 -07003709 test::SingleThreadedTaskQueueForTesting* const task_queue_;
Niels Möller4db138e2018-04-19 09:04:13 +02003710 test::EncoderProxyFactory encoder_factory_;
michaelta3328772016-11-29 09:25:03 -08003711 Call* call_;
3712 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07003713 uint32_t max_bitrate_bps_ RTC_GUARDED_BY(&crit_);
3714 bool first_packet_sent_ RTC_GUARDED_BY(&crit_);
michaelt192132e2017-01-26 09:05:27 -08003715 rtc::Event bitrate_changed_event_;
eladalon413ee9a2017-08-22 04:02:52 -07003716 } test(&task_queue_);
michaelta3328772016-11-29 09:25:03 -08003717 RunBaseTest(&test);
3718}
3719
sprang168794c2017-07-06 04:38:06 -07003720TEST_F(VideoSendStreamTest, SendsKeepAlive) {
3721 const int kTimeoutMs = 50; // Really short timeout for testing.
sprang168794c2017-07-06 04:38:06 -07003722
3723 class KeepaliveObserver : public test::SendTest {
3724 public:
3725 KeepaliveObserver() : SendTest(kDefaultTimeoutMs) {}
3726
sprangdb2a9fc2017-08-09 06:42:32 -07003727 void OnRtpTransportControllerSendCreated(
3728 RtpTransportControllerSend* controller) override {
3729 RtpKeepAliveConfig config;
3730 config.timeout_interval_ms = kTimeoutMs;
3731 config.payload_type = CallTest::kDefaultKeepalivePayloadType;
3732 controller->SetKeepAliveConfig(config);
sprange5c4a812017-07-11 03:44:17 -07003733 }
3734
sprang168794c2017-07-06 04:38:06 -07003735 private:
3736 Action OnSendRtp(const uint8_t* packet, size_t length) override {
3737 RTPHeader header;
3738 EXPECT_TRUE(parser_->Parse(packet, length, &header));
3739
sprangd2702ef2017-07-10 08:41:10 -07003740 if (header.payloadType != CallTest::kDefaultKeepalivePayloadType) {
sprang168794c2017-07-06 04:38:06 -07003741 // The video stream has started. Stop it now.
3742 if (capturer_)
3743 capturer_->Stop();
3744 } else {
3745 observation_complete_.Set();
3746 }
3747
3748 return SEND_PACKET;
3749 }
3750
sprang168794c2017-07-06 04:38:06 -07003751 void PerformTest() override {
3752 EXPECT_TRUE(Wait()) << "Timed out while waiting for keep-alive packet.";
3753 }
3754
3755 void OnFrameGeneratorCapturerCreated(
3756 test::FrameGeneratorCapturer* frame_generator_capturer) override {
3757 capturer_ = frame_generator_capturer;
3758 }
3759
3760 test::FrameGeneratorCapturer* capturer_ = nullptr;
3761 } test;
3762
3763 RunBaseTest(&test);
3764}
3765
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003766class PacingFactorObserver : public test::SendTest {
3767 public:
3768 PacingFactorObserver(bool configure_send_side,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +02003769 absl::optional<float> expected_pacing_factor)
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003770 : test::SendTest(VideoSendStreamTest::kDefaultTimeoutMs),
3771 configure_send_side_(configure_send_side),
3772 expected_pacing_factor_(expected_pacing_factor) {}
Erik Språng7c8cca32017-10-24 17:05:18 +02003773
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003774 void ModifyVideoConfigs(
3775 VideoSendStream::Config* send_config,
3776 std::vector<VideoReceiveStream::Config>* receive_configs,
3777 VideoEncoderConfig* encoder_config) override {
3778 // Check if send-side bwe extension is already present, and remove it if
3779 // it is not desired.
3780 bool has_send_side = false;
3781 for (auto it = send_config->rtp.extensions.begin();
3782 it != send_config->rtp.extensions.end(); ++it) {
3783 if (it->uri == RtpExtension::kTransportSequenceNumberUri) {
3784 if (configure_send_side_) {
3785 has_send_side = true;
3786 } else {
3787 send_config->rtp.extensions.erase(it);
Erik Språng7c8cca32017-10-24 17:05:18 +02003788 }
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003789 break;
Erik Språng7c8cca32017-10-24 17:05:18 +02003790 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003791 }
3792
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003793 if (configure_send_side_ && !has_send_side) {
3794 // Want send side, not present by default, so add it.
3795 send_config->rtp.extensions.emplace_back(
3796 RtpExtension::kTransportSequenceNumberUri,
3797 RtpExtension::kTransportSequenceNumberDefaultId);
Erik Språng7c8cca32017-10-24 17:05:18 +02003798 }
3799
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003800 // ALR only enabled for screenshare.
3801 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
3802 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003803
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003804 void OnVideoStreamsCreated(
3805 VideoSendStream* send_stream,
3806 const std::vector<VideoReceiveStream*>& receive_streams) override {
3807 auto internal_send_peer = test::VideoSendStreamPeer(send_stream);
3808 // Video streams created, check that pacing factor is correctly configured.
3809 EXPECT_EQ(expected_pacing_factor_,
3810 internal_send_peer.GetPacingFactorOverride());
3811 observation_complete_.Set();
3812 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003813
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003814 void PerformTest() override {
3815 EXPECT_TRUE(Wait()) << "Timed out while waiting for stream creation.";
3816 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003817
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003818 private:
3819 const bool configure_send_side_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +02003820 const absl::optional<float> expected_pacing_factor_;
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003821};
3822
3823std::string GetAlrProbingExperimentString() {
3824 return std::string(
3825 AlrExperimentSettings::kScreenshareProbingBweExperimentName) +
3826 "/1.0,2875,80,40,-60,3/";
3827}
3828const float kAlrProbingExperimentPaceMultiplier = 1.0f;
3829
3830TEST_F(VideoSendStreamTest, AlrConfiguredWhenSendSideOn) {
3831 test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
Erik Språng7c8cca32017-10-24 17:05:18 +02003832 // Send-side bwe on, use pacing factor from |kAlrProbingExperiment| above.
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003833 PacingFactorObserver test_with_send_side(true,
3834 kAlrProbingExperimentPaceMultiplier);
Erik Språng7c8cca32017-10-24 17:05:18 +02003835 RunBaseTest(&test_with_send_side);
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003836}
Erik Språng7c8cca32017-10-24 17:05:18 +02003837
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003838TEST_F(VideoSendStreamTest, AlrNotConfiguredWhenSendSideOff) {
3839 test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
3840 // Send-side bwe off, use configuration should not be overridden.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +02003841 PacingFactorObserver test_without_send_side(false, absl::nullopt);
Erik Språng7c8cca32017-10-24 17:05:18 +02003842 RunBaseTest(&test_without_send_side);
3843}
3844
Ilya Nikolaevskiy4425b052018-03-15 11:42:00 +01003845// Test class takes as argument a function pointer to reset the send
3846// stream and call OnVideoStreamsCreated. This is necessary since you cannot
3847// change the content type of a VideoSendStream, you need to recreate it.
3848// Stopping and recreating the stream can only be done on the main thread and in
3849// the context of VideoSendStreamTest (not BaseTest). The test switches from
3850// realtime to screenshare and back.
3851template <typename T>
3852class ContentSwitchTest : public test::SendTest {
3853 public:
3854 enum class StreamState {
3855 kBeforeSwitch = 0,
3856 kInScreenshare = 1,
3857 kAfterSwitchBack = 2,
3858 };
3859 static const uint32_t kMinPacketsToSend = 50;
3860
3861 explicit ContentSwitchTest(T* stream_reset_fun)
3862 : SendTest(test::CallTest::kDefaultTimeoutMs),
3863 content_switch_event_(false, false),
3864 call_(nullptr),
3865 state_(StreamState::kBeforeSwitch),
3866 send_stream_(nullptr),
3867 send_stream_config_(nullptr),
3868 packets_sent_(0),
3869 stream_resetter_(stream_reset_fun) {
3870 RTC_DCHECK(stream_resetter_);
3871 }
3872
3873 void OnVideoStreamsCreated(
3874 VideoSendStream* send_stream,
3875 const std::vector<VideoReceiveStream*>& receive_streams) override {
3876 rtc::CritScope lock(&crit_);
3877 send_stream_ = send_stream;
3878 }
3879
3880 void ModifyVideoConfigs(
3881 VideoSendStream::Config* send_config,
3882 std::vector<VideoReceiveStream::Config>* receive_configs,
3883 VideoEncoderConfig* encoder_config) override {
3884 RTC_DCHECK_EQ(1, encoder_config->number_of_streams);
3885 encoder_config->min_transmit_bitrate_bps = 0;
3886 encoder_config->content_type =
3887 VideoEncoderConfig::ContentType::kRealtimeVideo;
3888 send_stream_config_ = send_config->Copy();
3889 encoder_config_ = encoder_config->Copy();
3890 }
3891
3892 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
3893 call_ = sender_call;
3894 }
3895
3896 Action OnSendRtp(const uint8_t* packet, size_t length) override {
3897 rtc::CritScope lock(&crit_);
3898
3899 auto internal_send_peer = test::VideoSendStreamPeer(send_stream_);
3900 float pacing_factor =
3901 internal_send_peer.GetPacingFactorOverride().value_or(0.0f);
3902 float expected_pacing_factor = PacedSender::kDefaultPaceMultiplier;
3903 if (send_stream_->GetStats().content_type ==
3904 webrtc::VideoContentType::SCREENSHARE) {
3905 expected_pacing_factor = 1.0f; // Currently used pacing factor in ALR.
3906 }
3907
3908 EXPECT_NEAR(expected_pacing_factor, pacing_factor, 1e-6);
3909
3910 // Wait until at least kMinPacketsToSend packets to be sent, so that
3911 // some frames would be encoded.
3912 if (++packets_sent_ < kMinPacketsToSend)
3913 return SEND_PACKET;
3914
3915 if (state_ != StreamState::kAfterSwitchBack) {
3916 // We've sent kMinPacketsToSend packets, switch the content type and move
3917 // move to the next state.
3918 // Note that we need to recreate the stream if changing content type.
3919 packets_sent_ = 0;
3920 if (encoder_config_.content_type ==
3921 VideoEncoderConfig::ContentType::kRealtimeVideo) {
3922 encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
3923 } else {
3924 encoder_config_.content_type =
3925 VideoEncoderConfig::ContentType::kRealtimeVideo;
3926 }
3927 switch (state_) {
3928 case StreamState::kBeforeSwitch:
3929 state_ = StreamState::kInScreenshare;
3930 break;
3931 case StreamState::kInScreenshare:
3932 state_ = StreamState::kAfterSwitchBack;
3933 break;
3934 case StreamState::kAfterSwitchBack:
3935 RTC_NOTREACHED();
3936 break;
3937 }
3938 content_switch_event_.Set();
3939 return SEND_PACKET;
3940 }
3941
3942 observation_complete_.Set();
3943 return SEND_PACKET;
3944 }
3945
3946 void PerformTest() override {
3947 while (GetStreamState() != StreamState::kAfterSwitchBack) {
3948 ASSERT_TRUE(
3949 content_switch_event_.Wait(test::CallTest::kDefaultTimeoutMs));
3950 (*stream_resetter_)(send_stream_config_, encoder_config_, this);
3951 }
3952
3953 ASSERT_TRUE(Wait())
3954 << "Timed out waiting for a frame sent after switch back";
3955 }
3956
3957 private:
3958 StreamState GetStreamState() {
3959 rtc::CritScope lock(&crit_);
3960 return state_;
3961 }
3962
3963 rtc::CriticalSection crit_;
3964 rtc::Event content_switch_event_;
3965 Call* call_;
3966 StreamState state_ RTC_GUARDED_BY(crit_);
3967 VideoSendStream* send_stream_ RTC_GUARDED_BY(crit_);
3968 VideoSendStream::Config send_stream_config_;
3969 VideoEncoderConfig encoder_config_;
3970 uint32_t packets_sent_ RTC_GUARDED_BY(crit_);
3971 T* stream_resetter_;
3972};
3973
3974TEST_F(VideoSendStreamTest, SwitchesToScreenshareAndBack) {
3975 auto reset_fun = [this](const VideoSendStream::Config& send_stream_config,
3976 const VideoEncoderConfig& encoder_config,
3977 test::BaseTest* test) {
3978 task_queue_.SendTask([this, &send_stream_config, &encoder_config, &test]() {
3979 Stop();
3980 sender_call_->DestroyVideoSendStream(video_send_stream_);
3981 video_send_config_ = send_stream_config.Copy();
3982 video_encoder_config_ = encoder_config.Copy();
3983 video_send_stream_ = sender_call_->CreateVideoSendStream(
3984 video_send_config_.Copy(), video_encoder_config_.Copy());
Taylor Brandstetter49fcc102018-05-16 14:20:41 -07003985 video_send_stream_->SetSource(frame_generator_capturer_.get(),
3986 DegradationPreference::MAINTAIN_RESOLUTION);
Ilya Nikolaevskiy4425b052018-03-15 11:42:00 +01003987 test->OnVideoStreamsCreated(video_send_stream_, video_receive_streams_);
3988 Start();
3989 });
3990 };
3991 ContentSwitchTest<decltype(reset_fun)> test(&reset_fun);
3992 RunBaseTest(&test);
3993}
3994
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +00003995} // namespace webrtc