blob: cabc0a4ee0c7bcd32ed01d3dcf683f869bd9fb48 [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"
36#include "test/fake_texture_frame.h"
37#include "test/field_trial.h"
38#include "test/frame_generator.h"
39#include "test/frame_generator_capturer.h"
40#include "test/frame_utils.h"
Danil Chapovalov45d725d2018-02-19 19:09:53 +010041#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/gtest.h"
43#include "test/null_transport.h"
44#include "test/rtcp_packet_parser.h"
45#include "test/testsupport/perf_test.h"
perkjfa10b552016-10-02 23:45:26 -070046
Sebastian Janssona45c8da2018-01-16 10:55:29 +010047#include "call/video_send_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "video/send_statistics_proxy.h"
49#include "video/transport_adapter.h"
Sebastian Janssona45c8da2018-01-16 10:55:29 +010050#include "video/video_send_stream.h"
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +000051
52namespace webrtc {
Sebastian Janssona45c8da2018-01-16 10:55:29 +010053namespace test {
54class VideoSendStreamPeer {
55 public:
56 explicit VideoSendStreamPeer(webrtc::VideoSendStream* base_class_stream)
57 : internal_stream_(
58 static_cast<internal::VideoSendStream*>(base_class_stream)) {}
59 rtc::Optional<float> GetPacingFactorOverride() const {
60 return internal_stream_->GetPacingFactorOverride();
61 }
62
63 private:
64 internal::VideoSendStream const* const internal_stream_;
65};
66} // namespace test
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +000067
sprang@webrtc.org346094c2014-02-18 08:40:33 +000068enum VideoFormat { kGeneric, kVP8, };
69
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070070void ExpectEqualFramesVector(const std::vector<VideoFrame>& frames1,
71 const std::vector<VideoFrame>& frames2);
72VideoFrame CreateVideoFrame(int width, int height, uint8_t data);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +000073
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000074class VideoSendStreamTest : public test::CallTest {
pbos@webrtc.org013d9942013-08-22 09:42:17 +000075 protected:
stefan@webrtc.org69969e22013-11-15 12:32:15 +000076 void TestNackRetransmission(uint32_t retransmit_ssrc,
stefan@webrtc.orgcb254aa2014-06-12 15:12:25 +000077 uint8_t retransmit_payload_type);
sprang@webrtc.org346094c2014-02-18 08:40:33 +000078 void TestPacketFragmentationSize(VideoFormat format, bool with_fec);
Åsa Perssonff24c042015-12-04 10:58:08 +010079
80 void TestVp9NonFlexMode(uint8_t num_temporal_layers,
81 uint8_t num_spatial_layers);
perkj803d97f2016-11-01 11:45:46 -070082
83 void TestRequestSourceRotateVideo(bool support_orientation_ext);
pbos@webrtc.org013d9942013-08-22 09:42:17 +000084};
85
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +000086TEST_F(VideoSendStreamTest, CanStartStartedStream) {
eladalon413ee9a2017-08-22 04:02:52 -070087 task_queue_.SendTask([this]() {
88 CreateSenderCall(Call::Config(event_log_.get()));
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +000089
eladalon413ee9a2017-08-22 04:02:52 -070090 test::NullTransport transport;
91 CreateSendConfig(1, 0, 0, &transport);
92 CreateVideoStreams();
93 video_send_stream_->Start();
94 video_send_stream_->Start();
95 DestroyStreams();
96 DestroyCalls();
97 });
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +000098}
99
100TEST_F(VideoSendStreamTest, CanStopStoppedStream) {
eladalon413ee9a2017-08-22 04:02:52 -0700101 task_queue_.SendTask([this]() {
102 CreateSenderCall(Call::Config(event_log_.get()));
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +0000103
eladalon413ee9a2017-08-22 04:02:52 -0700104 test::NullTransport transport;
105 CreateSendConfig(1, 0, 0, &transport);
106 CreateVideoStreams();
107 video_send_stream_->Stop();
108 video_send_stream_->Stop();
109 DestroyStreams();
110 DestroyCalls();
111 });
pbos@webrtc.orgf777cf22014-01-10 18:47:32 +0000112}
113
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000114TEST_F(VideoSendStreamTest, SupportsCName) {
115 static std::string kCName = "PjQatC14dGfbVwGPUOA9IH7RlsFDbWl4AhXEiDsBizo=";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000116 class CNameObserver : public test::SendTest {
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000117 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000118 CNameObserver() : SendTest(kDefaultTimeoutMs) {}
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000119
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000120 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000121 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
danilchap3dc929e2016-11-02 08:21:59 -0700122 test::RtcpPacketParser parser;
123 EXPECT_TRUE(parser.Parse(packet, length));
124 if (parser.sdes()->num_packets() > 0) {
125 EXPECT_EQ(1u, parser.sdes()->chunks().size());
126 EXPECT_EQ(kCName, parser.sdes()->chunks()[0].cname);
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000127
danilchap3dc929e2016-11-02 08:21:59 -0700128 observation_complete_.Set();
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000129 }
130
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000131 return SEND_PACKET;
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000132 }
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000133
stefanff483612015-12-21 03:14:00 -0800134 void ModifyVideoConfigs(
135 VideoSendStream::Config* send_config,
136 std::vector<VideoReceiveStream::Config>* receive_configs,
137 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000138 send_config->rtp.c_name = kCName;
139 }
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000140
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000141 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100142 EXPECT_TRUE(Wait()) << "Timed out while waiting for RTCP with CNAME.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000143 }
144 } test;
pbos@webrtc.org013d9942013-08-22 09:42:17 +0000145
stefane74eef12016-01-08 06:47:13 -0800146 RunBaseTest(&test);
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +0000147}
148
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000149TEST_F(VideoSendStreamTest, SupportsAbsoluteSendTime) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000150 class AbsoluteSendTimeObserver : public test::SendTest {
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000151 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000152 AbsoluteSendTimeObserver() : SendTest(kDefaultTimeoutMs) {
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000153 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
Stefan Holmer4654d202015-12-08 09:10:43 +0100154 kRtpExtensionAbsoluteSendTime, test::kAbsSendTimeExtensionId));
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000155 }
156
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000157 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000158 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000159 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000160
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000161 EXPECT_FALSE(header.extension.hasTransmissionTimeOffset);
162 EXPECT_TRUE(header.extension.hasAbsoluteSendTime);
163 EXPECT_EQ(header.extension.transmissionTimeOffset, 0);
skvladc3f35152016-09-02 13:23:46 -0700164 if (header.extension.absoluteSendTime != 0) {
165 // Wait for at least one packet with a non-zero send time. The send time
166 // is a 16-bit value derived from the system clock, and it is valid
167 // for a packet to have a zero send time. To tell that from an
168 // unpopulated value we'll wait for a packet with non-zero send time.
169 observation_complete_.Set();
170 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100171 RTC_LOG(LS_WARNING)
172 << "Got a packet with zero absoluteSendTime, waiting"
173 " for another packet...";
skvladc3f35152016-09-02 13:23:46 -0700174 }
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000175
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000176 return SEND_PACKET;
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000177 }
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000178
stefanff483612015-12-21 03:14:00 -0800179 void ModifyVideoConfigs(
180 VideoSendStream::Config* send_config,
181 std::vector<VideoReceiveStream::Config>* receive_configs,
182 VideoEncoderConfig* encoder_config) override {
Erik Språng95261872015-04-10 11:58:49 +0200183 send_config->rtp.extensions.clear();
Stefan Holmer4654d202015-12-08 09:10:43 +0100184 send_config->rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700185 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000186 }
pbos@webrtc.orgdde16f12014-08-05 23:35:43 +0000187
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000188 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100189 EXPECT_TRUE(Wait()) << "Timed out while waiting for single RTP packet.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000190 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000191 } test;
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000192
stefane74eef12016-01-08 06:47:13 -0800193 RunBaseTest(&test);
pbos@webrtc.org5c678ea2013-09-11 19:00:39 +0000194}
195
pbos@webrtc.org29023282013-09-11 10:14:56 +0000196TEST_F(VideoSendStreamTest, SupportsTransmissionTimeOffset) {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000197 static const int kEncodeDelayMs = 5;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000198 class TransmissionTimeOffsetObserver : public test::SendTest {
pbos@webrtc.org29023282013-09-11 10:14:56 +0000199 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000200 TransmissionTimeOffsetObserver()
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000201 : SendTest(kDefaultTimeoutMs),
202 encoder_(Clock::GetRealTimeClock(), kEncodeDelayMs) {
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000203 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
Stefan Holmer12952972015-10-29 15:13:24 +0100204 kRtpExtensionTransmissionTimeOffset, test::kTOffsetExtensionId));
pbos@webrtc.org29023282013-09-11 10:14:56 +0000205 }
206
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000207 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000208 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org29023282013-09-11 10:14:56 +0000209 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000210 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org29023282013-09-11 10:14:56 +0000211
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000212 EXPECT_TRUE(header.extension.hasTransmissionTimeOffset);
213 EXPECT_FALSE(header.extension.hasAbsoluteSendTime);
pbos@webrtc.org29023282013-09-11 10:14:56 +0000214 EXPECT_GT(header.extension.transmissionTimeOffset, 0);
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000215 EXPECT_EQ(header.extension.absoluteSendTime, 0u);
Peter Boström5811a392015-12-10 13:02:50 +0100216 observation_complete_.Set();
pbos@webrtc.org29023282013-09-11 10:14:56 +0000217
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000218 return SEND_PACKET;
pbos@webrtc.org29023282013-09-11 10:14:56 +0000219 }
pbos@webrtc.org29023282013-09-11 10:14:56 +0000220
stefanff483612015-12-21 03:14:00 -0800221 void ModifyVideoConfigs(
222 VideoSendStream::Config* send_config,
223 std::vector<VideoReceiveStream::Config>* receive_configs,
224 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000225 send_config->encoder_settings.encoder = &encoder_;
Stefan Holmer12952972015-10-29 15:13:24 +0100226 send_config->rtp.extensions.clear();
isheriff6f8d6862016-05-26 11:24:55 -0700227 send_config->rtp.extensions.push_back(RtpExtension(
228 RtpExtension::kTimestampOffsetUri, test::kTOffsetExtensionId));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000229 }
pbos@webrtc.org29023282013-09-11 10:14:56 +0000230
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000231 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100232 EXPECT_TRUE(Wait()) << "Timed out while waiting for a single RTP packet.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000233 }
pbos@webrtc.org29023282013-09-11 10:14:56 +0000234
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000235 test::DelayedEncoder encoder_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000236 } test;
237
stefane74eef12016-01-08 06:47:13 -0800238 RunBaseTest(&test);
pbos@webrtc.org29023282013-09-11 10:14:56 +0000239}
240
sprang867fb522015-08-03 04:38:41 -0700241TEST_F(VideoSendStreamTest, SupportsTransportWideSequenceNumbers) {
danilchap42ca68a2016-10-31 03:34:40 -0700242 static const uint8_t kExtensionId = test::kTransportSequenceNumberExtensionId;
sprang867fb522015-08-03 04:38:41 -0700243 class TransportWideSequenceNumberObserver : public test::SendTest {
244 public:
245 TransportWideSequenceNumberObserver()
246 : SendTest(kDefaultTimeoutMs), encoder_(Clock::GetRealTimeClock()) {
247 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
248 kRtpExtensionTransportSequenceNumber, kExtensionId));
249 }
250
251 private:
252 Action OnSendRtp(const uint8_t* packet, size_t length) override {
253 RTPHeader header;
254 EXPECT_TRUE(parser_->Parse(packet, length, &header));
255
256 EXPECT_TRUE(header.extension.hasTransportSequenceNumber);
257 EXPECT_FALSE(header.extension.hasTransmissionTimeOffset);
258 EXPECT_FALSE(header.extension.hasAbsoluteSendTime);
259
Peter Boström5811a392015-12-10 13:02:50 +0100260 observation_complete_.Set();
sprang867fb522015-08-03 04:38:41 -0700261
262 return SEND_PACKET;
263 }
264
stefanff483612015-12-21 03:14:00 -0800265 void ModifyVideoConfigs(
266 VideoSendStream::Config* send_config,
267 std::vector<VideoReceiveStream::Config>* receive_configs,
268 VideoEncoderConfig* encoder_config) override {
sprang867fb522015-08-03 04:38:41 -0700269 send_config->encoder_settings.encoder = &encoder_;
sprang867fb522015-08-03 04:38:41 -0700270 }
271
272 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100273 EXPECT_TRUE(Wait()) << "Timed out while waiting for a single RTP packet.";
sprang867fb522015-08-03 04:38:41 -0700274 }
275
276 test::FakeEncoder encoder_;
277 } test;
278
stefane74eef12016-01-08 06:47:13 -0800279 RunBaseTest(&test);
sprang867fb522015-08-03 04:38:41 -0700280}
281
perkj803d97f2016-11-01 11:45:46 -0700282TEST_F(VideoSendStreamTest, SupportsVideoRotation) {
283 class VideoRotationObserver : public test::SendTest {
284 public:
285 VideoRotationObserver() : SendTest(kDefaultTimeoutMs) {
286 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
287 kRtpExtensionVideoRotation, test::kVideoRotationExtensionId));
288 }
289
290 Action OnSendRtp(const uint8_t* packet, size_t length) override {
291 RTPHeader header;
292 EXPECT_TRUE(parser_->Parse(packet, length, &header));
ilnik7a3006b2017-05-23 09:34:21 -0700293 // Only the last packet of the frame is required to have the extension.
294 if (!header.markerBit)
295 return SEND_PACKET;
perkj803d97f2016-11-01 11:45:46 -0700296 EXPECT_TRUE(header.extension.hasVideoRotation);
297 EXPECT_EQ(kVideoRotation_90, header.extension.videoRotation);
298 observation_complete_.Set();
299 return SEND_PACKET;
300 }
301
302 void ModifyVideoConfigs(
303 VideoSendStream::Config* send_config,
304 std::vector<VideoReceiveStream::Config>* receive_configs,
305 VideoEncoderConfig* encoder_config) override {
306 send_config->rtp.extensions.clear();
307 send_config->rtp.extensions.push_back(RtpExtension(
308 RtpExtension::kVideoRotationUri, test::kVideoRotationExtensionId));
309 }
310
311 void OnFrameGeneratorCapturerCreated(
312 test::FrameGeneratorCapturer* frame_generator_capturer) override {
313 frame_generator_capturer->SetFakeRotation(kVideoRotation_90);
314 }
315
316 void PerformTest() override {
317 EXPECT_TRUE(Wait()) << "Timed out while waiting for single RTP packet.";
318 }
319 } test;
320
321 RunBaseTest(&test);
322}
323
ilnik00d802b2017-04-11 10:34:31 -0700324TEST_F(VideoSendStreamTest, SupportsVideoContentType) {
ilnik10894992017-06-21 08:23:19 -0700325 class VideoContentTypeObserver : public test::SendTest {
ilnik00d802b2017-04-11 10:34:31 -0700326 public:
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100327 VideoContentTypeObserver()
328 : SendTest(kDefaultTimeoutMs), first_frame_sent_(false) {
ilnik00d802b2017-04-11 10:34:31 -0700329 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
330 kRtpExtensionVideoContentType, test::kVideoContentTypeExtensionId));
331 }
332
333 Action OnSendRtp(const uint8_t* packet, size_t length) override {
334 RTPHeader header;
335 EXPECT_TRUE(parser_->Parse(packet, length, &header));
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100336 // Only the last packet of the key-frame must have extension.
337 if (!header.markerBit || first_frame_sent_)
ilnik7a3006b2017-05-23 09:34:21 -0700338 return SEND_PACKET;
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100339 // First marker bit seen means that the first frame is sent.
340 first_frame_sent_ = true;
ilnik00d802b2017-04-11 10:34:31 -0700341 EXPECT_TRUE(header.extension.hasVideoContentType);
ilnik6d5b4d62017-08-30 03:32:14 -0700342 EXPECT_TRUE(videocontenttypehelpers::IsScreenshare(
343 header.extension.videoContentType));
ilnik00d802b2017-04-11 10:34:31 -0700344 observation_complete_.Set();
345 return SEND_PACKET;
346 }
347
348 void ModifyVideoConfigs(
349 VideoSendStream::Config* send_config,
350 std::vector<VideoReceiveStream::Config>* receive_configs,
351 VideoEncoderConfig* encoder_config) override {
352 send_config->rtp.extensions.clear();
353 send_config->rtp.extensions.push_back(
354 RtpExtension(RtpExtension::kVideoContentTypeUri,
355 test::kVideoContentTypeExtensionId));
356 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
357 }
358
359 void PerformTest() override {
360 EXPECT_TRUE(Wait()) << "Timed out while waiting for single RTP packet.";
361 }
Ilya Nikolaevskiyefbb9782018-03-12 10:42:08 +0100362
363 private:
364 bool first_frame_sent_;
ilnik00d802b2017-04-11 10:34:31 -0700365 } test;
366
367 RunBaseTest(&test);
368}
369
ilnik04f4d122017-06-19 07:18:55 -0700370TEST_F(VideoSendStreamTest, SupportsVideoTimingFrames) {
ilnik10894992017-06-21 08:23:19 -0700371 class VideoTimingObserver : public test::SendTest {
ilnik04f4d122017-06-19 07:18:55 -0700372 public:
ilnik10894992017-06-21 08:23:19 -0700373 VideoTimingObserver() : SendTest(kDefaultTimeoutMs) {
ilnik04f4d122017-06-19 07:18:55 -0700374 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
375 kRtpExtensionVideoTiming, test::kVideoTimingExtensionId));
376 }
377
378 Action OnSendRtp(const uint8_t* packet, size_t length) override {
379 RTPHeader header;
380 EXPECT_TRUE(parser_->Parse(packet, length, &header));
ilnik10894992017-06-21 08:23:19 -0700381 // Only the last packet of the frame must have extension.
382 if (!header.markerBit)
383 return SEND_PACKET;
384 EXPECT_TRUE(header.extension.has_video_timing);
385 observation_complete_.Set();
ilnik04f4d122017-06-19 07:18:55 -0700386 return SEND_PACKET;
387 }
388
389 void ModifyVideoConfigs(
390 VideoSendStream::Config* send_config,
391 std::vector<VideoReceiveStream::Config>* receive_configs,
392 VideoEncoderConfig* encoder_config) override {
393 send_config->rtp.extensions.clear();
394 send_config->rtp.extensions.push_back(RtpExtension(
395 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
396 }
397
398 void PerformTest() override {
399 EXPECT_TRUE(Wait()) << "Timed out while waiting for timing frames.";
400 }
401 } test;
402
403 RunBaseTest(&test);
404}
405
danilchap901b2df2017-07-28 08:56:04 -0700406class FakeReceiveStatistics : public ReceiveStatisticsProvider {
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000407 public:
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +0000408 FakeReceiveStatistics(uint32_t send_ssrc,
409 uint32_t last_sequence_number,
410 uint32_t cumulative_lost,
danilchap901b2df2017-07-28 08:56:04 -0700411 uint8_t fraction_lost) {
412 stat_.SetMediaSsrc(send_ssrc);
413 stat_.SetExtHighestSeqNum(last_sequence_number);
414 stat_.SetCumulativeLost(cumulative_lost);
415 stat_.SetFractionLost(fraction_lost);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000416 }
417
danilchap901b2df2017-07-28 08:56:04 -0700418 std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override {
419 EXPECT_GE(max_blocks, 1u);
420 return {stat_};
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000421 }
422
423 private:
danilchap901b2df2017-07-28 08:56:04 -0700424 rtcp::ReportBlock stat_;
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000425};
426
brandtre602f0a2016-10-31 03:40:49 -0700427class UlpfecObserver : public test::EndToEndTest {
Stefan Holmer4654d202015-12-08 09:10:43 +0100428 public:
brandtre602f0a2016-10-31 03:40:49 -0700429 UlpfecObserver(bool header_extensions_enabled,
brandtr65a1e772016-12-12 01:54:58 -0800430 bool use_nack,
431 bool expect_red,
432 bool expect_ulpfec,
brandtr696c9c62016-12-19 05:47:28 -0800433 const std::string& codec,
434 VideoEncoder* encoder)
brandtr20d45472017-01-02 00:34:27 -0800435 : EndToEndTest(kTimeoutMs),
brandtr696c9c62016-12-19 05:47:28 -0800436 encoder_(encoder),
Peter Boström39593972016-02-15 11:27:15 +0100437 payload_name_(codec),
438 use_nack_(use_nack),
439 expect_red_(expect_red),
brandtre602f0a2016-10-31 03:40:49 -0700440 expect_ulpfec_(expect_ulpfec),
brandtr65a1e772016-12-12 01:54:58 -0800441 sent_media_(false),
442 sent_ulpfec_(false),
brandtr696c9c62016-12-19 05:47:28 -0800443 header_extensions_enabled_(header_extensions_enabled) {}
Stefan Holmer4654d202015-12-08 09:10:43 +0100444
brandtr20d45472017-01-02 00:34:27 -0800445 // Some of the test cases are expected to time out and thus we are using
446 // a shorter timeout window than the default here.
447 static constexpr size_t kTimeoutMs = 10000;
448
Stefan Holmer4654d202015-12-08 09:10:43 +0100449 private:
450 Action OnSendRtp(const uint8_t* packet, size_t length) override {
451 RTPHeader header;
452 EXPECT_TRUE(parser_->Parse(packet, length, &header));
453
Stefan Holmer4654d202015-12-08 09:10:43 +0100454 int encapsulated_payload_type = -1;
455 if (header.payloadType == VideoSendStreamTest::kRedPayloadType) {
Peter Boström39593972016-02-15 11:27:15 +0100456 EXPECT_TRUE(expect_red_);
Stefan Holmer4654d202015-12-08 09:10:43 +0100457 encapsulated_payload_type = static_cast<int>(packet[header.headerLength]);
458 if (encapsulated_payload_type !=
Peter Boström39593972016-02-15 11:27:15 +0100459 VideoSendStreamTest::kFakeVideoSendPayloadType) {
Stefan Holmer4654d202015-12-08 09:10:43 +0100460 EXPECT_EQ(VideoSendStreamTest::kUlpfecPayloadType,
461 encapsulated_payload_type);
Peter Boström39593972016-02-15 11:27:15 +0100462 }
Stefan Holmer4654d202015-12-08 09:10:43 +0100463 } else {
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100464 EXPECT_EQ(VideoSendStreamTest::kFakeVideoSendPayloadType,
465 header.payloadType);
Peter Boström39593972016-02-15 11:27:15 +0100466 if (static_cast<size_t>(header.headerLength + header.paddingLength) <
467 length) {
468 // Not padding-only, media received outside of RED.
469 EXPECT_FALSE(expect_red_);
brandtr65a1e772016-12-12 01:54:58 -0800470 sent_media_ = true;
Peter Boström39593972016-02-15 11:27:15 +0100471 }
Stefan Holmer4654d202015-12-08 09:10:43 +0100472 }
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000473
Stefan Holmer4654d202015-12-08 09:10:43 +0100474 if (header_extensions_enabled_) {
475 EXPECT_TRUE(header.extension.hasAbsoluteSendTime);
476 uint32_t kHalf24BitsSpace = 0xFFFFFF / 2;
477 if (header.extension.absoluteSendTime <= kHalf24BitsSpace &&
478 prev_header_.extension.absoluteSendTime > kHalf24BitsSpace) {
479 // 24 bits wrap.
480 EXPECT_GT(prev_header_.extension.absoluteSendTime,
481 header.extension.absoluteSendTime);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000482 } else {
Stefan Holmer4654d202015-12-08 09:10:43 +0100483 EXPECT_GE(header.extension.absoluteSendTime,
484 prev_header_.extension.absoluteSendTime);
Stefan Holmer01b48882015-05-05 10:21:24 +0200485 }
Stefan Holmer4654d202015-12-08 09:10:43 +0100486 EXPECT_TRUE(header.extension.hasTransportSequenceNumber);
487 uint16_t seq_num_diff = header.extension.transportSequenceNumber -
488 prev_header_.extension.transportSequenceNumber;
489 EXPECT_EQ(1, seq_num_diff);
490 }
Stefan Holmer01b48882015-05-05 10:21:24 +0200491
Stefan Holmer4654d202015-12-08 09:10:43 +0100492 if (encapsulated_payload_type != -1) {
493 if (encapsulated_payload_type ==
494 VideoSendStreamTest::kUlpfecPayloadType) {
brandtre602f0a2016-10-31 03:40:49 -0700495 EXPECT_TRUE(expect_ulpfec_);
brandtr65a1e772016-12-12 01:54:58 -0800496 sent_ulpfec_ = true;
Stefan Holmer4654d202015-12-08 09:10:43 +0100497 } else {
brandtr65a1e772016-12-12 01:54:58 -0800498 sent_media_ = true;
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000499 }
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000500 }
501
brandtr20d45472017-01-02 00:34:27 -0800502 if (sent_media_ && sent_ulpfec_) {
503 observation_complete_.Set();
Peter Boström39593972016-02-15 11:27:15 +0100504 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000505
Stefan Holmer4654d202015-12-08 09:10:43 +0100506 prev_header_ = header;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000507
Stefan Holmer4654d202015-12-08 09:10:43 +0100508 return SEND_PACKET;
509 }
510
eladalon413ee9a2017-08-22 04:02:52 -0700511 test::PacketTransport* CreateSendTransport(
512 test::SingleThreadedTaskQueueForTesting* task_queue,
513 Call* sender_call) override {
Peter Boström39593972016-02-15 11:27:15 +0100514 // At low RTT (< kLowRttNackMs) -> NACK only, no FEC.
515 // Configure some network delay.
516 const int kNetworkDelayMs = 100;
517 FakeNetworkPipe::Config config;
brandtr65a1e772016-12-12 01:54:58 -0800518 config.loss_percent = 5;
Peter Boström39593972016-02-15 11:27:15 +0100519 config.queue_delay_ms = kNetworkDelayMs;
minyue20c84cc2017-04-10 16:57:57 -0700520 return new test::PacketTransport(
eladalon413ee9a2017-08-22 04:02:52 -0700521 task_queue, sender_call, this, test::PacketTransport::kSender,
minyue20c84cc2017-04-10 16:57:57 -0700522 VideoSendStreamTest::payload_type_map_, config);
Peter Boström39593972016-02-15 11:27:15 +0100523 }
524
stefanff483612015-12-21 03:14:00 -0800525 void ModifyVideoConfigs(
526 VideoSendStream::Config* send_config,
527 std::vector<VideoReceiveStream::Config>* receive_configs,
528 VideoEncoderConfig* encoder_config) override {
Peter Boström39593972016-02-15 11:27:15 +0100529 if (use_nack_) {
530 send_config->rtp.nack.rtp_history_ms =
531 (*receive_configs)[0].rtp.nack.rtp_history_ms =
532 VideoSendStreamTest::kNackRtpHistoryMs;
533 }
brandtr696c9c62016-12-19 05:47:28 -0800534 send_config->encoder_settings.encoder = encoder_;
Peter Boström39593972016-02-15 11:27:15 +0100535 send_config->encoder_settings.payload_name = payload_name_;
brandtrb5f2c3f2016-10-04 23:28:39 -0700536 send_config->rtp.ulpfec.red_payload_type =
537 VideoSendStreamTest::kRedPayloadType;
538 send_config->rtp.ulpfec.ulpfec_payload_type =
539 VideoSendStreamTest::kUlpfecPayloadType;
stefanb77c7162017-02-06 06:29:38 -0800540 EXPECT_FALSE(send_config->rtp.extensions.empty());
541 if (!header_extensions_enabled_) {
542 send_config->rtp.extensions.clear();
543 } else {
Stefan Holmer4654d202015-12-08 09:10:43 +0100544 send_config->rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700545 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Stefan Holmer4654d202015-12-08 09:10:43 +0100546 }
nisse3b3622f2017-09-26 02:49:21 -0700547 (*receive_configs)[0].rtp.red_payload_type =
brandtrb5f2c3f2016-10-04 23:28:39 -0700548 send_config->rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 02:49:21 -0700549 (*receive_configs)[0].rtp.ulpfec_payload_type =
brandtrb5f2c3f2016-10-04 23:28:39 -0700550 send_config->rtp.ulpfec.ulpfec_payload_type;
Stefan Holmer4654d202015-12-08 09:10:43 +0100551 }
552
553 void PerformTest() override {
brandtr20d45472017-01-02 00:34:27 -0800554 EXPECT_EQ(expect_ulpfec_, Wait())
555 << "Timed out waiting for ULPFEC and/or media packets.";
Stefan Holmer4654d202015-12-08 09:10:43 +0100556 }
557
brandtr696c9c62016-12-19 05:47:28 -0800558 VideoEncoder* const encoder_;
559 std::string payload_name_;
Peter Boström39593972016-02-15 11:27:15 +0100560 const bool use_nack_;
561 const bool expect_red_;
brandtre602f0a2016-10-31 03:40:49 -0700562 const bool expect_ulpfec_;
brandtr65a1e772016-12-12 01:54:58 -0800563 bool sent_media_;
564 bool sent_ulpfec_;
Stefan Holmer4654d202015-12-08 09:10:43 +0100565 bool header_extensions_enabled_;
566 RTPHeader prev_header_;
567};
568
brandtre602f0a2016-10-31 03:40:49 -0700569TEST_F(VideoSendStreamTest, SupportsUlpfecWithExtensions) {
brandtr696c9c62016-12-19 05:47:28 -0800570 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
571 UlpfecObserver test(true, false, true, true, "VP8", encoder.get());
stefane74eef12016-01-08 06:47:13 -0800572 RunBaseTest(&test);
Stefan Holmer4654d202015-12-08 09:10:43 +0100573}
574
brandtre602f0a2016-10-31 03:40:49 -0700575TEST_F(VideoSendStreamTest, SupportsUlpfecWithoutExtensions) {
brandtr696c9c62016-12-19 05:47:28 -0800576 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
577 UlpfecObserver test(false, false, true, true, "VP8", encoder.get());
Peter Boström39593972016-02-15 11:27:15 +0100578 RunBaseTest(&test);
579}
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000580
stefan60e10c72017-08-23 10:40:00 -0700581class VideoSendStreamWithoutUlpfecTest : public VideoSendStreamTest {
582 protected:
583 VideoSendStreamWithoutUlpfecTest()
584 : field_trial_("WebRTC-DisableUlpFecExperiment/Enabled/") {}
585
586 test::ScopedFieldTrials field_trial_;
587};
588
589TEST_F(VideoSendStreamWithoutUlpfecTest, NoUlpfecIfDisabledThroughFieldTrial) {
590 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
591 UlpfecObserver test(false, false, true, false, "VP8", encoder.get());
592 RunBaseTest(&test);
593}
594
Peter Boström39593972016-02-15 11:27:15 +0100595// The FEC scheme used is not efficient for H264, so we should not use RED/FEC
596// since we'll still have to re-request FEC packets, effectively wasting
597// bandwidth since the receiver has to wait for FEC retransmissions to determine
598// that the received state is actually decodable.
brandtre602f0a2016-10-31 03:40:49 -0700599TEST_F(VideoSendStreamTest, DoesNotUtilizeUlpfecForH264WithNackEnabled) {
brandtr696c9c62016-12-19 05:47:28 -0800600 std::unique_ptr<VideoEncoder> encoder(
601 new test::FakeH264Encoder(Clock::GetRealTimeClock()));
602 UlpfecObserver test(false, true, true, false, "H264", encoder.get());
Peter Boström39593972016-02-15 11:27:15 +0100603 RunBaseTest(&test);
604}
605
606// Without retransmissions FEC for H264 is fine.
brandtre6f98c72016-11-11 03:28:30 -0800607TEST_F(VideoSendStreamTest, DoesUtilizeUlpfecForH264WithoutNackEnabled) {
brandtr696c9c62016-12-19 05:47:28 -0800608 std::unique_ptr<VideoEncoder> encoder(
609 new test::FakeH264Encoder(Clock::GetRealTimeClock()));
610 UlpfecObserver test(false, false, true, true, "H264", encoder.get());
Peter Boström39593972016-02-15 11:27:15 +0100611 RunBaseTest(&test);
612}
613
danilchap9f5b6222017-03-02 06:22:21 -0800614// Disabled as flaky, see https://crbug.com/webrtc/7285 for details.
615TEST_F(VideoSendStreamTest, DISABLED_DoesUtilizeUlpfecForVp8WithNackEnabled) {
brandtr696c9c62016-12-19 05:47:28 -0800616 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
617 UlpfecObserver test(false, true, true, true, "VP8", encoder.get());
Peter Boström39593972016-02-15 11:27:15 +0100618 RunBaseTest(&test);
619}
620
Peter Boström12996152016-05-14 02:03:18 +0200621#if !defined(RTC_DISABLE_VP9)
danilchap9f5b6222017-03-02 06:22:21 -0800622// Disabled as flaky, see https://crbug.com/webrtc/7285 for details.
623TEST_F(VideoSendStreamTest, DISABLED_DoesUtilizeUlpfecForVp9WithNackEnabled) {
brandtr696c9c62016-12-19 05:47:28 -0800624 std::unique_ptr<VideoEncoder> encoder(VP9Encoder::Create());
625 UlpfecObserver test(false, true, true, true, "VP9", encoder.get());
stefane74eef12016-01-08 06:47:13 -0800626 RunBaseTest(&test);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000627}
Peter Boström12996152016-05-14 02:03:18 +0200628#endif // !defined(RTC_DISABLE_VP9)
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000629
brandtre78d2662017-01-16 05:57:16 -0800630TEST_F(VideoSendStreamTest, SupportsUlpfecWithMultithreadedH264) {
brandtr696c9c62016-12-19 05:47:28 -0800631 std::unique_ptr<VideoEncoder> encoder(
brandtre78d2662017-01-16 05:57:16 -0800632 new test::MultithreadedFakeH264Encoder(Clock::GetRealTimeClock()));
brandtr696c9c62016-12-19 05:47:28 -0800633 UlpfecObserver test(false, false, true, true, "H264", encoder.get());
634 RunBaseTest(&test);
635}
636
brandtr39f97292016-11-16 22:57:50 -0800637// TODO(brandtr): Move these FlexFEC tests when we have created
638// FlexfecSendStream.
639class FlexfecObserver : public test::EndToEndTest {
640 public:
641 FlexfecObserver(bool header_extensions_enabled,
642 bool use_nack,
brandtr696c9c62016-12-19 05:47:28 -0800643 const std::string& codec,
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100644 VideoEncoder* encoder,
645 size_t num_video_streams)
brandtr39f97292016-11-16 22:57:50 -0800646 : EndToEndTest(VideoSendStreamTest::kDefaultTimeoutMs),
brandtr696c9c62016-12-19 05:47:28 -0800647 encoder_(encoder),
brandtr39f97292016-11-16 22:57:50 -0800648 payload_name_(codec),
649 use_nack_(use_nack),
brandtr39f97292016-11-16 22:57:50 -0800650 sent_media_(false),
651 sent_flexfec_(false),
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100652 header_extensions_enabled_(header_extensions_enabled),
653 num_video_streams_(num_video_streams) {}
brandtr39f97292016-11-16 22:57:50 -0800654
655 size_t GetNumFlexfecStreams() const override { return 1; }
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100656 size_t GetNumVideoStreams() const override { return num_video_streams_; }
brandtr39f97292016-11-16 22:57:50 -0800657
658 private:
659 Action OnSendRtp(const uint8_t* packet, size_t length) override {
660 RTPHeader header;
661 EXPECT_TRUE(parser_->Parse(packet, length, &header));
662
brandtr39f97292016-11-16 22:57:50 -0800663 if (header.payloadType == VideoSendStreamTest::kFlexfecPayloadType) {
664 EXPECT_EQ(VideoSendStreamTest::kFlexfecSendSsrc, header.ssrc);
665 sent_flexfec_ = true;
666 } else {
667 EXPECT_EQ(VideoSendStreamTest::kFakeVideoSendPayloadType,
668 header.payloadType);
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100669 EXPECT_THAT(testing::make_tuple(VideoSendStreamTest::kVideoSendSsrcs,
670 num_video_streams_),
671 testing::Contains(header.ssrc));
brandtr39f97292016-11-16 22:57:50 -0800672 sent_media_ = true;
673 }
674
675 if (header_extensions_enabled_) {
676 EXPECT_TRUE(header.extension.hasAbsoluteSendTime);
677 EXPECT_TRUE(header.extension.hasTransmissionTimeOffset);
678 EXPECT_TRUE(header.extension.hasTransportSequenceNumber);
679 }
680
brandtr0c5a1542016-11-23 04:42:26 -0800681 if (sent_media_ && sent_flexfec_) {
brandtr39f97292016-11-16 22:57:50 -0800682 observation_complete_.Set();
683 }
684
685 return SEND_PACKET;
686 }
687
eladalon413ee9a2017-08-22 04:02:52 -0700688 test::PacketTransport* CreateSendTransport(
689 test::SingleThreadedTaskQueueForTesting* task_queue,
690 Call* sender_call) override {
brandtr39f97292016-11-16 22:57:50 -0800691 // At low RTT (< kLowRttNackMs) -> NACK only, no FEC.
692 // Therefore we need some network delay.
693 const int kNetworkDelayMs = 100;
694 FakeNetworkPipe::Config config;
brandtrd654a9b2016-12-05 05:38:19 -0800695 config.loss_percent = 5;
brandtr39f97292016-11-16 22:57:50 -0800696 config.queue_delay_ms = kNetworkDelayMs;
minyue20c84cc2017-04-10 16:57:57 -0700697 return new test::PacketTransport(
eladalon413ee9a2017-08-22 04:02:52 -0700698 task_queue, sender_call, this, test::PacketTransport::kSender,
minyue20c84cc2017-04-10 16:57:57 -0700699 VideoSendStreamTest::payload_type_map_, config);
brandtr39f97292016-11-16 22:57:50 -0800700 }
701
702 void ModifyVideoConfigs(
703 VideoSendStream::Config* send_config,
704 std::vector<VideoReceiveStream::Config>* receive_configs,
705 VideoEncoderConfig* encoder_config) override {
brandtr39f97292016-11-16 22:57:50 -0800706 if (use_nack_) {
707 send_config->rtp.nack.rtp_history_ms =
708 (*receive_configs)[0].rtp.nack.rtp_history_ms =
709 VideoSendStreamTest::kNackRtpHistoryMs;
710 }
brandtr696c9c62016-12-19 05:47:28 -0800711 send_config->encoder_settings.encoder = encoder_;
brandtr39f97292016-11-16 22:57:50 -0800712 send_config->encoder_settings.payload_name = payload_name_;
713 if (header_extensions_enabled_) {
714 send_config->rtp.extensions.push_back(RtpExtension(
715 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
716 send_config->rtp.extensions.push_back(RtpExtension(
717 RtpExtension::kTimestampOffsetUri, test::kTOffsetExtensionId));
stefanb77c7162017-02-06 06:29:38 -0800718 } else {
719 send_config->rtp.extensions.clear();
brandtr39f97292016-11-16 22:57:50 -0800720 }
721 }
722
723 void PerformTest() override {
724 EXPECT_TRUE(Wait())
725 << "Timed out waiting for FlexFEC and/or media packets.";
726 }
727
brandtr696c9c62016-12-19 05:47:28 -0800728 VideoEncoder* const encoder_;
729 std::string payload_name_;
brandtr39f97292016-11-16 22:57:50 -0800730 const bool use_nack_;
brandtr39f97292016-11-16 22:57:50 -0800731 bool sent_media_;
732 bool sent_flexfec_;
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100733 const bool header_extensions_enabled_;
734 const size_t num_video_streams_;
brandtr39f97292016-11-16 22:57:50 -0800735};
736
brandtrd654a9b2016-12-05 05:38:19 -0800737TEST_F(VideoSendStreamTest, SupportsFlexfecVp8) {
brandtr696c9c62016-12-19 05:47:28 -0800738 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100739 FlexfecObserver test(false, false, "VP8", encoder.get(), 1);
740 RunBaseTest(&test);
741}
742
743TEST_F(VideoSendStreamTest, SupportsFlexfecSimulcastVp8) {
744 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
745 FlexfecObserver test(false, false, "VP8", encoder.get(), 2);
brandtrd654a9b2016-12-05 05:38:19 -0800746 RunBaseTest(&test);
747}
748
749TEST_F(VideoSendStreamTest, SupportsFlexfecWithNackVp8) {
brandtr696c9c62016-12-19 05:47:28 -0800750 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100751 FlexfecObserver test(false, true, "VP8", encoder.get(), 1);
brandtrd654a9b2016-12-05 05:38:19 -0800752 RunBaseTest(&test);
753}
754
brandtr39f97292016-11-16 22:57:50 -0800755TEST_F(VideoSendStreamTest, SupportsFlexfecWithRtpExtensionsVp8) {
brandtr696c9c62016-12-19 05:47:28 -0800756 std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100757 FlexfecObserver test(true, false, "VP8", encoder.get(), 1);
brandtr39f97292016-11-16 22:57:50 -0800758 RunBaseTest(&test);
759}
760
brandtr39f97292016-11-16 22:57:50 -0800761#if !defined(RTC_DISABLE_VP9)
brandtrd654a9b2016-12-05 05:38:19 -0800762TEST_F(VideoSendStreamTest, SupportsFlexfecVp9) {
brandtr696c9c62016-12-19 05:47:28 -0800763 std::unique_ptr<VideoEncoder> encoder(VP9Encoder::Create());
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100764 FlexfecObserver test(false, false, "VP9", encoder.get(), 1);
brandtr39f97292016-11-16 22:57:50 -0800765 RunBaseTest(&test);
766}
767
brandtrd654a9b2016-12-05 05:38:19 -0800768TEST_F(VideoSendStreamTest, SupportsFlexfecWithNackVp9) {
brandtr696c9c62016-12-19 05:47:28 -0800769 std::unique_ptr<VideoEncoder> encoder(VP9Encoder::Create());
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100770 FlexfecObserver test(false, true, "VP9", encoder.get(), 1);
brandtr39f97292016-11-16 22:57:50 -0800771 RunBaseTest(&test);
772}
773#endif // defined(RTC_DISABLE_VP9)
774
brandtrd654a9b2016-12-05 05:38:19 -0800775TEST_F(VideoSendStreamTest, SupportsFlexfecH264) {
brandtr696c9c62016-12-19 05:47:28 -0800776 std::unique_ptr<VideoEncoder> encoder(
777 new test::FakeH264Encoder(Clock::GetRealTimeClock()));
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100778 FlexfecObserver test(false, false, "H264", encoder.get(), 1);
brandtr39f97292016-11-16 22:57:50 -0800779 RunBaseTest(&test);
780}
781
brandtrd654a9b2016-12-05 05:38:19 -0800782TEST_F(VideoSendStreamTest, SupportsFlexfecWithNackH264) {
brandtr696c9c62016-12-19 05:47:28 -0800783 std::unique_ptr<VideoEncoder> encoder(
784 new test::FakeH264Encoder(Clock::GetRealTimeClock()));
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100785 FlexfecObserver test(false, true, "H264", encoder.get(), 1);
brandtr696c9c62016-12-19 05:47:28 -0800786 RunBaseTest(&test);
787}
788
brandtre78d2662017-01-16 05:57:16 -0800789TEST_F(VideoSendStreamTest, SupportsFlexfecWithMultithreadedH264) {
brandtr696c9c62016-12-19 05:47:28 -0800790 std::unique_ptr<VideoEncoder> encoder(
brandtre78d2662017-01-16 05:57:16 -0800791 new test::MultithreadedFakeH264Encoder(Clock::GetRealTimeClock()));
Danil Chapovalov45d725d2018-02-19 19:09:53 +0100792 FlexfecObserver test(false, false, "H264", encoder.get(), 1);
brandtr39f97292016-11-16 22:57:50 -0800793 RunBaseTest(&test);
794}
795
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000796void VideoSendStreamTest::TestNackRetransmission(
797 uint32_t retransmit_ssrc,
stefan@webrtc.orgcb254aa2014-06-12 15:12:25 +0000798 uint8_t retransmit_payload_type) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000799 class NackObserver : public test::SendTest {
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000800 public:
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000801 explicit NackObserver(uint32_t retransmit_ssrc,
802 uint8_t retransmit_payload_type)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000803 : SendTest(kDefaultTimeoutMs),
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000804 send_count_(0),
Sebastian Janssond3f38162018-02-28 16:14:44 +0100805 retransmit_count_(0),
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000806 retransmit_ssrc_(retransmit_ssrc),
Sebastian Janssond3f38162018-02-28 16:14:44 +0100807 retransmit_payload_type_(retransmit_payload_type) {}
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000808
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000809 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000810 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000811 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000812 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000813
Sebastian Janssond3f38162018-02-28 16:14:44 +0100814 int kRetransmitTarget = 6;
815 ++send_count_;
816 if (send_count_ == 5 || send_count_ == 25) {
817 nacked_sequence_numbers_.push_back(
818 static_cast<uint16_t>(header.sequenceNumber - 3));
819 nacked_sequence_numbers_.push_back(
820 static_cast<uint16_t>(header.sequenceNumber - 2));
821 nacked_sequence_numbers_.push_back(
822 static_cast<uint16_t>(header.sequenceNumber - 1));
823
danilchap8a1d2a32017-08-01 03:21:37 -0700824 RTCPSender rtcp_sender(false, Clock::GetRealTimeClock(), nullptr,
Jiawei Ou3587b832018-01-31 22:08:26 -0800825 nullptr, nullptr, transport_adapter_.get(),
826 RtcpIntervalConfig{});
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000827
pbosda903ea2015-10-02 02:36:56 -0700828 rtcp_sender.SetRTCPStatus(RtcpMode::kReducedSize);
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100829 rtcp_sender.SetRemoteSSRC(kVideoSendSsrcs[0]);
pbos@webrtc.org0e63e762013-09-20 11:56:26 +0000830
831 RTCPSender::FeedbackState feedback_state;
832
Sebastian Janssond3f38162018-02-28 16:14:44 +0100833 EXPECT_EQ(0, rtcp_sender.SendRTCP(
834 feedback_state, kRtcpNack,
835 static_cast<int>(nacked_sequence_numbers_.size()),
836 &nacked_sequence_numbers_.front()));
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000837 }
838
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000839 uint16_t sequence_number = header.sequenceNumber;
840
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000841 if (header.ssrc == retransmit_ssrc_ &&
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100842 retransmit_ssrc_ != kVideoSendSsrcs[0]) {
843 // Not kVideoSendSsrcs[0], assume correct RTX packet. Extract sequence
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000844 // number.
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000845 const uint8_t* rtx_header = packet + header.headerLength;
846 sequence_number = (rtx_header[0] << 8) + rtx_header[1];
847 }
Sebastian Janssond3f38162018-02-28 16:14:44 +0100848 auto found = std::find(nacked_sequence_numbers_.begin(),
849 nacked_sequence_numbers_.end(), sequence_number);
850 if (found != nacked_sequence_numbers_.end()) {
851 nacked_sequence_numbers_.erase(found);
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000852
Sebastian Janssond3f38162018-02-28 16:14:44 +0100853 if (++retransmit_count_ == kRetransmitTarget) {
854 EXPECT_EQ(retransmit_ssrc_, header.ssrc);
855 EXPECT_EQ(retransmit_payload_type_, header.payloadType);
856 observation_complete_.Set();
857 }
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000858 }
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000859
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000860 return SEND_PACKET;
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000861 }
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +0000862
stefanff483612015-12-21 03:14:00 -0800863 void ModifyVideoConfigs(
864 VideoSendStream::Config* send_config,
865 std::vector<VideoReceiveStream::Config>* receive_configs,
866 VideoEncoderConfig* encoder_config) override {
stefanf116bd02015-10-27 08:29:42 -0700867 transport_adapter_.reset(
868 new internal::TransportAdapter(send_config->send_transport));
869 transport_adapter_->Enable();
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000870 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000871 send_config->rtp.rtx.payload_type = retransmit_payload_type_;
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100872 if (retransmit_ssrc_ != kVideoSendSsrcs[0])
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000873 send_config->rtp.rtx.ssrcs.push_back(retransmit_ssrc_);
874 }
875
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000876 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100877 EXPECT_TRUE(Wait()) << "Timed out while waiting for NACK retransmission.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000878 }
879
kwiberg27f982b2016-03-01 11:52:33 -0800880 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000881 int send_count_;
Sebastian Janssond3f38162018-02-28 16:14:44 +0100882 int retransmit_count_;
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000883 uint32_t retransmit_ssrc_;
stefan@webrtc.org69969e22013-11-15 12:32:15 +0000884 uint8_t retransmit_payload_type_;
Sebastian Janssond3f38162018-02-28 16:14:44 +0100885 std::vector<uint16_t> nacked_sequence_numbers_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000886 } test(retransmit_ssrc, retransmit_payload_type);
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000887
stefane74eef12016-01-08 06:47:13 -0800888 RunBaseTest(&test);
pbos@webrtc.orgdf531a22013-09-10 14:56:33 +0000889}
890
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000891TEST_F(VideoSendStreamTest, RetransmitsNack) {
892 // Normal NACKs should use the send SSRC.
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100893 TestNackRetransmission(kVideoSendSsrcs[0], kFakeVideoSendPayloadType);
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000894}
895
896TEST_F(VideoSendStreamTest, RetransmitsNackOverRtx) {
897 // NACKs over RTX should use a separate SSRC.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000898 TestNackRetransmission(kSendRtxSsrcs[0], kSendRtxPayloadType);
pbos@webrtc.org5860de02013-09-16 13:01:47 +0000899}
900
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000901void VideoSendStreamTest::TestPacketFragmentationSize(VideoFormat format,
902 bool with_fec) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000903 // Use a fake encoder to output a frame of every size in the range [90, 290],
904 // for each size making sure that the exact number of payload bytes received
905 // is correct and that packets are fragmented to respect max packet size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000906 static const size_t kMaxPacketSize = 128;
907 static const size_t start = 90;
908 static const size_t stop = 290;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000909
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000910 // Observer that verifies that the expected number of packets and bytes
911 // arrive for each frame size, from start_size to stop_size.
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000912 class FrameFragmentationTest : public test::SendTest,
913 public EncodedFrameObserver {
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000914 public:
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000915 FrameFragmentationTest(size_t max_packet_size,
916 size_t start_size,
917 size_t stop_size,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000918 bool test_generic_packetization,
919 bool use_fec)
920 : SendTest(kLongTimeoutMs),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000921 encoder_(stop),
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000922 max_packet_size_(max_packet_size),
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000923 stop_size_(stop_size),
924 test_generic_packetization_(test_generic_packetization),
925 use_fec_(use_fec),
926 packet_count_(0),
Sebastian Jansson56fa0502018-02-01 13:00:57 +0100927 packets_lost_(0),
928 last_packet_count_(0),
929 last_packets_lost_(0),
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000930 accumulated_size_(0),
931 accumulated_payload_(0),
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000932 fec_packet_received_(false),
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000933 current_size_rtp_(start_size),
Yuwei Huangd9f99c12017-10-24 15:40:52 -0700934 current_size_frame_(static_cast<int>(start_size)) {
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000935 // Fragmentation required, this test doesn't make sense without it.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000936 encoder_.SetFrameSize(start_size);
henrikg91d6ede2015-09-17 00:24:34 -0700937 RTC_DCHECK_GT(stop_size, max_packet_size);
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000938 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000939
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000940 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000941 Action OnSendRtp(const uint8_t* packet, size_t size) override {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000942 size_t length = size;
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000943 RTPHeader header;
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000944 EXPECT_TRUE(parser_->Parse(packet, length, &header));
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000945
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000946 EXPECT_LE(length, max_packet_size_);
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000947
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000948 if (use_fec_) {
949 uint8_t payload_type = packet[header.headerLength];
950 bool is_fec = header.payloadType == kRedPayloadType &&
951 payload_type == kUlpfecPayloadType;
952 if (is_fec) {
953 fec_packet_received_ = true;
954 return SEND_PACKET;
955 }
956 }
957
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000958 accumulated_size_ += length;
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000959
960 if (use_fec_)
961 TriggerLossReport(header);
962
963 if (test_generic_packetization_) {
Stefan Holmer586b19b2015-09-18 11:14:31 +0200964 size_t overhead = header.headerLength + header.paddingLength;
965 // Only remove payload header and RED header if the packet actually
966 // contains payload.
967 if (length > overhead) {
968 overhead += (1 /* Generic header */);
969 if (use_fec_)
970 overhead += 1; // RED for FEC header.
971 }
972 EXPECT_GE(length, overhead);
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000973 accumulated_payload_ += length - overhead;
974 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000975
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000976 // Marker bit set indicates last packet of a frame.
sprang@webrtc.org5d957e22013-10-16 11:37:54 +0000977 if (header.markerBit) {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000978 if (use_fec_ && accumulated_payload_ == current_size_rtp_ - 1) {
979 // With FEC enabled, frame size is incremented asynchronously, so
980 // "old" frames one byte too small may arrive. Accept, but don't
981 // increase expected frame size.
982 accumulated_size_ = 0;
983 accumulated_payload_ = 0;
984 return SEND_PACKET;
985 }
986
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000987 EXPECT_GE(accumulated_size_, current_size_rtp_);
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000988 if (test_generic_packetization_) {
989 EXPECT_EQ(current_size_rtp_, accumulated_payload_);
990 }
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000991
992 // Last packet of frame; reset counters.
993 accumulated_size_ = 0;
994 accumulated_payload_ = 0;
995 if (current_size_rtp_ == stop_size_) {
996 // Done! (Don't increase size again, might arrive more @ stop_size).
Peter Boström5811a392015-12-10 13:02:50 +0100997 observation_complete_.Set();
sprang@webrtc.org8b881922013-12-10 10:05:17 +0000998 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000999 // Increase next expected frame size. If testing with FEC, make sure
1000 // a FEC packet has been received for this frame size before
1001 // proceeding, to make sure that redundancy packets don't exceed
1002 // size limit.
1003 if (!use_fec_) {
1004 ++current_size_rtp_;
1005 } else if (fec_packet_received_) {
1006 fec_packet_received_ = false;
1007 ++current_size_rtp_;
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001008
1009 rtc::CritScope lock(&mutex_);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001010 ++current_size_frame_;
1011 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001012 }
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001013 }
1014
stefan@webrtc.org69969e22013-11-15 12:32:15 +00001015 return SEND_PACKET;
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001016 }
1017
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001018 void TriggerLossReport(const RTPHeader& header) {
1019 // Send lossy receive reports to trigger FEC enabling.
sprang4847ae62017-06-27 07:06:52 -07001020 const int kLossPercent = 5;
Sebastian Jansson56fa0502018-02-01 13:00:57 +01001021 if (++packet_count_ % (100 / kLossPercent) == 0) {
1022 packets_lost_++;
1023 int loss_delta = packets_lost_ - last_packets_lost_;
1024 int packets_delta = packet_count_ - last_packet_count_;
1025 last_packet_count_ = packet_count_;
1026 last_packets_lost_ = packets_lost_;
1027 uint8_t loss_ratio =
1028 static_cast<uint8_t>(loss_delta * 255 / packets_delta);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001029 FakeReceiveStatistics lossy_receive_stats(
sprang4847ae62017-06-27 07:06:52 -07001030 kVideoSendSsrcs[0], header.sequenceNumber,
Sebastian Jansson56fa0502018-02-01 13:00:57 +01001031 packets_lost_, // Cumulative lost.
1032 loss_ratio); // Loss percent.
Peter Boströmac547a62015-09-17 23:03:57 +02001033 RTCPSender rtcp_sender(false, Clock::GetRealTimeClock(),
terelius429c3452016-01-21 05:42:04 -08001034 &lossy_receive_stats, nullptr, nullptr,
Jiawei Ou3587b832018-01-31 22:08:26 -08001035 transport_adapter_.get(), RtcpIntervalConfig{});
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001036
pbosda903ea2015-10-02 02:36:56 -07001037 rtcp_sender.SetRTCPStatus(RtcpMode::kReducedSize);
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001038 rtcp_sender.SetRemoteSSRC(kVideoSendSsrcs[0]);
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001039
1040 RTCPSender::FeedbackState feedback_state;
1041
1042 EXPECT_EQ(0, rtcp_sender.SendRTCP(feedback_state, kRtcpRr));
1043 }
1044 }
1045
nisseef8b61e2016-04-29 06:09:15 -07001046 void EncodedFrameCallback(const EncodedFrame& encoded_frame) override {
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001047 rtc::CritScope lock(&mutex_);
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001048 // Increase frame size for next encoded frame, in the context of the
1049 // encoder thread.
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001050 if (!use_fec_ && current_size_frame_ < static_cast<int32_t>(stop_size_)) {
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001051 ++current_size_frame_;
1052 }
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001053 encoder_.SetFrameSize(static_cast<size_t>(current_size_frame_));
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001054 }
1055
Stefan Holmere5904162015-03-26 11:11:06 +01001056 Call::Config GetSenderCallConfig() override {
philipel4fb651d2017-04-10 03:54:05 -07001057 Call::Config config(event_log_.get());
Stefan Holmere5904162015-03-26 11:11:06 +01001058 const int kMinBitrateBps = 30000;
1059 config.bitrate_config.min_bitrate_bps = kMinBitrateBps;
1060 return config;
1061 }
1062
stefanff483612015-12-21 03:14:00 -08001063 void ModifyVideoConfigs(
1064 VideoSendStream::Config* send_config,
1065 std::vector<VideoReceiveStream::Config>* receive_configs,
1066 VideoEncoderConfig* encoder_config) override {
stefanf116bd02015-10-27 08:29:42 -07001067 transport_adapter_.reset(
1068 new internal::TransportAdapter(send_config->send_transport));
1069 transport_adapter_->Enable();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001070 if (use_fec_) {
brandtrb5f2c3f2016-10-04 23:28:39 -07001071 send_config->rtp.ulpfec.red_payload_type = kRedPayloadType;
1072 send_config->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001073 }
1074
1075 if (!test_generic_packetization_)
1076 send_config->encoder_settings.payload_name = "VP8";
1077
1078 send_config->encoder_settings.encoder = &encoder_;
1079 send_config->rtp.max_packet_size = kMaxPacketSize;
1080 send_config->post_encode_callback = this;
1081
Erik Språng95261872015-04-10 11:58:49 +02001082 // Make sure there is at least one extension header, to make the RTP
1083 // header larger than the base length of 12 bytes.
1084 EXPECT_FALSE(send_config->rtp.extensions.empty());
sprang4847ae62017-06-27 07:06:52 -07001085
1086 // Setup screen content disables frame dropping which makes this easier.
1087 class VideoStreamFactory
1088 : public VideoEncoderConfig::VideoStreamFactoryInterface {
1089 public:
1090 explicit VideoStreamFactory(size_t num_temporal_layers)
1091 : num_temporal_layers_(num_temporal_layers) {
1092 EXPECT_GT(num_temporal_layers, 0u);
1093 }
1094
1095 private:
1096 std::vector<VideoStream> CreateEncoderStreams(
1097 int width,
1098 int height,
1099 const VideoEncoderConfig& encoder_config) override {
1100 std::vector<VideoStream> streams =
1101 test::CreateVideoStreams(width, height, encoder_config);
1102 for (VideoStream& stream : streams) {
Sergey Silkina796a7e2018-03-01 15:11:29 +01001103 stream.num_temporal_layers = num_temporal_layers_;
sprang4847ae62017-06-27 07:06:52 -07001104 }
1105 return streams;
1106 }
1107 const size_t num_temporal_layers_;
1108 };
1109
1110 encoder_config->video_stream_factory =
1111 new rtc::RefCountedObject<VideoStreamFactory>(2);
1112 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001113 }
1114
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001115 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001116 EXPECT_TRUE(Wait()) << "Timed out while observing incoming RTP packets.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001117 }
1118
kwiberg27f982b2016-03-01 11:52:33 -08001119 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001120 test::ConfigurableFrameSizeEncoder encoder_;
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001121
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001122 const size_t max_packet_size_;
1123 const size_t stop_size_;
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001124 const bool test_generic_packetization_;
1125 const bool use_fec_;
1126
1127 uint32_t packet_count_;
Sebastian Jansson56fa0502018-02-01 13:00:57 +01001128 uint32_t packets_lost_;
1129 uint32_t last_packet_count_;
1130 uint32_t last_packets_lost_;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001131 size_t accumulated_size_;
1132 size_t accumulated_payload_;
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001133 bool fec_packet_received_;
1134
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001135 size_t current_size_rtp_;
Yuwei Huangd9f99c12017-10-24 15:40:52 -07001136 rtc::CriticalSection mutex_;
1137 int current_size_frame_ RTC_GUARDED_BY(mutex_);
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001138 };
1139
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001140 // Don't auto increment if FEC is used; continue sending frame size until
1141 // a FEC packet has been received.
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001142 FrameFragmentationTest test(
1143 kMaxPacketSize, start, stop, format == kGeneric, with_fec);
sprang@webrtc.org8b881922013-12-10 10:05:17 +00001144
stefane74eef12016-01-08 06:47:13 -08001145 RunBaseTest(&test);
sprang@webrtc.org5d957e22013-10-16 11:37:54 +00001146}
1147
sprang@webrtc.org346094c2014-02-18 08:40:33 +00001148// TODO(sprang): Is there any way of speeding up these tests?
1149TEST_F(VideoSendStreamTest, FragmentsGenericAccordingToMaxPacketSize) {
1150 TestPacketFragmentationSize(kGeneric, false);
1151}
1152
1153TEST_F(VideoSendStreamTest, FragmentsGenericAccordingToMaxPacketSizeWithFec) {
1154 TestPacketFragmentationSize(kGeneric, true);
1155}
1156
1157TEST_F(VideoSendStreamTest, FragmentsVp8AccordingToMaxPacketSize) {
1158 TestPacketFragmentationSize(kVP8, false);
1159}
1160
1161TEST_F(VideoSendStreamTest, FragmentsVp8AccordingToMaxPacketSizeWithFec) {
1162 TestPacketFragmentationSize(kVP8, true);
1163}
1164
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001165// The test will go through a number of phases.
1166// 1. Start sending packets.
1167// 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 +00001168// suspend the stream.
1169// 3. Wait until |kSuspendTimeFrames| have been captured without seeing any RTP
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001170// packets.
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001171// 4. Signal a high REMB and then wait for the RTP stream to start again.
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001172// When the stream is detected again, and the stats show that the stream
1173// is no longer suspended, the test ends.
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001174TEST_F(VideoSendStreamTest, SuspendBelowMinBitrate) {
1175 static const int kSuspendTimeFrames = 60; // Suspend for 2 seconds @ 30 fps.
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001176
nissed30a1112016-04-18 05:15:22 -07001177 class RembObserver : public test::SendTest,
1178 public rtc::VideoSinkInterface<VideoFrame> {
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001179 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001180 RembObserver()
1181 : SendTest(kDefaultTimeoutMs),
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001182 clock_(Clock::GetRealTimeClock()),
Erik Språng737336d2016-07-29 12:59:36 +02001183 stream_(nullptr),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001184 test_state_(kBeforeSuspend),
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001185 rtp_count_(0),
1186 last_sequence_number_(0),
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001187 suspended_frame_count_(0),
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001188 low_remb_bps_(0),
Erik Språng737336d2016-07-29 12:59:36 +02001189 high_remb_bps_(0) {}
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001190
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001191 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001192 Action OnSendRtp(const uint8_t* packet, size_t length) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001193 rtc::CritScope lock(&crit_);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001194 ++rtp_count_;
1195 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001196 EXPECT_TRUE(parser_->Parse(packet, length, &header));
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001197 last_sequence_number_ = header.sequenceNumber;
1198
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001199 if (test_state_ == kBeforeSuspend) {
1200 // The stream has started. Try to suspend it.
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001201 SendRtcpFeedback(low_remb_bps_);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001202 test_state_ = kDuringSuspend;
1203 } else if (test_state_ == kDuringSuspend) {
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001204 if (header.paddingLength == 0) {
1205 // Received non-padding packet during suspension period. Reset the
1206 // counter.
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001207 suspended_frame_count_ = 0;
1208 }
stefanf116bd02015-10-27 08:29:42 -07001209 SendRtcpFeedback(0); // REMB is only sent if value is > 0.
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001210 } else if (test_state_ == kWaitingForPacket) {
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001211 if (header.paddingLength == 0) {
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001212 // Non-padding packet observed. Test is almost complete. Will just
1213 // have to wait for the stats to change.
1214 test_state_ = kWaitingForStats;
1215 }
stefanf116bd02015-10-27 08:29:42 -07001216 SendRtcpFeedback(0); // REMB is only sent if value is > 0.
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001217 } else if (test_state_ == kWaitingForStats) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001218 VideoSendStream::Stats stats = stream_->GetStats();
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001219 if (stats.suspended == false) {
1220 // Stats flipped to false. Test is complete.
Peter Boström5811a392015-12-10 13:02:50 +01001221 observation_complete_.Set();
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +00001222 }
stefanf116bd02015-10-27 08:29:42 -07001223 SendRtcpFeedback(0); // REMB is only sent if value is > 0.
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001224 }
1225
stefan@webrtc.org69969e22013-11-15 12:32:15 +00001226 return SEND_PACKET;
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001227 }
1228
perkj26091b12016-09-01 01:17:40 -07001229 // This method implements the rtc::VideoSinkInterface. This is called when
1230 // a frame is provided to the VideoSendStream.
nissed30a1112016-04-18 05:15:22 -07001231 void OnFrame(const VideoFrame& video_frame) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001232 rtc::CritScope lock(&crit_);
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001233 if (test_state_ == kDuringSuspend &&
1234 ++suspended_frame_count_ > kSuspendTimeFrames) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001235 VideoSendStream::Stats stats = stream_->GetStats();
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +00001236 EXPECT_TRUE(stats.suspended);
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001237 SendRtcpFeedback(high_remb_bps_);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001238 test_state_ = kWaitingForPacket;
1239 }
1240 }
1241
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001242 void set_low_remb_bps(int value) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001243 rtc::CritScope lock(&crit_);
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001244 low_remb_bps_ = value;
1245 }
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001246
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001247 void set_high_remb_bps(int value) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001248 rtc::CritScope lock(&crit_);
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001249 high_remb_bps_ = value;
1250 }
henrik.lundin@webrtc.org1a3a6e52013-10-28 10:16:14 +00001251
stefanff483612015-12-21 03:14:00 -08001252 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +00001253 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001254 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001255 stream_ = send_stream;
1256 }
1257
stefanff483612015-12-21 03:14:00 -08001258 void ModifyVideoConfigs(
1259 VideoSendStream::Config* send_config,
1260 std::vector<VideoReceiveStream::Config>* receive_configs,
1261 VideoEncoderConfig* encoder_config) override {
kwibergaf476c72016-11-28 15:21:39 -08001262 RTC_DCHECK_EQ(1, encoder_config->number_of_streams);
stefanf116bd02015-10-27 08:29:42 -07001263 transport_adapter_.reset(
1264 new internal::TransportAdapter(send_config->send_transport));
1265 transport_adapter_->Enable();
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +00001266 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001267 send_config->pre_encode_callback = this;
1268 send_config->suspend_below_min_bitrate = true;
perkjfa10b552016-10-02 23:45:26 -07001269 int min_bitrate_bps =
1270 test::DefaultVideoStreamFactory::kDefaultMinBitratePerStream[0];
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001271 set_low_remb_bps(min_bitrate_bps - 10000);
mflodman101f2502016-06-09 17:21:19 +02001272 int threshold_window = std::max(min_bitrate_bps / 10, 20000);
perkjfa10b552016-10-02 23:45:26 -07001273 ASSERT_GT(encoder_config->max_bitrate_bps,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001274 min_bitrate_bps + threshold_window + 5000);
1275 set_high_remb_bps(min_bitrate_bps + threshold_window + 5000);
1276 }
1277
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001278 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001279 EXPECT_TRUE(Wait()) << "Timed out during suspend-below-min-bitrate test.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001280 }
1281
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001282 enum TestState {
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +00001283 kBeforeSuspend,
1284 kDuringSuspend,
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001285 kWaitingForPacket,
henrik.lundin@webrtc.orged8b2812014-03-18 08:43:29 +00001286 kWaitingForStats
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001287 };
1288
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001289 virtual void SendRtcpFeedback(int remb_value)
danilchapa37de392017-09-09 04:17:22 -07001290 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001291 FakeReceiveStatistics receive_stats(kVideoSendSsrcs[0],
1292 last_sequence_number_, rtp_count_, 0);
terelius429c3452016-01-21 05:42:04 -08001293 RTCPSender rtcp_sender(false, clock_, &receive_stats, nullptr, nullptr,
Jiawei Ou3587b832018-01-31 22:08:26 -08001294 transport_adapter_.get(), RtcpIntervalConfig{});
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001295
pbosda903ea2015-10-02 02:36:56 -07001296 rtcp_sender.SetRTCPStatus(RtcpMode::kReducedSize);
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001297 rtcp_sender.SetRemoteSSRC(kVideoSendSsrcs[0]);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001298 if (remb_value > 0) {
Danil Chapovalovf74d6412017-10-18 13:32:57 +02001299 rtcp_sender.SetRemb(remb_value, std::vector<uint32_t>());
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001300 }
1301 RTCPSender::FeedbackState feedback_state;
1302 EXPECT_EQ(0, rtcp_sender.SendRTCP(feedback_state, kRtcpRr));
1303 }
1304
kwiberg27f982b2016-03-01 11:52:33 -08001305 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001306 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001307 VideoSendStream* stream_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001308
Peter Boströmf2f82832015-05-01 13:00:41 +02001309 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001310 TestState test_state_ RTC_GUARDED_BY(crit_);
1311 int rtp_count_ RTC_GUARDED_BY(crit_);
1312 int last_sequence_number_ RTC_GUARDED_BY(crit_);
1313 int suspended_frame_count_ RTC_GUARDED_BY(crit_);
1314 int low_remb_bps_ RTC_GUARDED_BY(crit_);
1315 int high_remb_bps_ RTC_GUARDED_BY(crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001316 } test;
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001317
stefane74eef12016-01-08 06:47:13 -08001318 RunBaseTest(&test);
henrik.lundin@webrtc.orgba975e22013-10-23 11:04:57 +00001319}
1320
perkj71ee44c2016-06-15 00:47:53 -07001321// This test that padding stops being send after a while if the Camera stops
1322// producing video frames and that padding resumes if the camera restarts.
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001323TEST_F(VideoSendStreamTest, NoPaddingWhenVideoIsMuted) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001324 class NoPaddingWhenVideoIsMuted : public test::SendTest {
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001325 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001326 NoPaddingWhenVideoIsMuted()
1327 : SendTest(kDefaultTimeoutMs),
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001328 clock_(Clock::GetRealTimeClock()),
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001329 last_packet_time_ms_(-1),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +00001330 capturer_(nullptr) {
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +00001331 }
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001332
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001333 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001334 Action OnSendRtp(const uint8_t* packet, size_t length) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001335 rtc::CritScope lock(&crit_);
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001336 last_packet_time_ms_ = clock_->TimeInMilliseconds();
perkj71ee44c2016-06-15 00:47:53 -07001337
1338 RTPHeader header;
1339 parser_->Parse(packet, length, &header);
1340 const bool only_padding =
1341 header.headerLength + header.paddingLength == length;
1342
1343 if (test_state_ == kBeforeStopCapture) {
1344 capturer_->Stop();
1345 test_state_ = kWaitingForPadding;
1346 } else if (test_state_ == kWaitingForPadding && only_padding) {
1347 test_state_ = kWaitingForNoPackets;
1348 } else if (test_state_ == kWaitingForPaddingAfterCameraRestart &&
1349 only_padding) {
1350 observation_complete_.Set();
1351 }
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001352 return SEND_PACKET;
1353 }
1354
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001355 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001356 rtc::CritScope lock(&crit_);
perkj71ee44c2016-06-15 00:47:53 -07001357 const int kNoPacketsThresholdMs = 2000;
1358 if (test_state_ == kWaitingForNoPackets &&
1359 (last_packet_time_ms_ > 0 &&
1360 clock_->TimeInMilliseconds() - last_packet_time_ms_ >
1361 kNoPacketsThresholdMs)) {
1362 capturer_->Start();
1363 test_state_ = kWaitingForPaddingAfterCameraRestart;
1364 }
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001365 return SEND_PACKET;
1366 }
1367
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001368 size_t GetNumVideoStreams() const override { return 3; }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001369
nisseef8b61e2016-04-29 06:09:15 -07001370 void OnFrameGeneratorCapturerCreated(
1371 test::FrameGeneratorCapturer* frame_generator_capturer) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02001372 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001373 capturer_ = frame_generator_capturer;
1374 }
1375
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001376 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001377 EXPECT_TRUE(Wait())
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001378 << "Timed out while waiting for RTP packets to stop being sent.";
1379 }
1380
perkj71ee44c2016-06-15 00:47:53 -07001381 enum TestState {
1382 kBeforeStopCapture,
1383 kWaitingForPadding,
1384 kWaitingForNoPackets,
1385 kWaitingForPaddingAfterCameraRestart
1386 };
1387
1388 TestState test_state_ = kBeforeStopCapture;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +00001389 Clock* const clock_;
kwiberg27f982b2016-03-01 11:52:33 -08001390 std::unique_ptr<internal::TransportAdapter> transport_adapter_;
Peter Boströmf2f82832015-05-01 13:00:41 +02001391 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07001392 int64_t last_packet_time_ms_ RTC_GUARDED_BY(crit_);
1393 test::FrameGeneratorCapturer* capturer_ RTC_GUARDED_BY(crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001394 } test;
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001395
stefane74eef12016-01-08 06:47:13 -08001396 RunBaseTest(&test);
stefan@webrtc.org4ab4fc02013-11-25 11:54:24 +00001397}
1398
isheriffcc5903e2016-10-04 08:29:38 -07001399TEST_F(VideoSendStreamTest, PaddingIsPrimarilyRetransmissions) {
1400 const int kCapacityKbps = 10000; // 10 Mbps
1401 class PaddingIsPrimarilyRetransmissions : public test::EndToEndTest {
1402 public:
1403 PaddingIsPrimarilyRetransmissions()
1404 : EndToEndTest(kDefaultTimeoutMs),
1405 clock_(Clock::GetRealTimeClock()),
1406 padding_length_(0),
1407 total_length_(0),
1408 call_(nullptr) {}
1409
1410 private:
1411 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1412 call_ = sender_call;
1413 }
1414
1415 Action OnSendRtp(const uint8_t* packet, size_t length) override {
1416 rtc::CritScope lock(&crit_);
1417
1418 RTPHeader header;
1419 parser_->Parse(packet, length, &header);
1420 padding_length_ += header.paddingLength;
1421 total_length_ += length;
1422 return SEND_PACKET;
1423 }
1424
eladalon413ee9a2017-08-22 04:02:52 -07001425 test::PacketTransport* CreateSendTransport(
1426 test::SingleThreadedTaskQueueForTesting* task_queue,
1427 Call* sender_call) override {
isheriffcc5903e2016-10-04 08:29:38 -07001428 const int kNetworkDelayMs = 50;
1429 FakeNetworkPipe::Config config;
1430 config.loss_percent = 10;
1431 config.link_capacity_kbps = kCapacityKbps;
1432 config.queue_delay_ms = kNetworkDelayMs;
eladalon413ee9a2017-08-22 04:02:52 -07001433 return new test::PacketTransport(task_queue, sender_call, this,
nissee5ad5ca2017-03-29 23:57:43 -07001434 test::PacketTransport::kSender,
minyue20c84cc2017-04-10 16:57:57 -07001435 payload_type_map_, config);
isheriffcc5903e2016-10-04 08:29:38 -07001436 }
1437
1438 void ModifyVideoConfigs(
1439 VideoSendStream::Config* send_config,
1440 std::vector<VideoReceiveStream::Config>* receive_configs,
1441 VideoEncoderConfig* encoder_config) override {
isheriffcc5903e2016-10-04 08:29:38 -07001442 // Turn on RTX.
1443 send_config->rtp.rtx.payload_type = kFakeVideoSendPayloadType;
1444 send_config->rtp.rtx.ssrcs.push_back(kVideoSendSsrcs[0]);
isheriffcc5903e2016-10-04 08:29:38 -07001445 }
1446
1447 void PerformTest() override {
1448 // TODO(isheriff): Some platforms do not ramp up as expected to full
1449 // capacity due to packet scheduling delays. Fix that before getting
1450 // rid of this.
1451 SleepMs(5000);
1452 {
1453 rtc::CritScope lock(&crit_);
1454 // Expect padding to be a small percentage of total bytes sent.
1455 EXPECT_LT(padding_length_, .1 * total_length_);
1456 }
1457 }
1458
1459 rtc::CriticalSection crit_;
1460 Clock* const clock_;
danilchapa37de392017-09-09 04:17:22 -07001461 size_t padding_length_ RTC_GUARDED_BY(crit_);
1462 size_t total_length_ RTC_GUARDED_BY(crit_);
isheriffcc5903e2016-10-04 08:29:38 -07001463 Call* call_;
1464 } test;
1465
1466 RunBaseTest(&test);
1467}
1468
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001469// This test first observes "high" bitrate use at which point it sends a REMB to
1470// indicate that it should be lowered significantly. The test then observes that
1471// the bitrate observed is sinking well below the min-transmit-bitrate threshold
1472// to verify that the min-transmit bitrate respects incoming REMB.
andresp@webrtc.org44caf012014-03-26 21:00:21 +00001473//
1474// Note that the test starts at "high" bitrate and does not ramp up to "higher"
1475// bitrate since no receiver block or remb is sent in the initial phase.
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001476TEST_F(VideoSendStreamTest, MinTransmitBitrateRespectsRemb) {
1477 static const int kMinTransmitBitrateBps = 400000;
andresp@webrtc.org44caf012014-03-26 21:00:21 +00001478 static const int kHighBitrateBps = 150000;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001479 static const int kRembBitrateBps = 80000;
1480 static const int kRembRespectedBitrateBps = 100000;
stefanf116bd02015-10-27 08:29:42 -07001481 class BitrateObserver : public test::SendTest {
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001482 public:
1483 BitrateObserver()
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001484 : SendTest(kDefaultTimeoutMs),
Erik Språng737336d2016-07-29 12:59:36 +02001485 retranmission_rate_limiter_(Clock::GetRealTimeClock(), 1000),
1486 stream_(nullptr),
1487 bitrate_capped_(false) {}
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001488
1489 private:
nisseef8b61e2016-04-29 06:09:15 -07001490 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001491 if (RtpHeaderParser::IsRtcp(packet, length))
stefanf116bd02015-10-27 08:29:42 -07001492 return DROP_PACKET;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001493
1494 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001495 if (!parser_->Parse(packet, length, &header))
stefanf116bd02015-10-27 08:29:42 -07001496 return DROP_PACKET;
Peter Boström74f6e9e2016-04-04 17:56:10 +02001497 RTC_DCHECK(stream_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001498 VideoSendStream::Stats stats = stream_->GetStats();
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001499 if (!stats.substreams.empty()) {
1500 EXPECT_EQ(1u, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001501 int total_bitrate_bps =
1502 stats.substreams.begin()->second.total_bitrate_bps;
1503 test::PrintResult("bitrate_stats_",
1504 "min_transmit_bitrate_low_remb",
1505 "bitrate_bps",
1506 static_cast<size_t>(total_bitrate_bps),
1507 "bps",
1508 false);
1509 if (total_bitrate_bps > kHighBitrateBps) {
Danil Chapovalov51e21aa2017-10-10 17:46:26 +02001510 rtp_rtcp_->SetRemb(kRembBitrateBps,
1511 std::vector<uint32_t>(1, header.ssrc));
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001512 rtp_rtcp_->Process();
1513 bitrate_capped_ = true;
1514 } else if (bitrate_capped_ &&
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001515 total_bitrate_bps < kRembRespectedBitrateBps) {
Peter Boström5811a392015-12-10 13:02:50 +01001516 observation_complete_.Set();
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001517 }
1518 }
stefanf116bd02015-10-27 08:29:42 -07001519 // Packets don't have to be delivered since the test is the receiver.
1520 return DROP_PACKET;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001521 }
1522
stefanff483612015-12-21 03:14:00 -08001523 void OnVideoStreamsCreated(
stefanf116bd02015-10-27 08:29:42 -07001524 VideoSendStream* send_stream,
1525 const std::vector<VideoReceiveStream*>& receive_streams) override {
1526 stream_ = send_stream;
1527 RtpRtcp::Configuration config;
1528 config.outgoing_transport = feedback_transport_.get();
Erik Språng737336d2016-07-29 12:59:36 +02001529 config.retransmission_rate_limiter = &retranmission_rate_limiter_;
stefanf116bd02015-10-27 08:29:42 -07001530 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
stefanf116bd02015-10-27 08:29:42 -07001531 rtp_rtcp_->SetRTCPStatus(RtcpMode::kReducedSize);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001532 }
1533
stefanff483612015-12-21 03:14:00 -08001534 void ModifyVideoConfigs(
1535 VideoSendStream::Config* send_config,
1536 std::vector<VideoReceiveStream::Config>* receive_configs,
1537 VideoEncoderConfig* encoder_config) override {
stefanf116bd02015-10-27 08:29:42 -07001538 feedback_transport_.reset(
1539 new internal::TransportAdapter(send_config->send_transport));
1540 feedback_transport_->Enable();
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +00001541 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001542 }
1543
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001544 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01001545 EXPECT_TRUE(Wait())
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001546 << "Timeout while waiting for low bitrate stats after REMB.";
1547 }
1548
kwiberg27f982b2016-03-01 11:52:33 -08001549 std::unique_ptr<RtpRtcp> rtp_rtcp_;
1550 std::unique_ptr<internal::TransportAdapter> feedback_transport_;
Erik Språng737336d2016-07-29 12:59:36 +02001551 RateLimiter retranmission_rate_limiter_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001552 VideoSendStream* stream_;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001553 bool bitrate_capped_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +00001554 } test;
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001555
stefane74eef12016-01-08 06:47:13 -08001556 RunBaseTest(&test);
pbos@webrtc.org709e2972014-03-19 10:59:52 +00001557}
1558
Stefan Holmer280de9e2016-09-30 10:06:51 +02001559TEST_F(VideoSendStreamTest, ChangingNetworkRoute) {
1560 static const int kStartBitrateBps = 300000;
1561 static const int kNewMaxBitrateBps = 1234567;
danilchap42ca68a2016-10-31 03:34:40 -07001562 static const uint8_t kExtensionId = test::kTransportSequenceNumberExtensionId;
Stefan Holmerbe402962016-07-08 16:16:41 +02001563 class ChangingNetworkRouteTest : public test::EndToEndTest {
1564 public:
eladalon413ee9a2017-08-22 04:02:52 -07001565 explicit ChangingNetworkRouteTest(
1566 test::SingleThreadedTaskQueueForTesting* task_queue)
1567 : EndToEndTest(test::CallTest::kDefaultTimeoutMs),
1568 task_queue_(task_queue),
1569 call_(nullptr) {
Stefan Holmer280de9e2016-09-30 10:06:51 +02001570 EXPECT_TRUE(parser_->RegisterRtpHeaderExtension(
1571 kRtpExtensionTransportSequenceNumber, kExtensionId));
1572 }
Stefan Holmerbe402962016-07-08 16:16:41 +02001573
1574 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1575 call_ = sender_call;
1576 }
1577
Stefan Holmer280de9e2016-09-30 10:06:51 +02001578 void ModifyVideoConfigs(
1579 VideoSendStream::Config* send_config,
1580 std::vector<VideoReceiveStream::Config>* receive_configs,
1581 VideoEncoderConfig* encoder_config) override {
1582 send_config->rtp.extensions.clear();
1583 send_config->rtp.extensions.push_back(RtpExtension(
1584 RtpExtension::kTransportSequenceNumberUri, kExtensionId));
1585 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions;
1586 (*receive_configs)[0].rtp.transport_cc = true;
1587 }
1588
1589 void ModifyAudioConfigs(
1590 AudioSendStream::Config* send_config,
1591 std::vector<AudioReceiveStream::Config>* receive_configs) override {
1592 send_config->rtp.extensions.clear();
1593 send_config->rtp.extensions.push_back(RtpExtension(
1594 RtpExtension::kTransportSequenceNumberUri, kExtensionId));
1595 (*receive_configs)[0].rtp.extensions.clear();
1596 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions;
1597 (*receive_configs)[0].rtp.transport_cc = true;
1598 }
1599
Stefan Holmerbe402962016-07-08 16:16:41 +02001600 Action OnSendRtp(const uint8_t* packet, size_t length) override {
1601 if (call_->GetStats().send_bandwidth_bps > kStartBitrateBps) {
1602 observation_complete_.Set();
1603 }
1604
1605 return SEND_PACKET;
1606 }
1607
1608 void PerformTest() override {
1609 rtc::NetworkRoute new_route(true, 10, 20, -1);
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01001610 BitrateConstraints bitrate_config;
eladalon413ee9a2017-08-22 04:02:52 -07001611
1612 task_queue_->SendTask([this, &new_route, &bitrate_config]() {
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001613 call_->GetTransportControllerSend()->OnNetworkRouteChanged("transport",
1614 new_route);
eladalon413ee9a2017-08-22 04:02:52 -07001615 bitrate_config.start_bitrate_bps = kStartBitrateBps;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001616 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
1617 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07001618 });
1619
Stefan Holmerbe402962016-07-08 16:16:41 +02001620 EXPECT_TRUE(Wait())
1621 << "Timed out while waiting for start bitrate to be exceeded.";
1622
eladalon413ee9a2017-08-22 04:02:52 -07001623 task_queue_->SendTask([this, &new_route, &bitrate_config]() {
1624 bitrate_config.start_bitrate_bps = -1;
1625 bitrate_config.max_bitrate_bps = kNewMaxBitrateBps;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001626 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
1627 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07001628 // TODO(holmer): We should set the last sent packet id here and verify
1629 // that we correctly ignore any packet loss reported prior to that id.
1630 ++new_route.local_network_id;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001631 call_->GetTransportControllerSend()->OnNetworkRouteChanged("transport",
1632 new_route);
eladalon413ee9a2017-08-22 04:02:52 -07001633 EXPECT_GE(call_->GetStats().send_bandwidth_bps, kStartBitrateBps);
1634 });
Stefan Holmerbe402962016-07-08 16:16:41 +02001635 }
1636
1637 private:
eladalon413ee9a2017-08-22 04:02:52 -07001638 test::SingleThreadedTaskQueueForTesting* const task_queue_;
Stefan Holmerbe402962016-07-08 16:16:41 +02001639 Call* call_;
eladalon413ee9a2017-08-22 04:02:52 -07001640 } test(&task_queue_);
Stefan Holmerbe402962016-07-08 16:16:41 +02001641
1642 RunBaseTest(&test);
1643}
1644
michaelt79e05882016-11-08 02:50:09 -08001645TEST_F(VideoSendStreamTest, ChangingTransportOverhead) {
1646 class ChangingTransportOverheadTest : public test::EndToEndTest {
1647 public:
eladalon413ee9a2017-08-22 04:02:52 -07001648 explicit ChangingTransportOverheadTest(
1649 test::SingleThreadedTaskQueueForTesting* task_queue)
michaelt79e05882016-11-08 02:50:09 -08001650 : EndToEndTest(test::CallTest::kDefaultTimeoutMs),
eladalon413ee9a2017-08-22 04:02:52 -07001651 task_queue_(task_queue),
michaelt79e05882016-11-08 02:50:09 -08001652 call_(nullptr),
Erik Språng08127a92016-11-16 16:41:30 +01001653 packets_sent_(0),
1654 transport_overhead_(0) {}
michaelt79e05882016-11-08 02:50:09 -08001655
1656 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1657 call_ = sender_call;
1658 }
1659
1660 Action OnSendRtp(const uint8_t* packet, size_t length) override {
michaelta3328772016-11-29 09:25:03 -08001661 EXPECT_LE(length, kMaxRtpPacketSize);
sprang21253fc2017-02-27 03:35:47 -08001662 rtc::CritScope cs(&lock_);
michaelt79e05882016-11-08 02:50:09 -08001663 if (++packets_sent_ < 100)
1664 return SEND_PACKET;
1665 observation_complete_.Set();
1666 return SEND_PACKET;
1667 }
1668
michaelta3328772016-11-29 09:25:03 -08001669 void ModifyVideoConfigs(
1670 VideoSendStream::Config* send_config,
1671 std::vector<VideoReceiveStream::Config>* receive_configs,
1672 VideoEncoderConfig* encoder_config) override {
1673 send_config->rtp.max_packet_size = kMaxRtpPacketSize;
1674 }
1675
michaelt79e05882016-11-08 02:50:09 -08001676 void PerformTest() override {
eladalon413ee9a2017-08-22 04:02:52 -07001677 task_queue_->SendTask([this]() {
1678 transport_overhead_ = 100;
1679 call_->OnTransportOverheadChanged(webrtc::MediaType::VIDEO,
1680 transport_overhead_);
1681 });
1682
michaelt79e05882016-11-08 02:50:09 -08001683 EXPECT_TRUE(Wait());
eladalon413ee9a2017-08-22 04:02:52 -07001684
sprang21253fc2017-02-27 03:35:47 -08001685 {
1686 rtc::CritScope cs(&lock_);
1687 packets_sent_ = 0;
1688 }
eladalon413ee9a2017-08-22 04:02:52 -07001689
1690 task_queue_->SendTask([this]() {
1691 transport_overhead_ = 500;
1692 call_->OnTransportOverheadChanged(webrtc::MediaType::VIDEO,
1693 transport_overhead_);
1694 });
1695
michaelt79e05882016-11-08 02:50:09 -08001696 EXPECT_TRUE(Wait());
1697 }
1698
1699 private:
eladalon413ee9a2017-08-22 04:02:52 -07001700 test::SingleThreadedTaskQueueForTesting* const task_queue_;
michaelt79e05882016-11-08 02:50:09 -08001701 Call* call_;
sprang21253fc2017-02-27 03:35:47 -08001702 rtc::CriticalSection lock_;
danilchapa37de392017-09-09 04:17:22 -07001703 int packets_sent_ RTC_GUARDED_BY(lock_);
michaelt79e05882016-11-08 02:50:09 -08001704 int transport_overhead_;
michaelta3328772016-11-29 09:25:03 -08001705 const size_t kMaxRtpPacketSize = 1000;
eladalon413ee9a2017-08-22 04:02:52 -07001706 } test(&task_queue_);
michaelt79e05882016-11-08 02:50:09 -08001707
1708 RunBaseTest(&test);
1709}
1710
sprangf24a0642017-02-28 13:23:26 -08001711// Test class takes takes as argument a switch selecting if type switch should
1712// occur and a function pointer to reset the send stream. This is necessary
1713// since you cannot change the content type of a VideoSendStream, you need to
1714// recreate it. Stopping and recreating the stream can only be done on the main
1715// thread and in the context of VideoSendStreamTest (not BaseTest).
1716template <typename T>
sprang9c0b5512016-07-06 00:54:28 -07001717class MaxPaddingSetTest : public test::SendTest {
1718 public:
1719 static const uint32_t kMinTransmitBitrateBps = 400000;
1720 static const uint32_t kActualEncodeBitrateBps = 40000;
1721 static const uint32_t kMinPacketsToSend = 50;
1722
sprangf24a0642017-02-28 13:23:26 -08001723 explicit MaxPaddingSetTest(bool test_switch_content_type, T* stream_reset_fun)
sprang9c0b5512016-07-06 00:54:28 -07001724 : SendTest(test::CallTest::kDefaultTimeoutMs),
sprangf24a0642017-02-28 13:23:26 -08001725 content_switch_event_(false, false),
sprang9c0b5512016-07-06 00:54:28 -07001726 call_(nullptr),
1727 send_stream_(nullptr),
sprangf24a0642017-02-28 13:23:26 -08001728 send_stream_config_(nullptr),
sprang9c0b5512016-07-06 00:54:28 -07001729 packets_sent_(0),
sprangf24a0642017-02-28 13:23:26 -08001730 running_without_padding_(test_switch_content_type),
tommi39e12892017-03-13 05:15:14 -07001731 stream_resetter_(stream_reset_fun) {
1732 RTC_DCHECK(stream_resetter_);
1733 }
sprang9c0b5512016-07-06 00:54:28 -07001734
1735 void OnVideoStreamsCreated(
1736 VideoSendStream* send_stream,
1737 const std::vector<VideoReceiveStream*>& receive_streams) override {
sprangf24a0642017-02-28 13:23:26 -08001738 rtc::CritScope lock(&crit_);
sprang9c0b5512016-07-06 00:54:28 -07001739 send_stream_ = send_stream;
1740 }
1741
1742 void ModifyVideoConfigs(
1743 VideoSendStream::Config* send_config,
1744 std::vector<VideoReceiveStream::Config>* receive_configs,
1745 VideoEncoderConfig* encoder_config) override {
kwibergaf476c72016-11-28 15:21:39 -08001746 RTC_DCHECK_EQ(1, encoder_config->number_of_streams);
sprangf24a0642017-02-28 13:23:26 -08001747 if (RunningWithoutPadding()) {
sprang9c0b5512016-07-06 00:54:28 -07001748 encoder_config->min_transmit_bitrate_bps = 0;
1749 encoder_config->content_type =
1750 VideoEncoderConfig::ContentType::kRealtimeVideo;
1751 } else {
1752 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
1753 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
1754 }
sprangf24a0642017-02-28 13:23:26 -08001755 send_stream_config_ = send_config->Copy();
perkj26091b12016-09-01 01:17:40 -07001756 encoder_config_ = encoder_config->Copy();
sprang9c0b5512016-07-06 00:54:28 -07001757 }
1758
1759 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
1760 call_ = sender_call;
1761 }
1762
1763 Action OnSendRtp(const uint8_t* packet, size_t length) override {
1764 rtc::CritScope lock(&crit_);
sprang9c0b5512016-07-06 00:54:28 -07001765 if (running_without_padding_)
1766 EXPECT_EQ(0, call_->GetStats().max_padding_bitrate_bps);
1767
1768 // Wait until at least kMinPacketsToSend frames have been encoded, so that
1769 // we have reliable data.
1770 if (++packets_sent_ < kMinPacketsToSend)
1771 return SEND_PACKET;
1772
1773 if (running_without_padding_) {
1774 // We've sent kMinPacketsToSend packets with default configuration, switch
1775 // to enabling screen content and setting min transmit bitrate.
sprangf24a0642017-02-28 13:23:26 -08001776 // Note that we need to recreate the stream if changing content type.
sprang9c0b5512016-07-06 00:54:28 -07001777 packets_sent_ = 0;
1778 encoder_config_.min_transmit_bitrate_bps = kMinTransmitBitrateBps;
1779 encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
sprang9c0b5512016-07-06 00:54:28 -07001780 running_without_padding_ = false;
sprangf24a0642017-02-28 13:23:26 -08001781 content_switch_event_.Set();
sprang9c0b5512016-07-06 00:54:28 -07001782 return SEND_PACKET;
1783 }
1784
1785 // Make sure the pacer has been configured with a min transmit bitrate.
1786 if (call_->GetStats().max_padding_bitrate_bps > 0)
1787 observation_complete_.Set();
1788
1789 return SEND_PACKET;
1790 }
1791
1792 void PerformTest() override {
sprangf24a0642017-02-28 13:23:26 -08001793 if (RunningWithoutPadding()) {
1794 ASSERT_TRUE(
1795 content_switch_event_.Wait(test::CallTest::kDefaultTimeoutMs));
sprangf24a0642017-02-28 13:23:26 -08001796 (*stream_resetter_)(send_stream_config_, encoder_config_);
1797 }
1798
sprang9c0b5512016-07-06 00:54:28 -07001799 ASSERT_TRUE(Wait()) << "Timed out waiting for a valid padding bitrate.";
1800 }
1801
1802 private:
sprangf24a0642017-02-28 13:23:26 -08001803 bool RunningWithoutPadding() const {
1804 rtc::CritScope lock(&crit_);
1805 return running_without_padding_;
1806 }
1807
sprang9c0b5512016-07-06 00:54:28 -07001808 rtc::CriticalSection crit_;
sprangf24a0642017-02-28 13:23:26 -08001809 rtc::Event content_switch_event_;
sprang9c0b5512016-07-06 00:54:28 -07001810 Call* call_;
danilchapa37de392017-09-09 04:17:22 -07001811 VideoSendStream* send_stream_ RTC_GUARDED_BY(crit_);
sprangf24a0642017-02-28 13:23:26 -08001812 VideoSendStream::Config send_stream_config_;
sprang9c0b5512016-07-06 00:54:28 -07001813 VideoEncoderConfig encoder_config_;
danilchapa37de392017-09-09 04:17:22 -07001814 uint32_t packets_sent_ RTC_GUARDED_BY(crit_);
sprang9c0b5512016-07-06 00:54:28 -07001815 bool running_without_padding_;
sprangf24a0642017-02-28 13:23:26 -08001816 T* const stream_resetter_;
sprang9c0b5512016-07-06 00:54:28 -07001817};
1818
1819TEST_F(VideoSendStreamTest, RespectsMinTransmitBitrate) {
sprangf24a0642017-02-28 13:23:26 -08001820 auto reset_fun = [](const VideoSendStream::Config& send_stream_config,
1821 const VideoEncoderConfig& encoder_config) {};
1822 MaxPaddingSetTest<decltype(reset_fun)> test(false, &reset_fun);
sprang9c0b5512016-07-06 00:54:28 -07001823 RunBaseTest(&test);
1824}
1825
1826TEST_F(VideoSendStreamTest, RespectsMinTransmitBitrateAfterContentSwitch) {
sprangf24a0642017-02-28 13:23:26 -08001827 // Function for removing and recreating the send stream with a new config.
1828 auto reset_fun = [this](const VideoSendStream::Config& send_stream_config,
1829 const VideoEncoderConfig& encoder_config) {
eladalon413ee9a2017-08-22 04:02:52 -07001830 task_queue_.SendTask([this, &send_stream_config, &encoder_config]() {
1831 Stop();
1832 sender_call_->DestroyVideoSendStream(video_send_stream_);
1833 video_send_config_ = send_stream_config.Copy();
1834 video_encoder_config_ = encoder_config.Copy();
1835 video_send_stream_ = sender_call_->CreateVideoSendStream(
1836 video_send_config_.Copy(), video_encoder_config_.Copy());
1837 video_send_stream_->SetSource(
1838 frame_generator_capturer_.get(),
1839 VideoSendStream::DegradationPreference::kMaintainResolution);
1840 Start();
1841 });
sprangf24a0642017-02-28 13:23:26 -08001842 };
1843 MaxPaddingSetTest<decltype(reset_fun)> test(true, &reset_fun);
sprang9c0b5512016-07-06 00:54:28 -07001844 RunBaseTest(&test);
1845}
1846
perkjfa10b552016-10-02 23:45:26 -07001847// This test verifies that new frame sizes reconfigures encoders even though not
1848// (yet) sending. The purpose of this is to permit encoding as quickly as
1849// possible once we start sending. Likely the frames being input are from the
1850// same source that will be sent later, which just means that we're ready
1851// earlier.
1852TEST_F(VideoSendStreamTest,
1853 EncoderReconfigureOnResolutionChangeWhenNotSending) {
1854 class EncoderObserver : public test::FakeEncoder {
1855 public:
1856 EncoderObserver()
1857 : FakeEncoder(Clock::GetRealTimeClock()),
1858 init_encode_called_(false, false),
1859 number_of_initializations_(0),
1860 last_initialized_frame_width_(0),
1861 last_initialized_frame_height_(0) {}
1862
1863 void WaitForResolution(int width, int height) {
1864 {
1865 rtc::CritScope lock(&crit_);
1866 if (last_initialized_frame_width_ == width &&
1867 last_initialized_frame_height_ == height) {
1868 return;
1869 }
1870 }
Erik Språng08127a92016-11-16 16:41:30 +01001871 EXPECT_TRUE(
1872 init_encode_called_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
perkjfa10b552016-10-02 23:45:26 -07001873 {
1874 rtc::CritScope lock(&crit_);
1875 EXPECT_EQ(width, last_initialized_frame_width_);
1876 EXPECT_EQ(height, last_initialized_frame_height_);
1877 }
1878 }
1879
1880 private:
1881 int32_t InitEncode(const VideoCodec* config,
1882 int32_t number_of_cores,
1883 size_t max_payload_size) override {
1884 rtc::CritScope lock(&crit_);
1885 last_initialized_frame_width_ = config->width;
1886 last_initialized_frame_height_ = config->height;
1887 ++number_of_initializations_;
Erik Språng08127a92016-11-16 16:41:30 +01001888 init_encode_called_.Set();
perkjfa10b552016-10-02 23:45:26 -07001889 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
1890 }
1891
1892 int32_t Encode(const VideoFrame& input_image,
1893 const CodecSpecificInfo* codec_specific_info,
1894 const std::vector<FrameType>* frame_types) override {
1895 ADD_FAILURE()
1896 << "Unexpected Encode call since the send stream is not started";
1897 return 0;
1898 }
1899
1900 rtc::CriticalSection crit_;
1901 rtc::Event init_encode_called_;
danilchapa37de392017-09-09 04:17:22 -07001902 size_t number_of_initializations_ RTC_GUARDED_BY(&crit_);
1903 int last_initialized_frame_width_ RTC_GUARDED_BY(&crit_);
1904 int last_initialized_frame_height_ RTC_GUARDED_BY(&crit_);
perkjfa10b552016-10-02 23:45:26 -07001905 };
1906
perkjfa10b552016-10-02 23:45:26 -07001907 test::NullTransport transport;
perkjfa10b552016-10-02 23:45:26 -07001908 EncoderObserver encoder;
eladalon413ee9a2017-08-22 04:02:52 -07001909
1910 task_queue_.SendTask([this, &transport, &encoder]() {
1911 CreateSenderCall(Call::Config(event_log_.get()));
1912 CreateSendConfig(1, 0, 0, &transport);
1913 video_send_config_.encoder_settings.encoder = &encoder;
1914 CreateVideoStreams();
1915 CreateFrameGeneratorCapturer(kDefaultFramerate, kDefaultWidth,
1916 kDefaultHeight);
1917 frame_generator_capturer_->Start();
1918 });
perkjfa10b552016-10-02 23:45:26 -07001919
1920 encoder.WaitForResolution(kDefaultWidth, kDefaultHeight);
eladalon413ee9a2017-08-22 04:02:52 -07001921
1922 task_queue_.SendTask([this]() {
1923 frame_generator_capturer_->ChangeResolution(kDefaultWidth * 2,
1924 kDefaultHeight * 2);
1925 });
1926
perkjfa10b552016-10-02 23:45:26 -07001927 encoder.WaitForResolution(kDefaultWidth * 2, kDefaultHeight * 2);
eladalon413ee9a2017-08-22 04:02:52 -07001928
1929 task_queue_.SendTask([this]() {
1930 DestroyStreams();
1931 DestroyCalls();
1932 });
perkjfa10b552016-10-02 23:45:26 -07001933}
1934
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001935TEST_F(VideoSendStreamTest, CanReconfigureToUseStartBitrateAbovePreviousMax) {
1936 class StartBitrateObserver : public test::FakeEncoder {
1937 public:
1938 StartBitrateObserver()
pbos14fe7082016-04-20 06:35:56 -07001939 : FakeEncoder(Clock::GetRealTimeClock()),
1940 start_bitrate_changed_(false, false),
1941 start_bitrate_kbps_(0) {}
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001942 int32_t InitEncode(const VideoCodec* config,
1943 int32_t number_of_cores,
1944 size_t max_payload_size) override {
1945 rtc::CritScope lock(&crit_);
1946 start_bitrate_kbps_ = config->startBitrate;
pbos14fe7082016-04-20 06:35:56 -07001947 start_bitrate_changed_.Set();
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001948 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
1949 }
1950
1951 int32_t SetRates(uint32_t new_target_bitrate, uint32_t framerate) override {
1952 rtc::CritScope lock(&crit_);
1953 start_bitrate_kbps_ = new_target_bitrate;
pbos14fe7082016-04-20 06:35:56 -07001954 start_bitrate_changed_.Set();
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001955 return FakeEncoder::SetRates(new_target_bitrate, framerate);
1956 }
1957
1958 int GetStartBitrateKbps() const {
1959 rtc::CritScope lock(&crit_);
1960 return start_bitrate_kbps_;
1961 }
1962
pbos14fe7082016-04-20 06:35:56 -07001963 bool WaitForStartBitrate() {
1964 return start_bitrate_changed_.Wait(
1965 VideoSendStreamTest::kDefaultTimeoutMs);
1966 }
1967
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001968 private:
pbos5ad935c2016-01-25 03:52:44 -08001969 rtc::CriticalSection crit_;
pbos14fe7082016-04-20 06:35:56 -07001970 rtc::Event start_bitrate_changed_;
danilchapa37de392017-09-09 04:17:22 -07001971 int start_bitrate_kbps_ RTC_GUARDED_BY(crit_);
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001972 };
1973
philipel4fb651d2017-04-10 03:54:05 -07001974 CreateSenderCall(Call::Config(event_log_.get()));
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001975
solenberg4fbae2b2015-08-28 04:07:10 -07001976 test::NullTransport transport;
brandtr841de6a2016-11-15 07:10:52 -08001977 CreateSendConfig(1, 0, 0, &transport);
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001978
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01001979 BitrateConstraints bitrate_config;
perkjfa10b552016-10-02 23:45:26 -07001980 bitrate_config.start_bitrate_bps = 2 * video_encoder_config_.max_bitrate_bps;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001981 sender_call_->GetTransportControllerSend()->SetSdpBitrateParameters(
1982 bitrate_config);
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001983
1984 StartBitrateObserver encoder;
stefanff483612015-12-21 03:14:00 -08001985 video_send_config_.encoder_settings.encoder = &encoder;
perkjfa10b552016-10-02 23:45:26 -07001986 // Since this test does not use a capturer, set |internal_source| = true.
1987 // Encoder configuration is otherwise updated on the next video frame.
1988 video_send_config_.encoder_settings.internal_source = true;
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001989
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001990 CreateVideoStreams();
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001991
pbos14fe7082016-04-20 06:35:56 -07001992 EXPECT_TRUE(encoder.WaitForStartBitrate());
perkjfa10b552016-10-02 23:45:26 -07001993 EXPECT_EQ(video_encoder_config_.max_bitrate_bps / 1000,
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001994 encoder.GetStartBitrateKbps());
1995
perkjfa10b552016-10-02 23:45:26 -07001996 video_encoder_config_.max_bitrate_bps = 2 * bitrate_config.start_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -07001997 video_send_stream_->ReconfigureVideoEncoder(video_encoder_config_.Copy());
pbos@webrtc.org143451d2015-03-18 14:40:03 +00001998
1999 // New bitrate should be reconfigured above the previous max. As there's no
2000 // network connection this shouldn't be flaky, as no bitrate should've been
2001 // reported in between.
pbos14fe7082016-04-20 06:35:56 -07002002 EXPECT_TRUE(encoder.WaitForStartBitrate());
pbos@webrtc.org143451d2015-03-18 14:40:03 +00002003 EXPECT_EQ(bitrate_config.start_bitrate_bps / 1000,
2004 encoder.GetStartBitrateKbps());
2005
2006 DestroyStreams();
2007}
2008
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002009class StartStopBitrateObserver : public test::FakeEncoder {
2010 public:
2011 StartStopBitrateObserver()
2012 : FakeEncoder(Clock::GetRealTimeClock()),
2013 encoder_init_(false, false),
2014 bitrate_changed_(false, false) {}
2015 int32_t InitEncode(const VideoCodec* config,
2016 int32_t number_of_cores,
2017 size_t max_payload_size) override {
2018 rtc::CritScope lock(&crit_);
2019 encoder_init_.Set();
2020 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
2021 }
2022
2023 int32_t SetRateAllocation(const BitrateAllocation& bitrate,
2024 uint32_t framerate) override {
2025 rtc::CritScope lock(&crit_);
2026 bitrate_kbps_ = bitrate.get_sum_kbps();
2027 bitrate_changed_.Set();
2028 return FakeEncoder::SetRateAllocation(bitrate, framerate);
2029 }
2030
2031 bool WaitForEncoderInit() {
2032 return encoder_init_.Wait(VideoSendStreamTest::kDefaultTimeoutMs);
2033 }
2034
2035 bool WaitBitrateChanged(bool non_zero) {
2036 do {
2037 rtc::Optional<int> bitrate_kbps;
2038 {
2039 rtc::CritScope lock(&crit_);
2040 bitrate_kbps = bitrate_kbps_;
2041 }
2042 if (!bitrate_kbps)
2043 continue;
2044
2045 if ((non_zero && *bitrate_kbps > 0) ||
2046 (!non_zero && *bitrate_kbps == 0)) {
2047 return true;
2048 }
2049 } while (bitrate_changed_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
2050 return false;
2051 }
2052
2053 private:
2054 rtc::CriticalSection crit_;
2055 rtc::Event encoder_init_;
2056 rtc::Event bitrate_changed_;
2057 rtc::Optional<int> bitrate_kbps_ RTC_GUARDED_BY(crit_);
2058};
2059
perkj57c21f92016-06-17 07:27:16 -07002060// This test that if the encoder use an internal source, VideoEncoder::SetRates
2061// will be called with zero bitrate during initialization and that
2062// VideoSendStream::Stop also triggers VideoEncoder::SetRates Start to be called
2063// with zero bitrate.
2064TEST_F(VideoSendStreamTest, VideoSendStreamStopSetEncoderRateToZero) {
perkj57c21f92016-06-17 07:27:16 -07002065 test::NullTransport transport;
perkj57c21f92016-06-17 07:27:16 -07002066 StartStopBitrateObserver encoder;
perkj57c21f92016-06-17 07:27:16 -07002067
eladalon413ee9a2017-08-22 04:02:52 -07002068 task_queue_.SendTask([this, &transport, &encoder]() {
2069 CreateSenderCall(Call::Config(event_log_.get()));
2070 CreateSendConfig(1, 0, 0, &transport);
2071
2072 sender_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
2073
2074 video_send_config_.encoder_settings.encoder = &encoder;
2075 video_send_config_.encoder_settings.internal_source = true;
2076
2077 CreateVideoStreams();
2078 });
perkj57c21f92016-06-17 07:27:16 -07002079
2080 EXPECT_TRUE(encoder.WaitForEncoderInit());
Erik Språng08127a92016-11-16 16:41:30 +01002081
eladalon413ee9a2017-08-22 04:02:52 -07002082 task_queue_.SendTask([this]() {
2083 video_send_stream_->Start();
2084 });
Erik Språng08127a92016-11-16 16:41:30 +01002085 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
2086
eladalon413ee9a2017-08-22 04:02:52 -07002087 task_queue_.SendTask([this]() {
2088 video_send_stream_->Stop();
2089 });
Erik Språng08127a92016-11-16 16:41:30 +01002090 EXPECT_TRUE(encoder.WaitBitrateChanged(false));
2091
eladalon413ee9a2017-08-22 04:02:52 -07002092 task_queue_.SendTask([this]() {
2093 video_send_stream_->Start();
2094 });
Erik Språng08127a92016-11-16 16:41:30 +01002095 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
perkj57c21f92016-06-17 07:27:16 -07002096
eladalon413ee9a2017-08-22 04:02:52 -07002097 task_queue_.SendTask([this]() {
2098 DestroyStreams();
2099 DestroyCalls();
2100 });
perkj57c21f92016-06-17 07:27:16 -07002101}
2102
Seth Hampsoncc7125f2018-02-02 08:46:16 -08002103// Tests that when the encoder uses an internal source, the VideoEncoder will
2104// be updated with a new bitrate when turning the VideoSendStream on/off with
2105// VideoSendStream::UpdateActiveSimulcastLayers, and when the VideoStreamEncoder
2106// is reconfigured with new active layers.
2107TEST_F(VideoSendStreamTest, VideoSendStreamUpdateActiveSimulcastLayers) {
2108 test::NullTransport transport;
2109 StartStopBitrateObserver encoder;
2110
2111 task_queue_.SendTask([this, &transport, &encoder]() {
2112 CreateSenderCall(Call::Config(event_log_.get()));
2113 // Create two simulcast streams.
2114 CreateSendConfig(2, 0, 0, &transport);
2115
2116 sender_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
2117
2118 video_send_config_.encoder_settings.encoder = &encoder;
2119 video_send_config_.encoder_settings.internal_source = true;
2120 video_send_config_.encoder_settings.payload_name = "VP8";
2121
2122 CreateVideoStreams();
2123 });
2124
2125 EXPECT_TRUE(encoder.WaitForEncoderInit());
2126
2127 // When we turn on the simulcast layers it will update the BitrateAllocator,
2128 // which in turn updates the VideoEncoder's bitrate.
2129 task_queue_.SendTask([this]() {
2130 video_send_stream_->UpdateActiveSimulcastLayers({true, true});
2131 });
2132 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
2133
2134 video_encoder_config_.simulcast_layers[0].active = true;
2135 video_encoder_config_.simulcast_layers[1].active = false;
2136 task_queue_.SendTask([this]() {
2137 video_send_stream_->ReconfigureVideoEncoder(video_encoder_config_.Copy());
2138 });
2139 // TODO(bugs.webrtc.org/8807): Currently we require a hard reconfiguration to
2140 // update the VideoBitrateAllocator and BitrateAllocator of which layers are
2141 // active. Once the change is made for a "soft" reconfiguration we can remove
2142 // the expecation for an encoder init. We can also test that bitrate changes
2143 // when just updating individual active layers, which should change the
2144 // bitrate set to the video encoder.
2145 EXPECT_TRUE(encoder.WaitForEncoderInit());
2146 EXPECT_TRUE(encoder.WaitBitrateChanged(true));
2147
2148 // Turning off both simulcast layers should trigger a bitrate change of 0.
2149 video_encoder_config_.simulcast_layers[0].active = false;
2150 video_encoder_config_.simulcast_layers[1].active = false;
2151 task_queue_.SendTask([this]() {
2152 video_send_stream_->UpdateActiveSimulcastLayers({false, false});
2153 });
2154 EXPECT_TRUE(encoder.WaitBitrateChanged(false));
2155
2156 task_queue_.SendTask([this]() {
2157 DestroyStreams();
2158 DestroyCalls();
2159 });
2160}
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002161TEST_F(VideoSendStreamTest, CapturesTextureAndVideoFrames) {
nissed30a1112016-04-18 05:15:22 -07002162 class FrameObserver : public rtc::VideoSinkInterface<VideoFrame> {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002163 public:
Peter Boström5811a392015-12-10 13:02:50 +01002164 FrameObserver() : output_frame_event_(false, false) {}
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002165
nissed30a1112016-04-18 05:15:22 -07002166 void OnFrame(const VideoFrame& video_frame) override {
2167 output_frames_.push_back(video_frame);
Peter Boström5811a392015-12-10 13:02:50 +01002168 output_frame_event_.Set();
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002169 }
2170
2171 void WaitOutputFrame() {
Peter Boström5811a392015-12-10 13:02:50 +01002172 const int kWaitFrameTimeoutMs = 3000;
2173 EXPECT_TRUE(output_frame_event_.Wait(kWaitFrameTimeoutMs))
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002174 << "Timeout while waiting for output frames.";
2175 }
2176
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002177 const std::vector<VideoFrame>& output_frames() const {
magjed@webrtc.org2d2a30c2015-03-24 12:37:36 +00002178 return output_frames_;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002179 }
2180
2181 private:
2182 // Delivered output frames.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002183 std::vector<VideoFrame> output_frames_;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002184
2185 // Indicate an output frame has arrived.
Peter Boström5811a392015-12-10 13:02:50 +01002186 rtc::Event output_frame_event_;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002187 };
2188
solenberg4fbae2b2015-08-28 04:07:10 -07002189 test::NullTransport transport;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002190 FrameObserver observer;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002191 std::vector<VideoFrame> input_frames;
perkjfa10b552016-10-02 23:45:26 -07002192
eladalon413ee9a2017-08-22 04:02:52 -07002193 task_queue_.SendTask([this, &transport, &observer, &input_frames]() {
2194 // Initialize send stream.
2195 CreateSenderCall(Call::Config(event_log_.get()));
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002196
eladalon413ee9a2017-08-22 04:02:52 -07002197 CreateSendConfig(1, 0, 0, &transport);
2198 video_send_config_.pre_encode_callback = &observer;
2199 CreateVideoStreams();
2200
2201 // Prepare five input frames. Send ordinary VideoFrame and texture frames
2202 // alternatively.
2203 int width = 168;
2204 int height = 132;
2205
2206 input_frames.push_back(test::FakeNativeBuffer::CreateFrame(
2207 width, height, 1, 1, kVideoRotation_0));
2208 input_frames.push_back(test::FakeNativeBuffer::CreateFrame(
2209 width, height, 2, 2, kVideoRotation_0));
2210 input_frames.push_back(CreateVideoFrame(width, height, 3));
2211 input_frames.push_back(CreateVideoFrame(width, height, 4));
2212 input_frames.push_back(test::FakeNativeBuffer::CreateFrame(
2213 width, height, 5, 5, kVideoRotation_0));
2214
2215 video_send_stream_->Start();
2216 test::FrameForwarder forwarder;
2217 video_send_stream_->SetSource(
2218 &forwarder, VideoSendStream::DegradationPreference::kMaintainFramerate);
2219 for (size_t i = 0; i < input_frames.size(); i++) {
2220 forwarder.IncomingCapturedFrame(input_frames[i]);
2221 // Wait until the output frame is received before sending the next input
2222 // frame. Or the previous input frame may be replaced without delivering.
2223 observer.WaitOutputFrame();
2224 }
2225 video_send_stream_->Stop();
2226 video_send_stream_->SetSource(
2227 nullptr, VideoSendStream::DegradationPreference::kMaintainFramerate);
2228 });
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002229
2230 // Test if the input and output frames are the same. render_time_ms and
2231 // timestamp are not compared because capturer sets those values.
magjed@webrtc.org2d2a30c2015-03-24 12:37:36 +00002232 ExpectEqualFramesVector(input_frames, observer.output_frames());
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002233
eladalon413ee9a2017-08-22 04:02:52 -07002234 task_queue_.SendTask([this]() {
2235 DestroyStreams();
2236 DestroyCalls();
2237 });
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002238}
2239
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002240void ExpectEqualFramesVector(const std::vector<VideoFrame>& frames1,
2241 const std::vector<VideoFrame>& frames2) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002242 EXPECT_EQ(frames1.size(), frames2.size());
2243 for (size_t i = 0; i < std::min(frames1.size(), frames2.size()); ++i)
nisse26acec42016-04-15 03:43:39 -07002244 // Compare frame buffers, since we don't care about differing timestamps.
2245 EXPECT_TRUE(test::FrameBufsEqual(frames1[i].video_frame_buffer(),
2246 frames2[i].video_frame_buffer()));
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002247}
2248
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002249VideoFrame CreateVideoFrame(int width, int height, uint8_t data) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002250 const int kSizeY = width * height * 2;
kwiberg27f982b2016-03-01 11:52:33 -08002251 std::unique_ptr<uint8_t[]> buffer(new uint8_t[kSizeY]);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002252 memset(buffer.get(), data, kSizeY);
Magnus Jedvert90e31902017-06-07 11:32:50 +02002253 VideoFrame frame(I420Buffer::Create(width, height), kVideoRotation_0, data);
magjed@webrtc.org2d2a30c2015-03-24 12:37:36 +00002254 frame.set_timestamp(data);
nisse1c0dea82017-01-30 02:43:18 -08002255 // Use data as a ms timestamp.
2256 frame.set_timestamp_us(data * rtc::kNumMicrosecsPerMillisec);
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +00002257 return frame;
2258}
2259
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002260TEST_F(VideoSendStreamTest, EncoderIsProperlyInitializedAndDestroyed) {
2261 class EncoderStateObserver : public test::SendTest, public VideoEncoder {
2262 public:
eladalon413ee9a2017-08-22 04:02:52 -07002263 explicit EncoderStateObserver(
2264 test::SingleThreadedTaskQueueForTesting* task_queue)
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002265 : SendTest(kDefaultTimeoutMs),
eladalon413ee9a2017-08-22 04:02:52 -07002266 task_queue_(task_queue),
Erik Språng737336d2016-07-29 12:59:36 +02002267 stream_(nullptr),
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002268 initialized_(false),
2269 callback_registered_(false),
2270 num_releases_(0),
2271 released_(false) {}
2272
2273 bool IsReleased() {
Peter Boströmf2f82832015-05-01 13:00:41 +02002274 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002275 return released_;
2276 }
2277
2278 bool IsReadyForEncode() {
Peter Boströmf2f82832015-05-01 13:00:41 +02002279 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002280 return initialized_ && callback_registered_;
2281 }
2282
2283 size_t num_releases() {
Peter Boströmf2f82832015-05-01 13:00:41 +02002284 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002285 return num_releases_;
2286 }
2287
2288 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002289 int32_t InitEncode(const VideoCodec* codecSettings,
2290 int32_t numberOfCores,
2291 size_t maxPayloadSize) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02002292 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002293 EXPECT_FALSE(initialized_);
2294 initialized_ = true;
2295 released_ = false;
2296 return 0;
2297 }
2298
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002299 int32_t Encode(const VideoFrame& inputImage,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002300 const CodecSpecificInfo* codecSpecificInfo,
pbos22993e12015-10-19 02:39:06 -07002301 const std::vector<FrameType>* frame_types) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002302 EXPECT_TRUE(IsReadyForEncode());
2303
Peter Boström5811a392015-12-10 13:02:50 +01002304 observation_complete_.Set();
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002305 return 0;
2306 }
2307
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002308 int32_t RegisterEncodeCompleteCallback(
2309 EncodedImageCallback* callback) override {
Peter Boströmf2f82832015-05-01 13:00:41 +02002310 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002311 EXPECT_TRUE(initialized_);
2312 callback_registered_ = true;
2313 return 0;
2314 }
2315
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002316 int32_t Release() override {
Peter Boströmf2f82832015-05-01 13:00:41 +02002317 rtc::CritScope lock(&crit_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002318 EXPECT_TRUE(IsReadyForEncode());
2319 EXPECT_FALSE(released_);
2320 initialized_ = false;
2321 callback_registered_ = false;
2322 released_ = true;
2323 ++num_releases_;
2324 return 0;
2325 }
2326
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002327 int32_t SetChannelParameters(uint32_t packetLoss, int64_t rtt) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002328 EXPECT_TRUE(IsReadyForEncode());
2329 return 0;
2330 }
2331
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002332 int32_t SetRates(uint32_t newBitRate, uint32_t frameRate) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002333 EXPECT_TRUE(IsReadyForEncode());
2334 return 0;
2335 }
2336
stefanff483612015-12-21 03:14:00 -08002337 void OnVideoStreamsCreated(
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002338 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002339 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002340 stream_ = send_stream;
2341 }
2342
stefanff483612015-12-21 03:14:00 -08002343 void ModifyVideoConfigs(
2344 VideoSendStream::Config* send_config,
2345 std::vector<VideoReceiveStream::Config>* receive_configs,
2346 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002347 send_config->encoder_settings.encoder = this;
perkj26091b12016-09-01 01:17:40 -07002348 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002349 }
2350
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002351 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01002352 EXPECT_TRUE(Wait()) << "Timed out while waiting for Encode.";
eladalon413ee9a2017-08-22 04:02:52 -07002353
2354 task_queue_->SendTask([this]() {
2355 EXPECT_EQ(0u, num_releases());
2356 stream_->ReconfigureVideoEncoder(std::move(encoder_config_));
2357 EXPECT_EQ(0u, num_releases());
2358 stream_->Stop();
2359 // Encoder should not be released before destroying the VideoSendStream.
2360 EXPECT_FALSE(IsReleased());
2361 EXPECT_TRUE(IsReadyForEncode());
2362 stream_->Start();
2363 });
2364
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002365 // Sanity check, make sure we still encode frames with this encoder.
Peter Boström5811a392015-12-10 13:02:50 +01002366 EXPECT_TRUE(Wait()) << "Timed out while waiting for Encode.";
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002367 }
2368
eladalon413ee9a2017-08-22 04:02:52 -07002369 test::SingleThreadedTaskQueueForTesting* const task_queue_;
Peter Boströmf2f82832015-05-01 13:00:41 +02002370 rtc::CriticalSection crit_;
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002371 VideoSendStream* stream_;
danilchapa37de392017-09-09 04:17:22 -07002372 bool initialized_ RTC_GUARDED_BY(crit_);
2373 bool callback_registered_ RTC_GUARDED_BY(crit_);
2374 size_t num_releases_ RTC_GUARDED_BY(crit_);
2375 bool released_ RTC_GUARDED_BY(crit_);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002376 VideoEncoderConfig encoder_config_;
eladalon413ee9a2017-08-22 04:02:52 -07002377 } test_encoder(&task_queue_);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002378
stefane74eef12016-01-08 06:47:13 -08002379 RunBaseTest(&test_encoder);
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002380
2381 EXPECT_TRUE(test_encoder.IsReleased());
Per21d45d22016-10-30 21:37:57 +01002382 EXPECT_EQ(1u, test_encoder.num_releases());
pbos@webrtc.org161f8082014-07-07 14:22:35 +00002383}
2384
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002385TEST_F(VideoSendStreamTest, EncoderSetupPropagatesCommonEncoderConfigValues) {
2386 class VideoCodecConfigObserver : public test::SendTest,
2387 public test::FakeEncoder {
2388 public:
2389 VideoCodecConfigObserver()
2390 : SendTest(kDefaultTimeoutMs),
2391 FakeEncoder(Clock::GetRealTimeClock()),
pbos14fe7082016-04-20 06:35:56 -07002392 init_encode_event_(false, false),
Erik Språng737336d2016-07-29 12:59:36 +02002393 num_initializations_(0),
2394 stream_(nullptr) {}
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002395
2396 private:
stefanff483612015-12-21 03:14:00 -08002397 void ModifyVideoConfigs(
2398 VideoSendStream::Config* send_config,
2399 std::vector<VideoReceiveStream::Config>* receive_configs,
2400 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002401 send_config->encoder_settings.encoder = this;
sprangf24a0642017-02-28 13:23:26 -08002402 encoder_config->max_bitrate_bps = kFirstMaxBitrateBps;
perkj26091b12016-09-01 01:17:40 -07002403 encoder_config_ = encoder_config->Copy();
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002404 }
2405
stefanff483612015-12-21 03:14:00 -08002406 void OnVideoStreamsCreated(
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002407 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002408 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002409 stream_ = send_stream;
2410 }
2411
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002412 int32_t InitEncode(const VideoCodec* config,
2413 int32_t number_of_cores,
2414 size_t max_payload_size) override {
Per21d45d22016-10-30 21:37:57 +01002415 if (num_initializations_ == 0) {
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002416 // Verify default values.
sprangf24a0642017-02-28 13:23:26 -08002417 EXPECT_EQ(kFirstMaxBitrateBps / 1000, config->maxBitrate);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002418 } else {
2419 // Verify that changed values are propagated.
sprangf24a0642017-02-28 13:23:26 -08002420 EXPECT_EQ(kSecondMaxBitrateBps / 1000, config->maxBitrate);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002421 }
2422 ++num_initializations_;
Per21d45d22016-10-30 21:37:57 +01002423 init_encode_event_.Set();
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002424 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
2425 }
2426
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002427 void PerformTest() override {
pbos14fe7082016-04-20 06:35:56 -07002428 EXPECT_TRUE(init_encode_event_.Wait(kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002429 EXPECT_EQ(1u, num_initializations_) << "VideoEncoder not initialized.";
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002430
sprangf24a0642017-02-28 13:23:26 -08002431 encoder_config_.max_bitrate_bps = kSecondMaxBitrateBps;
perkj26091b12016-09-01 01:17:40 -07002432 stream_->ReconfigureVideoEncoder(std::move(encoder_config_));
pbos14fe7082016-04-20 06:35:56 -07002433 EXPECT_TRUE(init_encode_event_.Wait(kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002434 EXPECT_EQ(2u, num_initializations_)
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002435 << "ReconfigureVideoEncoder did not reinitialize the encoder with "
2436 "new encoder settings.";
2437 }
2438
sprangf24a0642017-02-28 13:23:26 -08002439 const uint32_t kFirstMaxBitrateBps = 1000000;
2440 const uint32_t kSecondMaxBitrateBps = 2000000;
2441
pbos14fe7082016-04-20 06:35:56 -07002442 rtc::Event init_encode_event_;
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002443 size_t num_initializations_;
2444 VideoSendStream* stream_;
2445 VideoEncoderConfig encoder_config_;
2446 } test;
2447
stefane74eef12016-01-08 06:47:13 -08002448 RunBaseTest(&test);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00002449}
2450
Peter Boström53eda3d2015-03-27 15:53:18 +01002451static const size_t kVideoCodecConfigObserverNumberOfTemporalLayers = 4;
2452template <typename T>
2453class VideoCodecConfigObserver : public test::SendTest,
2454 public test::FakeEncoder {
Peter Boström53eda3d2015-03-27 15:53:18 +01002455 public:
2456 VideoCodecConfigObserver(VideoCodecType video_codec_type,
2457 const char* codec_name)
2458 : SendTest(VideoSendStreamTest::kDefaultTimeoutMs),
2459 FakeEncoder(Clock::GetRealTimeClock()),
2460 video_codec_type_(video_codec_type),
2461 codec_name_(codec_name),
pbos14fe7082016-04-20 06:35:56 -07002462 init_encode_event_(false, false),
Erik Språng737336d2016-07-29 12:59:36 +02002463 num_initializations_(0),
2464 stream_(nullptr) {
Peter Boström53eda3d2015-03-27 15:53:18 +01002465 memset(&encoder_settings_, 0, sizeof(encoder_settings_));
2466 }
2467
2468 private:
perkjfa10b552016-10-02 23:45:26 -07002469 class VideoStreamFactory
2470 : public VideoEncoderConfig::VideoStreamFactoryInterface {
2471 public:
2472 VideoStreamFactory() {}
2473
2474 private:
2475 std::vector<VideoStream> CreateEncoderStreams(
2476 int width,
2477 int height,
2478 const VideoEncoderConfig& encoder_config) override {
2479 std::vector<VideoStream> streams =
2480 test::CreateVideoStreams(width, height, encoder_config);
2481 for (size_t i = 0; i < streams.size(); ++i) {
Sergey Silkina796a7e2018-03-01 15:11:29 +01002482 streams[i].num_temporal_layers =
2483 kVideoCodecConfigObserverNumberOfTemporalLayers;
perkjfa10b552016-10-02 23:45:26 -07002484 }
2485 return streams;
2486 }
2487 };
2488
stefanff483612015-12-21 03:14:00 -08002489 void ModifyVideoConfigs(
2490 VideoSendStream::Config* send_config,
2491 std::vector<VideoReceiveStream::Config>* receive_configs,
2492 VideoEncoderConfig* encoder_config) override {
Peter Boström53eda3d2015-03-27 15:53:18 +01002493 send_config->encoder_settings.encoder = this;
2494 send_config->encoder_settings.payload_name = codec_name_;
2495
kthelgason29a44e32016-09-27 03:52:02 -07002496 encoder_config->encoder_specific_settings = GetEncoderSpecificSettings();
perkjfa10b552016-10-02 23:45:26 -07002497 encoder_config->video_stream_factory =
2498 new rtc::RefCountedObject<VideoStreamFactory>();
perkj26091b12016-09-01 01:17:40 -07002499 encoder_config_ = encoder_config->Copy();
Peter Boström53eda3d2015-03-27 15:53:18 +01002500 }
2501
stefanff483612015-12-21 03:14:00 -08002502 void OnVideoStreamsCreated(
Peter Boström53eda3d2015-03-27 15:53:18 +01002503 VideoSendStream* send_stream,
2504 const std::vector<VideoReceiveStream*>& receive_streams) override {
2505 stream_ = send_stream;
2506 }
2507
2508 int32_t InitEncode(const VideoCodec* config,
2509 int32_t number_of_cores,
2510 size_t max_payload_size) override {
2511 EXPECT_EQ(video_codec_type_, config->codecType);
2512 VerifyCodecSpecifics(*config);
2513 ++num_initializations_;
Per21d45d22016-10-30 21:37:57 +01002514 init_encode_event_.Set();
Peter Boström53eda3d2015-03-27 15:53:18 +01002515 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
2516 }
2517
2518 void VerifyCodecSpecifics(const VideoCodec& config) const;
kthelgason29a44e32016-09-27 03:52:02 -07002519 rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2520 GetEncoderSpecificSettings() const;
Peter Boström53eda3d2015-03-27 15:53:18 +01002521
2522 void PerformTest() override {
pbos14fe7082016-04-20 06:35:56 -07002523 EXPECT_TRUE(
2524 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002525 ASSERT_EQ(1u, num_initializations_) << "VideoEncoder not initialized.";
Peter Boström53eda3d2015-03-27 15:53:18 +01002526
2527 encoder_settings_.frameDroppingOn = true;
kthelgason29a44e32016-09-27 03:52:02 -07002528 encoder_config_.encoder_specific_settings = GetEncoderSpecificSettings();
perkj26091b12016-09-01 01:17:40 -07002529 stream_->ReconfigureVideoEncoder(std::move(encoder_config_));
pbos14fe7082016-04-20 06:35:56 -07002530 ASSERT_TRUE(
2531 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002532 EXPECT_EQ(2u, num_initializations_)
Peter Boström53eda3d2015-03-27 15:53:18 +01002533 << "ReconfigureVideoEncoder did not reinitialize the encoder with "
2534 "new encoder settings.";
2535 }
2536
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002537 int32_t Encode(const VideoFrame& input_image,
Peter Boström53eda3d2015-03-27 15:53:18 +01002538 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -07002539 const std::vector<FrameType>* frame_types) override {
Peter Boström53eda3d2015-03-27 15:53:18 +01002540 // Silently skip the encode, FakeEncoder::Encode doesn't produce VP8.
2541 return 0;
2542 }
2543
2544 T encoder_settings_;
2545 const VideoCodecType video_codec_type_;
2546 const char* const codec_name_;
pbos14fe7082016-04-20 06:35:56 -07002547 rtc::Event init_encode_event_;
Peter Boström53eda3d2015-03-27 15:53:18 +01002548 size_t num_initializations_;
2549 VideoSendStream* stream_;
2550 VideoEncoderConfig encoder_config_;
2551};
2552
2553template <>
2554void VideoCodecConfigObserver<VideoCodecH264>::VerifyCodecSpecifics(
2555 const VideoCodec& config) const {
hta257dc392016-10-25 09:05:06 -07002556 EXPECT_EQ(
2557 0, memcmp(&config.H264(), &encoder_settings_, sizeof(encoder_settings_)));
Peter Boström53eda3d2015-03-27 15:53:18 +01002558}
kthelgason29a44e32016-09-27 03:52:02 -07002559
2560template <>
2561rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2562VideoCodecConfigObserver<VideoCodecH264>::GetEncoderSpecificSettings() const {
2563 return new rtc::RefCountedObject<
2564 VideoEncoderConfig::H264EncoderSpecificSettings>(encoder_settings_);
2565}
2566
Peter Boström53eda3d2015-03-27 15:53:18 +01002567template <>
2568void VideoCodecConfigObserver<VideoCodecVP8>::VerifyCodecSpecifics(
2569 const VideoCodec& config) const {
2570 // Check that the number of temporal layers has propagated properly to
2571 // VideoCodec.
2572 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
hta257dc392016-10-25 09:05:06 -07002573 config.VP8().numberOfTemporalLayers);
Peter Boström53eda3d2015-03-27 15:53:18 +01002574
2575 for (unsigned char i = 0; i < config.numberOfSimulcastStreams; ++i) {
2576 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
2577 config.simulcastStream[i].numberOfTemporalLayers);
2578 }
2579
2580 // Set expected temporal layers as they should have been set when
Erik Språng08127a92016-11-16 16:41:30 +01002581 // reconfiguring the encoder and not match the set config. Also copy the
mflodmancc3d4422017-08-03 08:27:51 -07002582 // TemporalLayersFactory pointer that has been injected by VideoStreamEncoder.
Peter Boström53eda3d2015-03-27 15:53:18 +01002583 VideoCodecVP8 encoder_settings = encoder_settings_;
2584 encoder_settings.numberOfTemporalLayers =
2585 kVideoCodecConfigObserverNumberOfTemporalLayers;
Erik Språng08127a92016-11-16 16:41:30 +01002586 encoder_settings.tl_factory = config.VP8().tl_factory;
hta257dc392016-10-25 09:05:06 -07002587 EXPECT_EQ(
2588 0, memcmp(&config.VP8(), &encoder_settings, sizeof(encoder_settings_)));
Peter Boström53eda3d2015-03-27 15:53:18 +01002589}
kthelgason29a44e32016-09-27 03:52:02 -07002590
2591template <>
2592rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2593VideoCodecConfigObserver<VideoCodecVP8>::GetEncoderSpecificSettings() const {
2594 return new rtc::RefCountedObject<
2595 VideoEncoderConfig::Vp8EncoderSpecificSettings>(encoder_settings_);
2596}
2597
Peter Boström53eda3d2015-03-27 15:53:18 +01002598template <>
2599void VideoCodecConfigObserver<VideoCodecVP9>::VerifyCodecSpecifics(
2600 const VideoCodec& config) const {
2601 // Check that the number of temporal layers has propagated properly to
2602 // VideoCodec.
2603 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
hta257dc392016-10-25 09:05:06 -07002604 config.VP9().numberOfTemporalLayers);
Peter Boström53eda3d2015-03-27 15:53:18 +01002605
2606 for (unsigned char i = 0; i < config.numberOfSimulcastStreams; ++i) {
2607 EXPECT_EQ(kVideoCodecConfigObserverNumberOfTemporalLayers,
2608 config.simulcastStream[i].numberOfTemporalLayers);
2609 }
2610
2611 // Set expected temporal layers as they should have been set when
2612 // reconfiguring the encoder and not match the set config.
2613 VideoCodecVP9 encoder_settings = encoder_settings_;
2614 encoder_settings.numberOfTemporalLayers =
2615 kVideoCodecConfigObserverNumberOfTemporalLayers;
hta257dc392016-10-25 09:05:06 -07002616 EXPECT_EQ(
2617 0, memcmp(&(config.VP9()), &encoder_settings, sizeof(encoder_settings_)));
Peter Boström53eda3d2015-03-27 15:53:18 +01002618}
2619
kthelgason29a44e32016-09-27 03:52:02 -07002620template <>
2621rtc::scoped_refptr<VideoEncoderConfig::EncoderSpecificSettings>
2622VideoCodecConfigObserver<VideoCodecVP9>::GetEncoderSpecificSettings() const {
2623 return new rtc::RefCountedObject<
2624 VideoEncoderConfig::Vp9EncoderSpecificSettings>(encoder_settings_);
2625}
2626
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002627TEST_F(VideoSendStreamTest, EncoderSetupPropagatesVp8Config) {
Peter Boström53eda3d2015-03-27 15:53:18 +01002628 VideoCodecConfigObserver<VideoCodecVP8> test(kVideoCodecVP8, "VP8");
stefane74eef12016-01-08 06:47:13 -08002629 RunBaseTest(&test);
Peter Boström53eda3d2015-03-27 15:53:18 +01002630}
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002631
Peter Boström53eda3d2015-03-27 15:53:18 +01002632TEST_F(VideoSendStreamTest, EncoderSetupPropagatesVp9Config) {
2633 VideoCodecConfigObserver<VideoCodecVP9> test(kVideoCodecVP9, "VP9");
stefane74eef12016-01-08 06:47:13 -08002634 RunBaseTest(&test);
Peter Boström53eda3d2015-03-27 15:53:18 +01002635}
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002636
Peter Boström53eda3d2015-03-27 15:53:18 +01002637TEST_F(VideoSendStreamTest, EncoderSetupPropagatesH264Config) {
2638 VideoCodecConfigObserver<VideoCodecH264> test(kVideoCodecH264, "H264");
stefane74eef12016-01-08 06:47:13 -08002639 RunBaseTest(&test);
pbos@webrtc.org91f17522014-07-10 10:13:37 +00002640}
2641
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002642TEST_F(VideoSendStreamTest, RtcpSenderReportContainsMediaBytesSent) {
asapersson@webrtc.org14674212015-02-23 08:14:07 +00002643 class RtcpSenderReportTest : public test::SendTest {
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002644 public:
asapersson@webrtc.org14674212015-02-23 08:14:07 +00002645 RtcpSenderReportTest() : SendTest(kDefaultTimeoutMs),
2646 rtp_packets_sent_(0),
2647 media_bytes_sent_(0) {}
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002648
2649 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002650 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefan4b569042015-11-11 06:39:57 -08002651 rtc::CritScope lock(&crit_);
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002652 RTPHeader header;
2653 EXPECT_TRUE(parser_->Parse(packet, length, &header));
asapersson@webrtc.org14674212015-02-23 08:14:07 +00002654 ++rtp_packets_sent_;
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002655 media_bytes_sent_ += length - header.headerLength - header.paddingLength;
2656 return SEND_PACKET;
2657 }
2658
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002659 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
stefan4b569042015-11-11 06:39:57 -08002660 rtc::CritScope lock(&crit_);
danilchap3dc929e2016-11-02 08:21:59 -07002661 test::RtcpPacketParser parser;
2662 EXPECT_TRUE(parser.Parse(packet, length));
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002663
danilchap3dc929e2016-11-02 08:21:59 -07002664 if (parser.sender_report()->num_packets() > 0) {
2665 // Only compare sent media bytes if SenderPacketCount matches the
2666 // number of sent rtp packets (a new rtp packet could be sent before
2667 // the rtcp packet).
2668 if (parser.sender_report()->sender_octet_count() > 0 &&
2669 parser.sender_report()->sender_packet_count() ==
2670 rtp_packets_sent_) {
2671 EXPECT_EQ(media_bytes_sent_,
2672 parser.sender_report()->sender_octet_count());
2673 observation_complete_.Set();
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002674 }
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002675 }
2676
2677 return SEND_PACKET;
2678 }
2679
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002680 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01002681 EXPECT_TRUE(Wait()) << "Timed out while waiting for RTCP sender report.";
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002682 }
2683
stefan4b569042015-11-11 06:39:57 -08002684 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07002685 size_t rtp_packets_sent_ RTC_GUARDED_BY(&crit_);
2686 size_t media_bytes_sent_ RTC_GUARDED_BY(&crit_);
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002687 } test;
2688
stefane74eef12016-01-08 06:47:13 -08002689 RunBaseTest(&test);
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00002690}
2691
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002692TEST_F(VideoSendStreamTest, TranslatesTwoLayerScreencastToTargetBitrate) {
Sergey Silkina796a7e2018-03-01 15:11:29 +01002693 static const int kScreencastMaxTargetBitrateDeltaKbps = 1;
perkjfa10b552016-10-02 23:45:26 -07002694
2695 class VideoStreamFactory
2696 : public VideoEncoderConfig::VideoStreamFactoryInterface {
2697 public:
2698 VideoStreamFactory() {}
2699
2700 private:
2701 std::vector<VideoStream> CreateEncoderStreams(
2702 int width,
2703 int height,
2704 const VideoEncoderConfig& encoder_config) override {
2705 std::vector<VideoStream> streams =
2706 test::CreateVideoStreams(width, height, encoder_config);
Sergey Silkina796a7e2018-03-01 15:11:29 +01002707 EXPECT_FALSE(streams[0].num_temporal_layers.has_value());
2708 streams[0].num_temporal_layers = 2;
2709 RTC_CHECK_GT(streams[0].max_bitrate_bps,
2710 kScreencastMaxTargetBitrateDeltaKbps);
2711 streams[0].target_bitrate_bps =
2712 streams[0].max_bitrate_bps -
2713 kScreencastMaxTargetBitrateDeltaKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -07002714 return streams;
2715 }
2716 };
2717
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002718 class ScreencastTargetBitrateTest : public test::SendTest,
2719 public test::FakeEncoder {
2720 public:
2721 ScreencastTargetBitrateTest()
2722 : SendTest(kDefaultTimeoutMs),
2723 test::FakeEncoder(Clock::GetRealTimeClock()) {}
2724
2725 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002726 int32_t InitEncode(const VideoCodec* config,
2727 int32_t number_of_cores,
2728 size_t max_payload_size) override {
Sergey Silkina796a7e2018-03-01 15:11:29 +01002729 EXPECT_EQ(static_cast<unsigned int>(kScreencastMaxTargetBitrateDeltaKbps),
2730 config->maxBitrate - config->targetBitrate);
Peter Boström5811a392015-12-10 13:02:50 +01002731 observation_complete_.Set();
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002732 return test::FakeEncoder::InitEncode(
2733 config, number_of_cores, max_payload_size);
2734 }
stefanff483612015-12-21 03:14:00 -08002735 void ModifyVideoConfigs(
2736 VideoSendStream::Config* send_config,
2737 std::vector<VideoReceiveStream::Config>* receive_configs,
2738 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002739 send_config->encoder_settings.encoder = this;
perkjfa10b552016-10-02 23:45:26 -07002740 EXPECT_EQ(1u, encoder_config->number_of_streams);
2741 encoder_config->video_stream_factory =
2742 new rtc::RefCountedObject<VideoStreamFactory>();
Erik Språng143cec12015-04-28 10:01:41 +02002743 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002744 }
2745
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002746 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01002747 EXPECT_TRUE(Wait())
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002748 << "Timed out while waiting for the encoder to be initialized.";
2749 }
2750 } test;
2751
stefane74eef12016-01-08 06:47:13 -08002752 RunBaseTest(&test);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00002753}
pbos@webrtc.org00873182014-11-25 14:03:34 +00002754
philipelc6957c72016-04-28 15:52:49 +02002755TEST_F(VideoSendStreamTest, ReconfigureBitratesSetsEncoderBitratesCorrectly) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002756 // These are chosen to be "kind of odd" to not be accidentally checked against
2757 // default values.
2758 static const int kMinBitrateKbps = 137;
2759 static const int kStartBitrateKbps = 345;
2760 static const int kLowerMaxBitrateKbps = 312;
2761 static const int kMaxBitrateKbps = 413;
2762 static const int kIncreasedStartBitrateKbps = 451;
2763 static const int kIncreasedMaxBitrateKbps = 597;
2764 class EncoderBitrateThresholdObserver : public test::SendTest,
2765 public test::FakeEncoder {
2766 public:
eladalon413ee9a2017-08-22 04:02:52 -07002767 explicit EncoderBitrateThresholdObserver(
2768 test::SingleThreadedTaskQueueForTesting* task_queue)
pbos@webrtc.org00873182014-11-25 14:03:34 +00002769 : SendTest(kDefaultTimeoutMs),
2770 FakeEncoder(Clock::GetRealTimeClock()),
eladalon413ee9a2017-08-22 04:02:52 -07002771 task_queue_(task_queue),
pbos14fe7082016-04-20 06:35:56 -07002772 init_encode_event_(false, false),
perkj26091b12016-09-01 01:17:40 -07002773 bitrate_changed_event_(false, false),
2774 target_bitrate_(0),
Erik Språng737336d2016-07-29 12:59:36 +02002775 num_initializations_(0),
2776 call_(nullptr),
2777 send_stream_(nullptr) {}
pbos@webrtc.org00873182014-11-25 14:03:34 +00002778
2779 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002780 int32_t InitEncode(const VideoCodec* codecSettings,
2781 int32_t numberOfCores,
2782 size_t maxPayloadSize) override {
perkj26091b12016-09-01 01:17:40 -07002783 EXPECT_GE(codecSettings->startBitrate, codecSettings->minBitrate);
2784 EXPECT_LE(codecSettings->startBitrate, codecSettings->maxBitrate);
Per21d45d22016-10-30 21:37:57 +01002785 if (num_initializations_ == 0) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002786 EXPECT_EQ(static_cast<unsigned int>(kMinBitrateKbps),
2787 codecSettings->minBitrate);
2788 EXPECT_EQ(static_cast<unsigned int>(kStartBitrateKbps),
2789 codecSettings->startBitrate);
2790 EXPECT_EQ(static_cast<unsigned int>(kMaxBitrateKbps),
2791 codecSettings->maxBitrate);
Peter Boström5811a392015-12-10 13:02:50 +01002792 observation_complete_.Set();
Per21d45d22016-10-30 21:37:57 +01002793 } else if (num_initializations_ == 1) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002794 EXPECT_EQ(static_cast<unsigned int>(kLowerMaxBitrateKbps),
2795 codecSettings->maxBitrate);
2796 // The start bitrate should be kept (-1) and capped to the max bitrate.
2797 // Since this is not an end-to-end call no receiver should have been
2798 // returning a REMB that could lower this estimate.
2799 EXPECT_EQ(codecSettings->startBitrate, codecSettings->maxBitrate);
Per21d45d22016-10-30 21:37:57 +01002800 } else if (num_initializations_ == 2) {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002801 EXPECT_EQ(static_cast<unsigned int>(kIncreasedMaxBitrateKbps),
2802 codecSettings->maxBitrate);
perkj26091b12016-09-01 01:17:40 -07002803 // The start bitrate will be whatever the rate BitRateController
2804 // has currently configured but in the span of the set max and min
2805 // bitrate.
pbos@webrtc.org00873182014-11-25 14:03:34 +00002806 }
2807 ++num_initializations_;
Per21d45d22016-10-30 21:37:57 +01002808 init_encode_event_.Set();
2809
pbos@webrtc.org00873182014-11-25 14:03:34 +00002810 return FakeEncoder::InitEncode(codecSettings, numberOfCores,
2811 maxPayloadSize);
2812 }
2813
Erik Språng08127a92016-11-16 16:41:30 +01002814 int32_t SetRateAllocation(const BitrateAllocation& bitrate,
2815 uint32_t frameRate) override {
perkj26091b12016-09-01 01:17:40 -07002816 {
2817 rtc::CritScope lock(&crit_);
Erik Språng08127a92016-11-16 16:41:30 +01002818 if (target_bitrate_ == bitrate.get_sum_kbps()) {
2819 return FakeEncoder::SetRateAllocation(bitrate, frameRate);
perkjfa10b552016-10-02 23:45:26 -07002820 }
Erik Språng08127a92016-11-16 16:41:30 +01002821 target_bitrate_ = bitrate.get_sum_kbps();
perkj26091b12016-09-01 01:17:40 -07002822 }
2823 bitrate_changed_event_.Set();
Erik Språng08127a92016-11-16 16:41:30 +01002824 return FakeEncoder::SetRateAllocation(bitrate, frameRate);
perkj26091b12016-09-01 01:17:40 -07002825 }
2826
2827 void WaitForSetRates(uint32_t expected_bitrate) {
2828 EXPECT_TRUE(
2829 bitrate_changed_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs))
2830 << "Timed out while waiting encoder rate to be set.";
2831 rtc::CritScope lock(&crit_);
2832 EXPECT_EQ(expected_bitrate, target_bitrate_);
2833 }
2834
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002835 Call::Config GetSenderCallConfig() override {
philipel4fb651d2017-04-10 03:54:05 -07002836 Call::Config config(event_log_.get());
Stefan Holmere5904162015-03-26 11:11:06 +01002837 config.bitrate_config.min_bitrate_bps = kMinBitrateKbps * 1000;
2838 config.bitrate_config.start_bitrate_bps = kStartBitrateKbps * 1000;
2839 config.bitrate_config.max_bitrate_bps = kMaxBitrateKbps * 1000;
pbos@webrtc.org00873182014-11-25 14:03:34 +00002840 return config;
2841 }
2842
perkjfa10b552016-10-02 23:45:26 -07002843 class VideoStreamFactory
2844 : public VideoEncoderConfig::VideoStreamFactoryInterface {
2845 public:
2846 explicit VideoStreamFactory(int min_bitrate_bps)
2847 : min_bitrate_bps_(min_bitrate_bps) {}
2848
2849 private:
2850 std::vector<VideoStream> CreateEncoderStreams(
2851 int width,
2852 int height,
2853 const VideoEncoderConfig& encoder_config) override {
2854 std::vector<VideoStream> streams =
2855 test::CreateVideoStreams(width, height, encoder_config);
2856 streams[0].min_bitrate_bps = min_bitrate_bps_;
2857 return streams;
2858 }
2859
2860 const int min_bitrate_bps_;
2861 };
2862
stefanff483612015-12-21 03:14:00 -08002863 void ModifyVideoConfigs(
2864 VideoSendStream::Config* send_config,
2865 std::vector<VideoReceiveStream::Config>* receive_configs,
2866 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002867 send_config->encoder_settings.encoder = this;
2868 // Set bitrates lower/higher than min/max to make sure they are properly
2869 // capped.
perkjfa10b552016-10-02 23:45:26 -07002870 encoder_config->max_bitrate_bps = kMaxBitrateKbps * 1000;
2871 // Create a new StreamFactory to be able to set
2872 // |VideoStream.min_bitrate_bps|.
2873 encoder_config->video_stream_factory =
2874 new rtc::RefCountedObject<VideoStreamFactory>(kMinBitrateKbps * 1000);
perkj26091b12016-09-01 01:17:40 -07002875 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org00873182014-11-25 14:03:34 +00002876 }
2877
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002878 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
pbos@webrtc.org00873182014-11-25 14:03:34 +00002879 call_ = sender_call;
2880 }
2881
stefanff483612015-12-21 03:14:00 -08002882 void OnVideoStreamsCreated(
Stefan Holmere5904162015-03-26 11:11:06 +01002883 VideoSendStream* send_stream,
2884 const std::vector<VideoReceiveStream*>& receive_streams) override {
2885 send_stream_ = send_stream;
2886 }
2887
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002888 void PerformTest() override {
pbos14fe7082016-04-20 06:35:56 -07002889 ASSERT_TRUE(
2890 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs))
perkj26091b12016-09-01 01:17:40 -07002891 << "Timed out while waiting for encoder to be configured.";
2892 WaitForSetRates(kStartBitrateKbps);
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01002893 BitrateConstraints bitrate_config;
pbos@webrtc.org00873182014-11-25 14:03:34 +00002894 bitrate_config.start_bitrate_bps = kIncreasedStartBitrateKbps * 1000;
2895 bitrate_config.max_bitrate_bps = kIncreasedMaxBitrateKbps * 1000;
eladalon413ee9a2017-08-22 04:02:52 -07002896 task_queue_->SendTask([this, &bitrate_config]() {
Sebastian Jansson8f83b422018-02-21 13:07:13 +01002897 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
2898 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07002899 });
perkj26091b12016-09-01 01:17:40 -07002900 // Encoder rate is capped by EncoderConfig max_bitrate_bps.
2901 WaitForSetRates(kMaxBitrateKbps);
perkjfa10b552016-10-02 23:45:26 -07002902 encoder_config_.max_bitrate_bps = kLowerMaxBitrateKbps * 1000;
perkj26091b12016-09-01 01:17:40 -07002903 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
pbos14fe7082016-04-20 06:35:56 -07002904 ASSERT_TRUE(
2905 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002906 EXPECT_EQ(2, num_initializations_)
pbos@webrtc.org00873182014-11-25 14:03:34 +00002907 << "Encoder should have been reconfigured with the new value.";
perkjfa10b552016-10-02 23:45:26 -07002908 WaitForSetRates(kLowerMaxBitrateKbps);
2909
2910 encoder_config_.max_bitrate_bps = kIncreasedMaxBitrateKbps * 1000;
2911 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
2912 ASSERT_TRUE(
2913 init_encode_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs));
Per21d45d22016-10-30 21:37:57 +01002914 EXPECT_EQ(3, num_initializations_)
perkjfa10b552016-10-02 23:45:26 -07002915 << "Encoder should have been reconfigured with the new value.";
perkj26091b12016-09-01 01:17:40 -07002916 // Expected target bitrate is the start bitrate set in the call to
Sebastian Jansson8f83b422018-02-21 13:07:13 +01002917 // call_->GetTransportControllerSend()->SetSdpBitrateParameters.
perkj26091b12016-09-01 01:17:40 -07002918 WaitForSetRates(kIncreasedStartBitrateKbps);
pbos@webrtc.org00873182014-11-25 14:03:34 +00002919 }
2920
eladalon413ee9a2017-08-22 04:02:52 -07002921 test::SingleThreadedTaskQueueForTesting* const task_queue_;
pbos14fe7082016-04-20 06:35:56 -07002922 rtc::Event init_encode_event_;
perkj26091b12016-09-01 01:17:40 -07002923 rtc::Event bitrate_changed_event_;
2924 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07002925 uint32_t target_bitrate_ RTC_GUARDED_BY(&crit_);
perkjfa10b552016-10-02 23:45:26 -07002926
pbos@webrtc.org00873182014-11-25 14:03:34 +00002927 int num_initializations_;
2928 webrtc::Call* call_;
Stefan Holmere5904162015-03-26 11:11:06 +01002929 webrtc::VideoSendStream* send_stream_;
2930 webrtc::VideoEncoderConfig encoder_config_;
eladalon413ee9a2017-08-22 04:02:52 -07002931 } test(&task_queue_);
pbos@webrtc.org00873182014-11-25 14:03:34 +00002932
stefane74eef12016-01-08 06:47:13 -08002933 RunBaseTest(&test);
pbos@webrtc.org00873182014-11-25 14:03:34 +00002934}
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002935
2936TEST_F(VideoSendStreamTest, ReportsSentResolution) {
2937 static const size_t kNumStreams = 3;
2938 // Unusual resolutions to make sure that they are the ones being reported.
2939 static const struct {
2940 int width;
2941 int height;
2942 } kEncodedResolution[kNumStreams] = {
2943 {241, 181}, {300, 121}, {121, 221}};
2944 class ScreencastTargetBitrateTest : public test::SendTest,
2945 public test::FakeEncoder {
2946 public:
2947 ScreencastTargetBitrateTest()
2948 : SendTest(kDefaultTimeoutMs),
Erik Språng737336d2016-07-29 12:59:36 +02002949 test::FakeEncoder(Clock::GetRealTimeClock()),
2950 send_stream_(nullptr) {}
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002951
2952 private:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07002953 int32_t Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002954 const CodecSpecificInfo* codecSpecificInfo,
pbos22993e12015-10-19 02:39:06 -07002955 const std::vector<FrameType>* frame_types) override {
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002956 CodecSpecificInfo specifics;
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002957 specifics.codecType = kVideoCodecGeneric;
2958
2959 uint8_t buffer[16] = {0};
2960 EncodedImage encoded(buffer, sizeof(buffer), sizeof(buffer));
2961 encoded._timeStamp = input_image.timestamp();
2962 encoded.capture_time_ms_ = input_image.render_time_ms();
2963
2964 for (size_t i = 0; i < kNumStreams; ++i) {
2965 specifics.codecSpecific.generic.simulcast_idx = static_cast<uint8_t>(i);
2966 encoded._frameType = (*frame_types)[i];
2967 encoded._encodedWidth = kEncodedResolution[i].width;
2968 encoded._encodedHeight = kEncodedResolution[i].height;
brandtre78d2662017-01-16 05:57:16 -08002969 EncodedImageCallback* callback;
2970 {
2971 rtc::CritScope cs(&crit_sect_);
2972 callback = callback_;
2973 }
2974 RTC_DCHECK(callback);
2975 if (callback->OnEncodedImage(encoded, &specifics, nullptr).error !=
sergeyu2cb155a2016-11-04 11:39:29 -07002976 EncodedImageCallback::Result::OK) {
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002977 return -1;
sergeyu2cb155a2016-11-04 11:39:29 -07002978 }
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002979 }
2980
Peter Boström5811a392015-12-10 13:02:50 +01002981 observation_complete_.Set();
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002982 return 0;
2983 }
stefanff483612015-12-21 03:14:00 -08002984 void ModifyVideoConfigs(
2985 VideoSendStream::Config* send_config,
2986 std::vector<VideoReceiveStream::Config>* receive_configs,
2987 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002988 send_config->encoder_settings.encoder = this;
perkjfa10b552016-10-02 23:45:26 -07002989 EXPECT_EQ(kNumStreams, encoder_config->number_of_streams);
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002990 }
2991
Stefan Holmer9fea80f2016-01-07 17:43:18 +01002992 size_t GetNumVideoStreams() const override { return kNumStreams; }
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002993
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00002994 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01002995 EXPECT_TRUE(Wait())
pbos@webrtc.org273a4142014-12-01 15:23:21 +00002996 << "Timed out while waiting for the encoder to send one frame.";
2997 VideoSendStream::Stats stats = send_stream_->GetStats();
2998
2999 for (size_t i = 0; i < kNumStreams; ++i) {
Stefan Holmer9fea80f2016-01-07 17:43:18 +01003000 ASSERT_TRUE(stats.substreams.find(kVideoSendSsrcs[i]) !=
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003001 stats.substreams.end())
Stefan Holmer9fea80f2016-01-07 17:43:18 +01003002 << "No stats for SSRC: " << kVideoSendSsrcs[i]
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003003 << ", stats should exist as soon as frames have been encoded.";
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00003004 VideoSendStream::StreamStats ssrc_stats =
Stefan Holmer9fea80f2016-01-07 17:43:18 +01003005 stats.substreams[kVideoSendSsrcs[i]];
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00003006 EXPECT_EQ(kEncodedResolution[i].width, ssrc_stats.width);
3007 EXPECT_EQ(kEncodedResolution[i].height, ssrc_stats.height);
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003008 }
3009 }
3010
stefanff483612015-12-21 03:14:00 -08003011 void OnVideoStreamsCreated(
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003012 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00003013 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003014 send_stream_ = send_stream;
3015 }
3016
3017 VideoSendStream* send_stream_;
3018 } test;
3019
stefane74eef12016-01-08 06:47:13 -08003020 RunBaseTest(&test);
pbos@webrtc.org273a4142014-12-01 15:23:21 +00003021}
philipel0f9af012015-09-01 07:01:51 -07003022
Peter Boström12996152016-05-14 02:03:18 +02003023#if !defined(RTC_DISABLE_VP9)
Åsa Perssonff24c042015-12-04 10:58:08 +01003024class Vp9HeaderObserver : public test::SendTest {
philipel0f9af012015-09-01 07:01:51 -07003025 public:
Åsa Perssonff24c042015-12-04 10:58:08 +01003026 Vp9HeaderObserver()
3027 : SendTest(VideoSendStreamTest::kLongTimeoutMs),
philipel7fabd462015-09-03 04:42:32 -07003028 vp9_encoder_(VP9Encoder::Create()),
Åsa Perssonff24c042015-12-04 10:58:08 +01003029 vp9_settings_(VideoEncoder::GetDefaultVp9Settings()),
3030 packets_sent_(0),
perkjfa10b552016-10-02 23:45:26 -07003031 frames_sent_(0),
3032 expected_width_(0),
3033 expected_height_(0) {}
philipel7fabd462015-09-03 04:42:32 -07003034
stefanff483612015-12-21 03:14:00 -08003035 virtual void ModifyVideoConfigsHook(
philipel0f9af012015-09-01 07:01:51 -07003036 VideoSendStream::Config* send_config,
3037 std::vector<VideoReceiveStream::Config>* receive_configs,
3038 VideoEncoderConfig* encoder_config) {}
3039
Åsa Perssonff24c042015-12-04 10:58:08 +01003040 virtual void InspectHeader(const RTPVideoHeaderVP9& vp9) = 0;
philipel0f9af012015-09-01 07:01:51 -07003041
3042 private:
minyue20c84cc2017-04-10 16:57:57 -07003043 const int kVp9PayloadType = test::CallTest::kVideoSendPayloadType;
philipel0f9af012015-09-01 07:01:51 -07003044
perkjfa10b552016-10-02 23:45:26 -07003045 class VideoStreamFactory
3046 : public VideoEncoderConfig::VideoStreamFactoryInterface {
3047 public:
3048 explicit VideoStreamFactory(size_t number_of_temporal_layers)
3049 : number_of_temporal_layers_(number_of_temporal_layers) {}
3050
3051 private:
3052 std::vector<VideoStream> CreateEncoderStreams(
3053 int width,
3054 int height,
3055 const VideoEncoderConfig& encoder_config) override {
3056 std::vector<VideoStream> streams =
3057 test::CreateVideoStreams(width, height, encoder_config);
Sergey Silkina796a7e2018-03-01 15:11:29 +01003058 streams.back().num_temporal_layers = number_of_temporal_layers_;
perkjfa10b552016-10-02 23:45:26 -07003059 return streams;
3060 }
3061
3062 const size_t number_of_temporal_layers_;
3063 };
3064
stefanff483612015-12-21 03:14:00 -08003065 void ModifyVideoConfigs(
3066 VideoSendStream::Config* send_config,
3067 std::vector<VideoReceiveStream::Config>* receive_configs,
3068 VideoEncoderConfig* encoder_config) override {
philipel7fabd462015-09-03 04:42:32 -07003069 send_config->encoder_settings.encoder = vp9_encoder_.get();
philipel0f9af012015-09-01 07:01:51 -07003070 send_config->encoder_settings.payload_name = "VP9";
3071 send_config->encoder_settings.payload_type = kVp9PayloadType;
stefanff483612015-12-21 03:14:00 -08003072 ModifyVideoConfigsHook(send_config, receive_configs, encoder_config);
kthelgason29a44e32016-09-27 03:52:02 -07003073 encoder_config->encoder_specific_settings = new rtc::RefCountedObject<
3074 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings_);
perkjfa10b552016-10-02 23:45:26 -07003075 EXPECT_EQ(1u, encoder_config->number_of_streams);
3076 encoder_config->video_stream_factory =
3077 new rtc::RefCountedObject<VideoStreamFactory>(
3078 vp9_settings_.numberOfTemporalLayers);
perkj26091b12016-09-01 01:17:40 -07003079 encoder_config_ = encoder_config->Copy();
philipel0f9af012015-09-01 07:01:51 -07003080 }
3081
perkjfa10b552016-10-02 23:45:26 -07003082 void ModifyVideoCaptureStartResolution(int* width,
3083 int* height,
3084 int* frame_rate) override {
3085 expected_width_ = *width;
3086 expected_height_ = *height;
3087 }
3088
philipel0f9af012015-09-01 07:01:51 -07003089 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +01003090 EXPECT_TRUE(Wait()) << "Test timed out waiting for VP9 packet, num frames "
3091 << frames_sent_;
philipel0f9af012015-09-01 07:01:51 -07003092 }
3093
3094 Action OnSendRtp(const uint8_t* packet, size_t length) override {
3095 RTPHeader header;
3096 EXPECT_TRUE(parser_->Parse(packet, length, &header));
3097
Åsa Perssonff24c042015-12-04 10:58:08 +01003098 EXPECT_EQ(kVp9PayloadType, header.payloadType);
3099 const uint8_t* payload = packet + header.headerLength;
3100 size_t payload_length = length - header.headerLength - header.paddingLength;
philipel0f9af012015-09-01 07:01:51 -07003101
Åsa Perssonff24c042015-12-04 10:58:08 +01003102 bool new_packet = packets_sent_ == 0 ||
3103 IsNewerSequenceNumber(header.sequenceNumber,
3104 last_header_.sequenceNumber);
3105 if (payload_length > 0 && new_packet) {
3106 RtpDepacketizer::ParsedPayload parsed;
3107 RtpDepacketizerVp9 depacketizer;
3108 EXPECT_TRUE(depacketizer.Parse(&parsed, payload, payload_length));
3109 EXPECT_EQ(RtpVideoCodecTypes::kRtpVideoVp9, parsed.type.Video.codec);
3110 // Verify common fields for all configurations.
3111 VerifyCommonHeader(parsed.type.Video.codecHeader.VP9);
3112 CompareConsecutiveFrames(header, parsed.type.Video);
3113 // Verify configuration specific settings.
3114 InspectHeader(parsed.type.Video.codecHeader.VP9);
philipel0f9af012015-09-01 07:01:51 -07003115
Åsa Perssonff24c042015-12-04 10:58:08 +01003116 ++packets_sent_;
3117 if (header.markerBit) {
3118 ++frames_sent_;
philipel0f9af012015-09-01 07:01:51 -07003119 }
Åsa Perssonff24c042015-12-04 10:58:08 +01003120 last_header_ = header;
3121 last_vp9_ = parsed.type.Video.codecHeader.VP9;
philipel0f9af012015-09-01 07:01:51 -07003122 }
philipel0f9af012015-09-01 07:01:51 -07003123 return SEND_PACKET;
3124 }
3125
philipel7fabd462015-09-03 04:42:32 -07003126 protected:
Åsa Perssonff24c042015-12-04 10:58:08 +01003127 bool ContinuousPictureId(const RTPVideoHeaderVP9& vp9) const {
3128 if (last_vp9_.picture_id > vp9.picture_id) {
3129 return vp9.picture_id == 0; // Wrap.
3130 } else {
3131 return vp9.picture_id == last_vp9_.picture_id + 1;
3132 }
3133 }
3134
3135 void VerifySpatialIdxWithinFrame(const RTPVideoHeaderVP9& vp9) const {
Åsa Perssonff24c042015-12-04 10:58:08 +01003136 bool new_layer = vp9.spatial_idx != last_vp9_.spatial_idx;
3137 EXPECT_EQ(new_layer, vp9.beginning_of_frame);
3138 EXPECT_EQ(new_layer, last_vp9_.end_of_frame);
3139 EXPECT_EQ(new_layer ? last_vp9_.spatial_idx + 1 : last_vp9_.spatial_idx,
3140 vp9.spatial_idx);
3141 }
3142
3143 void VerifyFixedTemporalLayerStructure(const RTPVideoHeaderVP9& vp9,
3144 uint8_t num_layers) const {
3145 switch (num_layers) {
3146 case 0:
3147 VerifyTemporalLayerStructure0(vp9);
3148 break;
3149 case 1:
3150 VerifyTemporalLayerStructure1(vp9);
3151 break;
3152 case 2:
3153 VerifyTemporalLayerStructure2(vp9);
3154 break;
3155 case 3:
3156 VerifyTemporalLayerStructure3(vp9);
3157 break;
3158 default:
3159 RTC_NOTREACHED();
3160 }
3161 }
3162
3163 void VerifyTemporalLayerStructure0(const RTPVideoHeaderVP9& vp9) const {
3164 EXPECT_EQ(kNoTl0PicIdx, vp9.tl0_pic_idx);
3165 EXPECT_EQ(kNoTemporalIdx, vp9.temporal_idx); // no tid
3166 EXPECT_FALSE(vp9.temporal_up_switch);
3167 }
3168
3169 void VerifyTemporalLayerStructure1(const RTPVideoHeaderVP9& vp9) const {
3170 EXPECT_NE(kNoTl0PicIdx, vp9.tl0_pic_idx);
3171 EXPECT_EQ(0, vp9.temporal_idx); // 0,0,0,...
3172 EXPECT_FALSE(vp9.temporal_up_switch);
3173 }
3174
3175 void VerifyTemporalLayerStructure2(const RTPVideoHeaderVP9& vp9) const {
3176 EXPECT_NE(kNoTl0PicIdx, vp9.tl0_pic_idx);
3177 EXPECT_GE(vp9.temporal_idx, 0); // 0,1,0,1,... (tid reset on I-frames).
3178 EXPECT_LE(vp9.temporal_idx, 1);
3179 EXPECT_EQ(vp9.temporal_idx > 0, vp9.temporal_up_switch);
3180 if (IsNewPictureId(vp9)) {
3181 uint8_t expected_tid =
3182 (!vp9.inter_pic_predicted || last_vp9_.temporal_idx == 1) ? 0 : 1;
3183 EXPECT_EQ(expected_tid, vp9.temporal_idx);
3184 }
3185 }
3186
3187 void VerifyTemporalLayerStructure3(const RTPVideoHeaderVP9& vp9) const {
3188 EXPECT_NE(kNoTl0PicIdx, vp9.tl0_pic_idx);
3189 EXPECT_GE(vp9.temporal_idx, 0); // 0,2,1,2,... (tid reset on I-frames).
3190 EXPECT_LE(vp9.temporal_idx, 2);
3191 if (IsNewPictureId(vp9) && vp9.inter_pic_predicted) {
3192 EXPECT_NE(vp9.temporal_idx, last_vp9_.temporal_idx);
3193 switch (vp9.temporal_idx) {
3194 case 0:
3195 EXPECT_EQ(2, last_vp9_.temporal_idx);
3196 EXPECT_FALSE(vp9.temporal_up_switch);
3197 break;
3198 case 1:
3199 EXPECT_EQ(2, last_vp9_.temporal_idx);
3200 EXPECT_TRUE(vp9.temporal_up_switch);
3201 break;
3202 case 2:
3203 EXPECT_EQ(last_vp9_.temporal_idx == 0, vp9.temporal_up_switch);
3204 break;
3205 }
3206 }
3207 }
3208
3209 void VerifyTl0Idx(const RTPVideoHeaderVP9& vp9) const {
3210 if (vp9.tl0_pic_idx == kNoTl0PicIdx)
3211 return;
3212
3213 uint8_t expected_tl0_idx = last_vp9_.tl0_pic_idx;
3214 if (vp9.temporal_idx == 0)
3215 ++expected_tl0_idx;
3216 EXPECT_EQ(expected_tl0_idx, vp9.tl0_pic_idx);
3217 }
3218
3219 bool IsNewPictureId(const RTPVideoHeaderVP9& vp9) const {
3220 return frames_sent_ > 0 && (vp9.picture_id != last_vp9_.picture_id);
3221 }
3222
3223 // Flexible mode (F=1): Non-flexible mode (F=0):
3224 //
3225 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3226 // |I|P|L|F|B|E|V|-| |I|P|L|F|B|E|V|-|
3227 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3228 // I: |M| PICTURE ID | I: |M| PICTURE ID |
3229 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3230 // M: | EXTENDED PID | M: | EXTENDED PID |
3231 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3232 // L: | T |U| S |D| L: | T |U| S |D|
3233 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3234 // P,F: | P_DIFF |X|N| | TL0PICIDX |
3235 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3236 // X: |EXTENDED P_DIFF| V: | SS .. |
3237 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
3238 // V: | SS .. |
3239 // +-+-+-+-+-+-+-+-+
3240 void VerifyCommonHeader(const RTPVideoHeaderVP9& vp9) const {
3241 EXPECT_EQ(kMaxTwoBytePictureId, vp9.max_picture_id); // M:1
3242 EXPECT_NE(kNoPictureId, vp9.picture_id); // I:1
3243 EXPECT_EQ(vp9_settings_.flexibleMode, vp9.flexible_mode); // F
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003244
3245 if (vp9_settings_.numberOfSpatialLayers > 1) {
3246 EXPECT_LT(vp9.spatial_idx, vp9_settings_.numberOfSpatialLayers);
3247 } else if (vp9_settings_.numberOfTemporalLayers > 1) {
3248 EXPECT_EQ(vp9.spatial_idx, 0);
3249 } else {
3250 EXPECT_EQ(vp9.spatial_idx, kNoSpatialIdx);
3251 }
3252
3253 if (vp9_settings_.numberOfTemporalLayers > 1) {
3254 EXPECT_LT(vp9.temporal_idx, vp9_settings_.numberOfTemporalLayers);
3255 } else if (vp9_settings_.numberOfSpatialLayers > 1) {
3256 EXPECT_EQ(vp9.temporal_idx, 0);
3257 } else {
3258 EXPECT_EQ(vp9.temporal_idx, kNoTemporalIdx);
3259 }
3260
Åsa Perssonff24c042015-12-04 10:58:08 +01003261 if (vp9.ss_data_available) // V
3262 VerifySsData(vp9);
3263
3264 if (frames_sent_ == 0)
3265 EXPECT_FALSE(vp9.inter_pic_predicted); // P
3266
3267 if (!vp9.inter_pic_predicted) {
3268 EXPECT_TRUE(vp9.temporal_idx == 0 || vp9.temporal_idx == kNoTemporalIdx);
3269 EXPECT_FALSE(vp9.temporal_up_switch);
3270 }
3271 }
3272
3273 // Scalability structure (SS).
3274 //
3275 // +-+-+-+-+-+-+-+-+
3276 // V: | N_S |Y|G|-|-|-|
3277 // +-+-+-+-+-+-+-+-+
3278 // Y: | WIDTH | N_S + 1 times
3279 // +-+-+-+-+-+-+-+-+
3280 // | HEIGHT |
3281 // +-+-+-+-+-+-+-+-+
3282 // G: | N_G |
3283 // +-+-+-+-+-+-+-+-+
3284 // N_G: | T |U| R |-|-| N_G times
3285 // +-+-+-+-+-+-+-+-+
3286 // | P_DIFF | R times
3287 // +-+-+-+-+-+-+-+-+
3288 void VerifySsData(const RTPVideoHeaderVP9& vp9) const {
3289 EXPECT_TRUE(vp9.ss_data_available); // V
3290 EXPECT_EQ(vp9_settings_.numberOfSpatialLayers, // N_S + 1
3291 vp9.num_spatial_layers);
3292 EXPECT_TRUE(vp9.spatial_layer_resolution_present); // Y:1
perkjfa10b552016-10-02 23:45:26 -07003293 int expected_width = expected_width_;
3294 int expected_height = expected_height_;
Peter Boström02083222016-06-14 12:52:54 +02003295 for (int i = static_cast<int>(vp9.num_spatial_layers) - 1; i >= 0; --i) {
Åsa Perssonff24c042015-12-04 10:58:08 +01003296 EXPECT_EQ(expected_width, vp9.width[i]); // WIDTH
3297 EXPECT_EQ(expected_height, vp9.height[i]); // HEIGHT
3298 expected_width /= 2;
3299 expected_height /= 2;
3300 }
3301 }
3302
3303 void CompareConsecutiveFrames(const RTPHeader& header,
3304 const RTPVideoHeader& video) const {
3305 const RTPVideoHeaderVP9& vp9 = video.codecHeader.VP9;
3306
3307 bool new_frame = packets_sent_ == 0 ||
3308 IsNewerTimestamp(header.timestamp, last_header_.timestamp);
johan0d1b2b62017-01-10 04:21:35 -08003309 EXPECT_EQ(new_frame, video.is_first_packet_in_frame);
Åsa Perssonff24c042015-12-04 10:58:08 +01003310 if (!new_frame) {
3311 EXPECT_FALSE(last_header_.markerBit);
3312 EXPECT_EQ(last_header_.timestamp, header.timestamp);
3313 EXPECT_EQ(last_vp9_.picture_id, vp9.picture_id);
3314 EXPECT_EQ(last_vp9_.temporal_idx, vp9.temporal_idx);
3315 EXPECT_EQ(last_vp9_.tl0_pic_idx, vp9.tl0_pic_idx);
3316 VerifySpatialIdxWithinFrame(vp9);
3317 return;
3318 }
3319 // New frame.
3320 EXPECT_TRUE(vp9.beginning_of_frame);
3321
3322 // Compare with last packet in previous frame.
3323 if (frames_sent_ == 0)
3324 return;
3325 EXPECT_TRUE(last_vp9_.end_of_frame);
3326 EXPECT_TRUE(last_header_.markerBit);
3327 EXPECT_TRUE(ContinuousPictureId(vp9));
3328 VerifyTl0Idx(vp9);
3329 }
3330
kwiberg27f982b2016-03-01 11:52:33 -08003331 std::unique_ptr<VP9Encoder> vp9_encoder_;
philipel0f9af012015-09-01 07:01:51 -07003332 VideoCodecVP9 vp9_settings_;
Åsa Perssonff24c042015-12-04 10:58:08 +01003333 webrtc::VideoEncoderConfig encoder_config_;
3334 RTPHeader last_header_;
3335 RTPVideoHeaderVP9 last_vp9_;
3336 size_t packets_sent_;
3337 size_t frames_sent_;
perkjfa10b552016-10-02 23:45:26 -07003338 int expected_width_;
3339 int expected_height_;
philipel0f9af012015-09-01 07:01:51 -07003340};
3341
Åsa Perssonff24c042015-12-04 10:58:08 +01003342TEST_F(VideoSendStreamTest, Vp9NonFlexMode_1Tl1SLayers) {
3343 const uint8_t kNumTemporalLayers = 1;
3344 const uint8_t kNumSpatialLayers = 1;
3345 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3346}
3347
3348TEST_F(VideoSendStreamTest, Vp9NonFlexMode_2Tl1SLayers) {
3349 const uint8_t kNumTemporalLayers = 2;
3350 const uint8_t kNumSpatialLayers = 1;
3351 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3352}
3353
3354TEST_F(VideoSendStreamTest, Vp9NonFlexMode_3Tl1SLayers) {
3355 const uint8_t kNumTemporalLayers = 3;
3356 const uint8_t kNumSpatialLayers = 1;
3357 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3358}
3359
3360TEST_F(VideoSendStreamTest, Vp9NonFlexMode_1Tl2SLayers) {
3361 const uint8_t kNumTemporalLayers = 1;
3362 const uint8_t kNumSpatialLayers = 2;
3363 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3364}
3365
3366TEST_F(VideoSendStreamTest, Vp9NonFlexMode_2Tl2SLayers) {
3367 const uint8_t kNumTemporalLayers = 2;
3368 const uint8_t kNumSpatialLayers = 2;
3369 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3370}
3371
3372TEST_F(VideoSendStreamTest, Vp9NonFlexMode_3Tl2SLayers) {
3373 const uint8_t kNumTemporalLayers = 3;
3374 const uint8_t kNumSpatialLayers = 2;
3375 TestVp9NonFlexMode(kNumTemporalLayers, kNumSpatialLayers);
3376}
3377
3378void VideoSendStreamTest::TestVp9NonFlexMode(uint8_t num_temporal_layers,
3379 uint8_t num_spatial_layers) {
3380 static const size_t kNumFramesToSend = 100;
3381 // Set to < kNumFramesToSend and coprime to length of temporal layer
3382 // structures to verify temporal id reset on key frame.
3383 static const int kKeyFrameInterval = 31;
3384 class NonFlexibleMode : public Vp9HeaderObserver {
3385 public:
3386 NonFlexibleMode(uint8_t num_temporal_layers, uint8_t num_spatial_layers)
3387 : num_temporal_layers_(num_temporal_layers),
3388 num_spatial_layers_(num_spatial_layers),
3389 l_field_(num_temporal_layers > 1 || num_spatial_layers > 1) {}
stefanff483612015-12-21 03:14:00 -08003390 void ModifyVideoConfigsHook(
philipel0f9af012015-09-01 07:01:51 -07003391 VideoSendStream::Config* send_config,
3392 std::vector<VideoReceiveStream::Config>* receive_configs,
3393 VideoEncoderConfig* encoder_config) override {
Åsa Perssonff24c042015-12-04 10:58:08 +01003394 vp9_settings_.flexibleMode = false;
3395 vp9_settings_.frameDroppingOn = false;
3396 vp9_settings_.keyFrameInterval = kKeyFrameInterval;
3397 vp9_settings_.numberOfTemporalLayers = num_temporal_layers_;
3398 vp9_settings_.numberOfSpatialLayers = num_spatial_layers_;
philipel0f9af012015-09-01 07:01:51 -07003399 }
3400
Åsa Perssonff24c042015-12-04 10:58:08 +01003401 void InspectHeader(const RTPVideoHeaderVP9& vp9) override {
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003402 bool ss_data_expected =
3403 !vp9.inter_pic_predicted && vp9.beginning_of_frame &&
3404 (vp9.spatial_idx == 0 || vp9.spatial_idx == kNoSpatialIdx);
Åsa Perssonff24c042015-12-04 10:58:08 +01003405 EXPECT_EQ(ss_data_expected, vp9.ss_data_available);
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003406 if (num_spatial_layers_ > 1) {
3407 EXPECT_EQ(vp9.spatial_idx > 0, vp9.inter_layer_predicted);
3408 } else {
3409 EXPECT_FALSE(vp9.inter_layer_predicted);
3410 }
3411
asapersson38bb8ad2015-12-14 01:41:19 -08003412 EXPECT_EQ(!vp9.inter_pic_predicted,
3413 frames_sent_ % kKeyFrameInterval == 0);
Åsa Perssonff24c042015-12-04 10:58:08 +01003414
3415 if (IsNewPictureId(vp9)) {
Sergey Silkin3f2634e2017-11-06 11:49:19 +01003416 if (num_temporal_layers_ == 1 && num_spatial_layers_ == 1) {
3417 EXPECT_EQ(kNoSpatialIdx, vp9.spatial_idx);
3418 } else {
3419 EXPECT_EQ(0, vp9.spatial_idx);
3420 }
3421 if (num_spatial_layers_ > 1)
3422 EXPECT_EQ(num_spatial_layers_ - 1, last_vp9_.spatial_idx);
Åsa Perssonff24c042015-12-04 10:58:08 +01003423 }
3424
3425 VerifyFixedTemporalLayerStructure(vp9,
3426 l_field_ ? num_temporal_layers_ : 0);
3427
3428 if (frames_sent_ > kNumFramesToSend)
Peter Boström5811a392015-12-10 13:02:50 +01003429 observation_complete_.Set();
philipel0f9af012015-09-01 07:01:51 -07003430 }
Åsa Perssonff24c042015-12-04 10:58:08 +01003431 const uint8_t num_temporal_layers_;
3432 const uint8_t num_spatial_layers_;
3433 const bool l_field_;
3434 } test(num_temporal_layers, num_spatial_layers);
philipelcfc319b2015-11-10 07:17:23 -08003435
stefane74eef12016-01-08 06:47:13 -08003436 RunBaseTest(&test);
philipelcfc319b2015-11-10 07:17:23 -08003437}
3438
asaperssond9f641e2016-01-21 01:11:35 -08003439TEST_F(VideoSendStreamTest, Vp9NonFlexModeSmallResolution) {
3440 static const size_t kNumFramesToSend = 50;
3441 static const int kWidth = 4;
3442 static const int kHeight = 4;
3443 class NonFlexibleModeResolution : public Vp9HeaderObserver {
3444 void ModifyVideoConfigsHook(
3445 VideoSendStream::Config* send_config,
3446 std::vector<VideoReceiveStream::Config>* receive_configs,
3447 VideoEncoderConfig* encoder_config) override {
3448 vp9_settings_.flexibleMode = false;
3449 vp9_settings_.numberOfTemporalLayers = 1;
3450 vp9_settings_.numberOfSpatialLayers = 1;
3451
perkjfa10b552016-10-02 23:45:26 -07003452 EXPECT_EQ(1u, encoder_config->number_of_streams);
asaperssond9f641e2016-01-21 01:11:35 -08003453 }
3454
3455 void InspectHeader(const RTPVideoHeaderVP9& vp9_header) override {
3456 if (frames_sent_ > kNumFramesToSend)
3457 observation_complete_.Set();
3458 }
perkjfa10b552016-10-02 23:45:26 -07003459
3460 void ModifyVideoCaptureStartResolution(int* width,
3461 int* height,
3462 int* frame_rate) override {
3463 expected_width_ = kWidth;
3464 expected_height_ = kHeight;
3465 *width = kWidth;
3466 *height = kHeight;
3467 }
asaperssond9f641e2016-01-21 01:11:35 -08003468 } test;
3469
3470 RunBaseTest(&test);
3471}
3472
kjellanderf9e2a362017-03-24 12:17:33 -07003473#if defined(WEBRTC_ANDROID)
3474// Crashes on Android; bugs.webrtc.org/7401
3475#define MAYBE_Vp9FlexModeRefCount DISABLED_Vp9FlexModeRefCount
3476#else
3477#define MAYBE_Vp9FlexModeRefCount Vp9FlexModeRefCount
3478#endif
3479TEST_F(VideoSendStreamTest, MAYBE_Vp9FlexModeRefCount) {
Åsa Perssonff24c042015-12-04 10:58:08 +01003480 class FlexibleMode : public Vp9HeaderObserver {
stefanff483612015-12-21 03:14:00 -08003481 void ModifyVideoConfigsHook(
philipelcfc319b2015-11-10 07:17:23 -08003482 VideoSendStream::Config* send_config,
3483 std::vector<VideoReceiveStream::Config>* receive_configs,
3484 VideoEncoderConfig* encoder_config) override {
Åsa Perssonff24c042015-12-04 10:58:08 +01003485 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
philipelcfc319b2015-11-10 07:17:23 -08003486 vp9_settings_.flexibleMode = true;
Åsa Perssonff24c042015-12-04 10:58:08 +01003487 vp9_settings_.numberOfTemporalLayers = 1;
3488 vp9_settings_.numberOfSpatialLayers = 2;
philipelcfc319b2015-11-10 07:17:23 -08003489 }
3490
Åsa Perssonff24c042015-12-04 10:58:08 +01003491 void InspectHeader(const RTPVideoHeaderVP9& vp9_header) override {
3492 EXPECT_TRUE(vp9_header.flexible_mode);
3493 EXPECT_EQ(kNoTl0PicIdx, vp9_header.tl0_pic_idx);
3494 if (vp9_header.inter_pic_predicted) {
3495 EXPECT_GT(vp9_header.num_ref_pics, 0u);
Peter Boström5811a392015-12-10 13:02:50 +01003496 observation_complete_.Set();
philipelcfc319b2015-11-10 07:17:23 -08003497 }
3498 }
3499 } test;
3500
stefane74eef12016-01-08 06:47:13 -08003501 RunBaseTest(&test);
philipelcfc319b2015-11-10 07:17:23 -08003502}
Peter Boström12996152016-05-14 02:03:18 +02003503#endif // !defined(RTC_DISABLE_VP9)
philipelcfc319b2015-11-10 07:17:23 -08003504
perkj803d97f2016-11-01 11:45:46 -07003505void VideoSendStreamTest::TestRequestSourceRotateVideo(
3506 bool support_orientation_ext) {
philipel4fb651d2017-04-10 03:54:05 -07003507 CreateSenderCall(Call::Config(event_log_.get()));
perkj803d97f2016-11-01 11:45:46 -07003508
3509 test::NullTransport transport;
brandtr841de6a2016-11-15 07:10:52 -08003510 CreateSendConfig(1, 0, 0, &transport);
perkj803d97f2016-11-01 11:45:46 -07003511 video_send_config_.rtp.extensions.clear();
3512 if (support_orientation_ext) {
3513 video_send_config_.rtp.extensions.push_back(
3514 RtpExtension(RtpExtension::kVideoRotationUri, 1));
3515 }
3516
3517 CreateVideoStreams();
3518 test::FrameForwarder forwarder;
3519 video_send_stream_->SetSource(
sprangc5d62e22017-04-02 23:53:04 -07003520 &forwarder, VideoSendStream::DegradationPreference::kMaintainFramerate);
perkj803d97f2016-11-01 11:45:46 -07003521
3522 EXPECT_TRUE(forwarder.sink_wants().rotation_applied !=
3523 support_orientation_ext);
3524
3525 DestroyStreams();
3526}
3527
3528TEST_F(VideoSendStreamTest,
3529 RequestSourceRotateIfVideoOrientationExtensionNotSupported) {
3530 TestRequestSourceRotateVideo(false);
3531}
3532
3533TEST_F(VideoSendStreamTest,
3534 DoNotRequestsRotationIfVideoOrientationExtensionSupported) {
3535 TestRequestSourceRotateVideo(true);
3536}
3537
michaelta3328772016-11-29 09:25:03 -08003538// This test verifies that overhead is removed from the bandwidth estimate by
3539// testing that the maximum possible target payload rate is smaller than the
3540// maximum bandwidth estimate by the overhead rate.
michaelt273f31b2017-02-08 08:21:52 -08003541TEST_F(VideoSendStreamTest, RemoveOverheadFromBandwidth) {
michaelta3328772016-11-29 09:25:03 -08003542 test::ScopedFieldTrials override_field_trials(
3543 "WebRTC-SendSideBwe-WithOverhead/Enabled/");
3544 class RemoveOverheadFromBandwidthTest : public test::EndToEndTest,
3545 public test::FakeEncoder {
3546 public:
eladalon413ee9a2017-08-22 04:02:52 -07003547 explicit RemoveOverheadFromBandwidthTest(
3548 test::SingleThreadedTaskQueueForTesting* task_queue)
michaelta3328772016-11-29 09:25:03 -08003549 : EndToEndTest(test::CallTest::kDefaultTimeoutMs),
3550 FakeEncoder(Clock::GetRealTimeClock()),
eladalon413ee9a2017-08-22 04:02:52 -07003551 task_queue_(task_queue),
michaelta3328772016-11-29 09:25:03 -08003552 call_(nullptr),
michaelt192132e2017-01-26 09:05:27 -08003553 max_bitrate_bps_(0),
3554 first_packet_sent_(false),
3555 bitrate_changed_event_(false, false) {}
michaelta3328772016-11-29 09:25:03 -08003556
3557 int32_t SetRateAllocation(const BitrateAllocation& bitrate,
3558 uint32_t frameRate) override {
3559 rtc::CritScope lock(&crit_);
michaelt192132e2017-01-26 09:05:27 -08003560 // Wait for the first sent packet so that videosendstream knows
3561 // rtp_overhead.
3562 if (first_packet_sent_) {
3563 max_bitrate_bps_ = bitrate.get_sum_bps();
3564 bitrate_changed_event_.Set();
3565 }
michaelta3328772016-11-29 09:25:03 -08003566 return FakeEncoder::SetRateAllocation(bitrate, frameRate);
3567 }
3568
3569 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
3570 call_ = sender_call;
3571 }
3572
3573 void ModifyVideoConfigs(
3574 VideoSendStream::Config* send_config,
3575 std::vector<VideoReceiveStream::Config>* receive_configs,
3576 VideoEncoderConfig* encoder_config) override {
3577 send_config->rtp.max_packet_size = 1200;
3578 send_config->encoder_settings.encoder = this;
3579 EXPECT_FALSE(send_config->rtp.extensions.empty());
3580 }
3581
michaelt192132e2017-01-26 09:05:27 -08003582 Action OnSendRtp(const uint8_t* packet, size_t length) override {
3583 rtc::CritScope lock(&crit_);
3584 first_packet_sent_ = true;
3585 return SEND_PACKET;
3586 }
3587
michaelta3328772016-11-29 09:25:03 -08003588 void PerformTest() override {
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003589 BitrateConstraints bitrate_config;
michaelt192132e2017-01-26 09:05:27 -08003590 constexpr int kStartBitrateBps = 60000;
michaelta3328772016-11-29 09:25:03 -08003591 constexpr int kMaxBitrateBps = 60000;
michaelt192132e2017-01-26 09:05:27 -08003592 constexpr int kMinBitrateBps = 10000;
michaelta3328772016-11-29 09:25:03 -08003593 bitrate_config.start_bitrate_bps = kStartBitrateBps;
3594 bitrate_config.max_bitrate_bps = kMaxBitrateBps;
michaelt192132e2017-01-26 09:05:27 -08003595 bitrate_config.min_bitrate_bps = kMinBitrateBps;
eladalon413ee9a2017-08-22 04:02:52 -07003596 task_queue_->SendTask([this, &bitrate_config]() {
Sebastian Jansson8f83b422018-02-21 13:07:13 +01003597 call_->GetTransportControllerSend()->SetSdpBitrateParameters(
3598 bitrate_config);
eladalon413ee9a2017-08-22 04:02:52 -07003599 call_->OnTransportOverheadChanged(webrtc::MediaType::VIDEO, 40);
3600 });
michaelta3328772016-11-29 09:25:03 -08003601
3602 // At a bitrate of 60kbps with a packet size of 1200B video and an
michaelt192132e2017-01-26 09:05:27 -08003603 // overhead of 40B per packet video produces 2240bps overhead.
3604 // So the encoder BW should be set to 57760bps.
3605 bitrate_changed_event_.Wait(VideoSendStreamTest::kDefaultTimeoutMs);
michaelta3328772016-11-29 09:25:03 -08003606 {
3607 rtc::CritScope lock(&crit_);
michaelt273f31b2017-02-08 08:21:52 -08003608 EXPECT_LE(max_bitrate_bps_, 57760u);
michaelta3328772016-11-29 09:25:03 -08003609 }
3610 }
3611
3612 private:
eladalon413ee9a2017-08-22 04:02:52 -07003613 test::SingleThreadedTaskQueueForTesting* const task_queue_;
michaelta3328772016-11-29 09:25:03 -08003614 Call* call_;
3615 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -07003616 uint32_t max_bitrate_bps_ RTC_GUARDED_BY(&crit_);
3617 bool first_packet_sent_ RTC_GUARDED_BY(&crit_);
michaelt192132e2017-01-26 09:05:27 -08003618 rtc::Event bitrate_changed_event_;
eladalon413ee9a2017-08-22 04:02:52 -07003619 } test(&task_queue_);
michaelta3328772016-11-29 09:25:03 -08003620 RunBaseTest(&test);
3621}
3622
sprang168794c2017-07-06 04:38:06 -07003623TEST_F(VideoSendStreamTest, SendsKeepAlive) {
3624 const int kTimeoutMs = 50; // Really short timeout for testing.
sprang168794c2017-07-06 04:38:06 -07003625
3626 class KeepaliveObserver : public test::SendTest {
3627 public:
3628 KeepaliveObserver() : SendTest(kDefaultTimeoutMs) {}
3629
sprangdb2a9fc2017-08-09 06:42:32 -07003630 void OnRtpTransportControllerSendCreated(
3631 RtpTransportControllerSend* controller) override {
3632 RtpKeepAliveConfig config;
3633 config.timeout_interval_ms = kTimeoutMs;
3634 config.payload_type = CallTest::kDefaultKeepalivePayloadType;
3635 controller->SetKeepAliveConfig(config);
sprange5c4a812017-07-11 03:44:17 -07003636 }
3637
sprang168794c2017-07-06 04:38:06 -07003638 private:
3639 Action OnSendRtp(const uint8_t* packet, size_t length) override {
3640 RTPHeader header;
3641 EXPECT_TRUE(parser_->Parse(packet, length, &header));
3642
sprangd2702ef2017-07-10 08:41:10 -07003643 if (header.payloadType != CallTest::kDefaultKeepalivePayloadType) {
sprang168794c2017-07-06 04:38:06 -07003644 // The video stream has started. Stop it now.
3645 if (capturer_)
3646 capturer_->Stop();
3647 } else {
3648 observation_complete_.Set();
3649 }
3650
3651 return SEND_PACKET;
3652 }
3653
sprang168794c2017-07-06 04:38:06 -07003654 void PerformTest() override {
3655 EXPECT_TRUE(Wait()) << "Timed out while waiting for keep-alive packet.";
3656 }
3657
3658 void OnFrameGeneratorCapturerCreated(
3659 test::FrameGeneratorCapturer* frame_generator_capturer) override {
3660 capturer_ = frame_generator_capturer;
3661 }
3662
3663 test::FrameGeneratorCapturer* capturer_ = nullptr;
3664 } test;
3665
3666 RunBaseTest(&test);
3667}
3668
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003669class PacingFactorObserver : public test::SendTest {
3670 public:
3671 PacingFactorObserver(bool configure_send_side,
3672 rtc::Optional<float> expected_pacing_factor)
3673 : test::SendTest(VideoSendStreamTest::kDefaultTimeoutMs),
3674 configure_send_side_(configure_send_side),
3675 expected_pacing_factor_(expected_pacing_factor) {}
Erik Språng7c8cca32017-10-24 17:05:18 +02003676
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003677 void ModifyVideoConfigs(
3678 VideoSendStream::Config* send_config,
3679 std::vector<VideoReceiveStream::Config>* receive_configs,
3680 VideoEncoderConfig* encoder_config) override {
3681 // Check if send-side bwe extension is already present, and remove it if
3682 // it is not desired.
3683 bool has_send_side = false;
3684 for (auto it = send_config->rtp.extensions.begin();
3685 it != send_config->rtp.extensions.end(); ++it) {
3686 if (it->uri == RtpExtension::kTransportSequenceNumberUri) {
3687 if (configure_send_side_) {
3688 has_send_side = true;
3689 } else {
3690 send_config->rtp.extensions.erase(it);
Erik Språng7c8cca32017-10-24 17:05:18 +02003691 }
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003692 break;
Erik Språng7c8cca32017-10-24 17:05:18 +02003693 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003694 }
3695
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003696 if (configure_send_side_ && !has_send_side) {
3697 // Want send side, not present by default, so add it.
3698 send_config->rtp.extensions.emplace_back(
3699 RtpExtension::kTransportSequenceNumberUri,
3700 RtpExtension::kTransportSequenceNumberDefaultId);
Erik Språng7c8cca32017-10-24 17:05:18 +02003701 }
3702
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003703 // ALR only enabled for screenshare.
3704 encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
3705 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003706
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003707 void OnVideoStreamsCreated(
3708 VideoSendStream* send_stream,
3709 const std::vector<VideoReceiveStream*>& receive_streams) override {
3710 auto internal_send_peer = test::VideoSendStreamPeer(send_stream);
3711 // Video streams created, check that pacing factor is correctly configured.
3712 EXPECT_EQ(expected_pacing_factor_,
3713 internal_send_peer.GetPacingFactorOverride());
3714 observation_complete_.Set();
3715 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003716
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003717 void PerformTest() override {
3718 EXPECT_TRUE(Wait()) << "Timed out while waiting for stream creation.";
3719 }
Erik Språng7c8cca32017-10-24 17:05:18 +02003720
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003721 private:
3722 const bool configure_send_side_;
3723 const rtc::Optional<float> expected_pacing_factor_;
3724};
3725
3726std::string GetAlrProbingExperimentString() {
3727 return std::string(
3728 AlrExperimentSettings::kScreenshareProbingBweExperimentName) +
3729 "/1.0,2875,80,40,-60,3/";
3730}
3731const float kAlrProbingExperimentPaceMultiplier = 1.0f;
3732
3733TEST_F(VideoSendStreamTest, AlrConfiguredWhenSendSideOn) {
3734 test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
Erik Språng7c8cca32017-10-24 17:05:18 +02003735 // Send-side bwe on, use pacing factor from |kAlrProbingExperiment| above.
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003736 PacingFactorObserver test_with_send_side(true,
3737 kAlrProbingExperimentPaceMultiplier);
Erik Språng7c8cca32017-10-24 17:05:18 +02003738 RunBaseTest(&test_with_send_side);
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003739}
Erik Språng7c8cca32017-10-24 17:05:18 +02003740
Sebastian Janssona45c8da2018-01-16 10:55:29 +01003741TEST_F(VideoSendStreamTest, AlrNotConfiguredWhenSendSideOff) {
3742 test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
3743 // Send-side bwe off, use configuration should not be overridden.
3744 PacingFactorObserver test_without_send_side(false, rtc::nullopt);
Erik Språng7c8cca32017-10-24 17:05:18 +02003745 RunBaseTest(&test_without_send_side);
3746}
3747
pbos@webrtc.org119a1cc2013-08-20 13:14:07 +00003748} // namespace webrtc