Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: webrtc/media/engine/webrtcvideoengine2_unittest.cc

Issue 2932073002: s/WebRtcVideoChannel2/WebRtcVideoChannel and s/WebRtcVideoEngine2/WebRtcVideoEngine (Closed)
Patch Set: . Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <algorithm>
12 #include <map>
13 #include <memory>
14 #include <vector>
15
16 #include "webrtc/api/video_codecs/video_encoder.h"
17 #include "webrtc/base/arraysize.h"
18 #include "webrtc/base/gunit.h"
19 #include "webrtc/base/stringutils.h"
20 #include "webrtc/call/flexfec_receive_stream.h"
21 #include "webrtc/common_video/h264/profile_level_id.h"
22 #include "webrtc/logging/rtc_event_log/rtc_event_log.h"
23 #include "webrtc/media/base/mediaconstants.h"
24 #include "webrtc/media/base/rtputils.h"
25 #include "webrtc/media/base/testutils.h"
26 #include "webrtc/media/base/videoengine_unittest.h"
27 #include "webrtc/media/engine/constants.h"
28 #include "webrtc/media/engine/fakewebrtccall.h"
29 #include "webrtc/media/engine/fakewebrtcvideoengine.h"
30 #include "webrtc/media/engine/simulcast.h"
31 #include "webrtc/media/engine/webrtcvideoengine2.h"
32 #include "webrtc/media/engine/webrtcvoiceengine.h"
33 #include "webrtc/test/field_trial.h"
34
35 using webrtc::RtpExtension;
36
37 namespace {
38 static const int kDefaultQpMax = 56;
39
40 static const uint8_t kRedRtxPayloadType = 125;
41
42 static const uint32_t kSsrcs1[] = {1};
43 static const uint32_t kSsrcs3[] = {1, 2, 3};
44 static const uint32_t kRtxSsrcs1[] = {4};
45 static const uint32_t kFlexfecSsrc = 5;
46 static const uint32_t kIncomingUnsignalledSsrc = 0xC0FFEE;
47 static const uint32_t kDefaultRecvSsrc = 0;
48
49 static const char kUnsupportedExtensionName[] =
50 "urn:ietf:params:rtp-hdrext:unsupported";
51
52 cricket::VideoCodec RemoveFeedbackParams(cricket::VideoCodec&& codec) {
53 codec.feedback_params = cricket::FeedbackParams();
54 return codec;
55 }
56
57 void VerifyCodecHasDefaultFeedbackParams(const cricket::VideoCodec& codec) {
58 EXPECT_TRUE(codec.HasFeedbackParam(cricket::FeedbackParam(
59 cricket::kRtcpFbParamNack, cricket::kParamValueEmpty)));
60 EXPECT_TRUE(codec.HasFeedbackParam(cricket::FeedbackParam(
61 cricket::kRtcpFbParamNack, cricket::kRtcpFbNackParamPli)));
62 EXPECT_TRUE(codec.HasFeedbackParam(cricket::FeedbackParam(
63 cricket::kRtcpFbParamRemb, cricket::kParamValueEmpty)));
64 EXPECT_TRUE(codec.HasFeedbackParam(cricket::FeedbackParam(
65 cricket::kRtcpFbParamTransportCc, cricket::kParamValueEmpty)));
66 EXPECT_TRUE(codec.HasFeedbackParam(cricket::FeedbackParam(
67 cricket::kRtcpFbParamCcm, cricket::kRtcpFbCcmParamFir)));
68 }
69
70 // Return true if any codec in |codecs| is an RTX codec with associated payload
71 // type |payload_type|.
72 bool HasRtxCodec(const std::vector<cricket::VideoCodec>& codecs,
73 int payload_type) {
74 for (const cricket::VideoCodec& codec : codecs) {
75 int associated_payload_type;
76 if (cricket::CodecNamesEq(codec.name.c_str(), "rtx") &&
77 codec.GetParam(cricket::kCodecParamAssociatedPayloadType,
78 &associated_payload_type) &&
79 associated_payload_type == payload_type) {
80 return true;
81 }
82 }
83 return false;
84 }
85
86 rtc::scoped_refptr<webrtc::VideoFrameBuffer> CreateBlackFrameBuffer(
87 int width,
88 int height) {
89 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
90 webrtc::I420Buffer::Create(width, height);
91 webrtc::I420Buffer::SetBlack(buffer);
92 return buffer;
93 }
94
95 void VerifySendStreamHasRtxTypes(const webrtc::VideoSendStream::Config& config,
96 const std::map<int, int>& rtx_types) {
97 std::map<int, int>::const_iterator it;
98 it = rtx_types.find(config.encoder_settings.payload_type);
99 EXPECT_TRUE(it != rtx_types.end() &&
100 it->second == config.rtp.rtx.payload_type);
101
102 if (config.rtp.ulpfec.red_rtx_payload_type != -1) {
103 it = rtx_types.find(config.rtp.ulpfec.red_payload_type);
104 EXPECT_TRUE(it != rtx_types.end() &&
105 it->second == config.rtp.ulpfec.red_rtx_payload_type);
106 }
107 }
108
109 cricket::MediaConfig GetMediaConfig() {
110 cricket::MediaConfig media_config;
111 media_config.video.enable_cpu_overuse_detection = false;
112 return media_config;
113 }
114 } // namespace
115
116 namespace cricket {
117 class WebRtcVideoEngine2Test : public ::testing::Test {
118 public:
119 WebRtcVideoEngine2Test() : WebRtcVideoEngine2Test("") {}
120 explicit WebRtcVideoEngine2Test(const char* field_trials)
121 : override_field_trials_(field_trials),
122 call_(webrtc::Call::Create(webrtc::Call::Config(&event_log_))),
123 engine_() {
124 std::vector<VideoCodec> engine_codecs = engine_.codecs();
125 RTC_DCHECK(!engine_codecs.empty());
126 bool codec_set = false;
127 for (const cricket::VideoCodec& codec : engine_codecs) {
128 if (codec.name == "rtx") {
129 int associated_payload_type;
130 if (codec.GetParam(kCodecParamAssociatedPayloadType,
131 &associated_payload_type)) {
132 default_apt_rtx_types_[associated_payload_type] = codec.id;
133 }
134 } else if (!codec_set && codec.name != "red" && codec.name != "ulpfec") {
135 default_codec_ = codec;
136 codec_set = true;
137 }
138 }
139
140 RTC_DCHECK(codec_set);
141 }
142
143 protected:
144 // Find the codec in the engine with the given name. The codec must be
145 // present.
146 cricket::VideoCodec GetEngineCodec(const std::string& name);
147
148 VideoMediaChannel* SetUpForExternalEncoderFactory(
149 cricket::WebRtcVideoEncoderFactory* encoder_factory);
150
151 VideoMediaChannel* SetUpForExternalDecoderFactory(
152 cricket::WebRtcVideoDecoderFactory* decoder_factory,
153 const std::vector<VideoCodec>& codecs);
154
155 void TestExtendedEncoderOveruse(bool use_external_encoder);
156
157 webrtc::test::ScopedFieldTrials override_field_trials_;
158 webrtc::RtcEventLogNullImpl event_log_;
159 // Used in WebRtcVideoEngine2VoiceTest, but defined here so it's properly
160 // initialized when the constructor is called.
161 std::unique_ptr<webrtc::Call> call_;
162 WebRtcVideoEngine2 engine_;
163 VideoCodec default_codec_;
164 std::map<int, int> default_apt_rtx_types_;
165 };
166
167 TEST_F(WebRtcVideoEngine2Test, AnnouncesVp9AccordingToBuildFlags) {
168 bool claims_vp9_support = false;
169 for (const cricket::VideoCodec& codec : engine_.codecs()) {
170 if (codec.name == "VP9") {
171 claims_vp9_support = true;
172 break;
173 }
174 }
175 #if defined(RTC_DISABLE_VP9)
176 EXPECT_FALSE(claims_vp9_support);
177 #else
178 EXPECT_TRUE(claims_vp9_support);
179 #endif // defined(RTC_DISABLE_VP9)
180 }
181
182 TEST_F(WebRtcVideoEngine2Test, DefaultRtxCodecHasAssociatedPayloadTypeSet) {
183 std::vector<VideoCodec> engine_codecs = engine_.codecs();
184 for (size_t i = 0; i < engine_codecs.size(); ++i) {
185 if (engine_codecs[i].name != kRtxCodecName)
186 continue;
187 int associated_payload_type;
188 EXPECT_TRUE(engine_codecs[i].GetParam(kCodecParamAssociatedPayloadType,
189 &associated_payload_type));
190 EXPECT_EQ(default_codec_.id, associated_payload_type);
191 return;
192 }
193 FAIL() << "No RTX codec found among default codecs.";
194 }
195
196 TEST_F(WebRtcVideoEngine2Test, SupportsTimestampOffsetHeaderExtension) {
197 RtpCapabilities capabilities = engine_.GetCapabilities();
198 ASSERT_FALSE(capabilities.header_extensions.empty());
199 for (const RtpExtension& extension : capabilities.header_extensions) {
200 if (extension.uri == RtpExtension::kTimestampOffsetUri) {
201 EXPECT_EQ(RtpExtension::kTimestampOffsetDefaultId, extension.id);
202 return;
203 }
204 }
205 FAIL() << "Timestamp offset extension not in header-extension list.";
206 }
207
208 TEST_F(WebRtcVideoEngine2Test, SupportsAbsoluteSenderTimeHeaderExtension) {
209 RtpCapabilities capabilities = engine_.GetCapabilities();
210 ASSERT_FALSE(capabilities.header_extensions.empty());
211 for (const RtpExtension& extension : capabilities.header_extensions) {
212 if (extension.uri == RtpExtension::kAbsSendTimeUri) {
213 EXPECT_EQ(RtpExtension::kAbsSendTimeDefaultId, extension.id);
214 return;
215 }
216 }
217 FAIL() << "Absolute Sender Time extension not in header-extension list.";
218 }
219
220 TEST_F(WebRtcVideoEngine2Test, SupportsTransportSequenceNumberHeaderExtension) {
221 RtpCapabilities capabilities = engine_.GetCapabilities();
222 ASSERT_FALSE(capabilities.header_extensions.empty());
223 for (const RtpExtension& extension : capabilities.header_extensions) {
224 if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
225 EXPECT_EQ(RtpExtension::kTransportSequenceNumberDefaultId, extension.id);
226 return;
227 }
228 }
229 FAIL() << "Transport sequence number extension not in header-extension list.";
230 }
231
232 TEST_F(WebRtcVideoEngine2Test, SupportsVideoRotationHeaderExtension) {
233 RtpCapabilities capabilities = engine_.GetCapabilities();
234 ASSERT_FALSE(capabilities.header_extensions.empty());
235 for (const RtpExtension& extension : capabilities.header_extensions) {
236 if (extension.uri == RtpExtension::kVideoRotationUri) {
237 EXPECT_EQ(RtpExtension::kVideoRotationDefaultId, extension.id);
238 return;
239 }
240 }
241 FAIL() << "Video Rotation extension not in header-extension list.";
242 }
243
244 TEST_F(WebRtcVideoEngine2Test, CVOSetHeaderExtensionBeforeCapturer) {
245 // Allocate the capturer first to prevent early destruction before channel's
246 // dtor is called.
247 cricket::FakeVideoCapturer capturer;
248
249 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
250 encoder_factory.AddSupportedVideoCodecType("VP8");
251
252 std::unique_ptr<VideoMediaChannel> channel(
253 SetUpForExternalEncoderFactory(&encoder_factory));
254 EXPECT_TRUE(channel->AddSendStream(StreamParams::CreateLegacy(kSsrc)));
255
256 // Add CVO extension.
257 const int id = 1;
258 cricket::VideoSendParameters parameters;
259 parameters.codecs.push_back(GetEngineCodec("VP8"));
260 parameters.extensions.push_back(
261 RtpExtension(RtpExtension::kVideoRotationUri, id));
262 EXPECT_TRUE(channel->SetSendParameters(parameters));
263
264 // Set capturer.
265 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, nullptr, &capturer));
266
267 // Verify capturer has turned off applying rotation.
268 EXPECT_FALSE(capturer.apply_rotation());
269
270 // Verify removing header extension turns on applying rotation.
271 parameters.extensions.clear();
272 EXPECT_TRUE(channel->SetSendParameters(parameters));
273 EXPECT_TRUE(capturer.apply_rotation());
274 }
275
276
277 // TODO(ilnik): Remove this test once field trial is gone.
278 TEST_F(WebRtcVideoEngine2Test, SupportsVideoContentTypeHeaderExtension) {
279 // Extension shound not be reported outside of the field trial.
280 RtpCapabilities capabilities = engine_.GetCapabilities();
281 EXPECT_FALSE(capabilities.header_extensions.empty());
282 for (const RtpExtension& extension : capabilities.header_extensions) {
283 EXPECT_NE(extension.uri, RtpExtension::kVideoContentTypeUri);
284 }
285 webrtc::test::ScopedFieldTrials override_field_trials_(
286 "WebRTC-VideoContentTypeExtension/Enabled/");
287 // Should be reported within field trial.
288 capabilities = engine_.GetCapabilities();
289 EXPECT_FALSE(capabilities.header_extensions.empty());
290 for (const RtpExtension& extension : capabilities.header_extensions) {
291 if (extension.uri == RtpExtension::kVideoContentTypeUri) {
292 EXPECT_EQ(RtpExtension::kVideoContentTypeDefaultId, extension.id);
293 return;
294 }
295 }
296 FAIL() << "Video Content Type extension not in header-extension list.";
297 }
298
299 TEST_F(WebRtcVideoEngine2Test, CVOSetHeaderExtensionBeforeAddSendStream) {
300 // Allocate the capturer first to prevent early destruction before channel's
301 // dtor is called.
302 cricket::FakeVideoCapturer capturer;
303
304 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
305 encoder_factory.AddSupportedVideoCodecType("VP8");
306
307 std::unique_ptr<VideoMediaChannel> channel(
308 SetUpForExternalEncoderFactory(&encoder_factory));
309 // Add CVO extension.
310 const int id = 1;
311 cricket::VideoSendParameters parameters;
312 parameters.codecs.push_back(GetEngineCodec("VP8"));
313 parameters.extensions.push_back(
314 RtpExtension(RtpExtension::kVideoRotationUri, id));
315 EXPECT_TRUE(channel->SetSendParameters(parameters));
316 EXPECT_TRUE(channel->AddSendStream(StreamParams::CreateLegacy(kSsrc)));
317
318 // Set capturer.
319 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, nullptr, &capturer));
320
321 // Verify capturer has turned off applying rotation.
322 EXPECT_FALSE(capturer.apply_rotation());
323 }
324
325 TEST_F(WebRtcVideoEngine2Test, CVOSetHeaderExtensionAfterCapturer) {
326 cricket::FakeVideoCapturer capturer;
327
328 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
329 encoder_factory.AddSupportedVideoCodecType("VP8");
330 encoder_factory.AddSupportedVideoCodecType("VP9");
331
332 std::unique_ptr<VideoMediaChannel> channel(
333 SetUpForExternalEncoderFactory(&encoder_factory));
334 EXPECT_TRUE(channel->AddSendStream(StreamParams::CreateLegacy(kSsrc)));
335
336 // Set capturer.
337 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, nullptr, &capturer));
338
339 // Verify capturer has turned on applying rotation.
340 EXPECT_TRUE(capturer.apply_rotation());
341
342 // Add CVO extension.
343 const int id = 1;
344 cricket::VideoSendParameters parameters;
345 parameters.codecs.push_back(GetEngineCodec("VP8"));
346 parameters.codecs.push_back(GetEngineCodec("VP9"));
347 parameters.extensions.push_back(
348 RtpExtension(RtpExtension::kVideoRotationUri, id));
349 // Also remove the first codec to trigger a codec change as well.
350 parameters.codecs.erase(parameters.codecs.begin());
351 EXPECT_TRUE(channel->SetSendParameters(parameters));
352
353 // Verify capturer has turned off applying rotation.
354 EXPECT_FALSE(capturer.apply_rotation());
355
356 // Verify removing header extension turns on applying rotation.
357 parameters.extensions.clear();
358 EXPECT_TRUE(channel->SetSendParameters(parameters));
359 EXPECT_TRUE(capturer.apply_rotation());
360 }
361
362 TEST_F(WebRtcVideoEngine2Test, SetSendFailsBeforeSettingCodecs) {
363 engine_.Init();
364 std::unique_ptr<VideoMediaChannel> channel(
365 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions()));
366
367 EXPECT_TRUE(channel->AddSendStream(StreamParams::CreateLegacy(123)));
368
369 EXPECT_FALSE(channel->SetSend(true))
370 << "Channel should not start without codecs.";
371 EXPECT_TRUE(channel->SetSend(false))
372 << "Channel should be stoppable even without set codecs.";
373 }
374
375 TEST_F(WebRtcVideoEngine2Test, GetStatsWithoutSendCodecsSetDoesNotCrash) {
376 engine_.Init();
377 std::unique_ptr<VideoMediaChannel> channel(
378 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions()));
379 EXPECT_TRUE(channel->AddSendStream(StreamParams::CreateLegacy(123)));
380 VideoMediaInfo info;
381 channel->GetStats(&info);
382 }
383
384 TEST_F(WebRtcVideoEngine2Test, UseExternalFactoryForVp8WhenSupported) {
385 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
386 encoder_factory.AddSupportedVideoCodecType("VP8");
387
388 std::unique_ptr<VideoMediaChannel> channel(
389 SetUpForExternalEncoderFactory(&encoder_factory));
390 channel->OnReadyToSend(true);
391
392 EXPECT_TRUE(
393 channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
394 EXPECT_EQ(0, encoder_factory.GetNumCreatedEncoders());
395 EXPECT_TRUE(channel->SetSend(true));
396 cricket::FakeVideoCapturer capturer;
397 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, nullptr, &capturer));
398 EXPECT_EQ(cricket::CS_RUNNING,
399 capturer.Start(capturer.GetSupportedFormats()->front()));
400 EXPECT_TRUE(capturer.CaptureFrame());
401 // Sending one frame will have allocate the encoder.
402 ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(1));
403 EXPECT_TRUE_WAIT(encoder_factory.encoders()[0]->GetNumEncodedFrames() > 0,
404 kTimeout);
405
406 int num_created_encoders = encoder_factory.GetNumCreatedEncoders();
407 EXPECT_EQ(num_created_encoders, 1);
408
409 // Setting codecs of the same type should not reallocate any encoders
410 // (expecting a no-op).
411 cricket::VideoSendParameters parameters;
412 parameters.codecs.push_back(GetEngineCodec("VP8"));
413 EXPECT_TRUE(channel->SetSendParameters(parameters));
414 EXPECT_EQ(num_created_encoders, encoder_factory.GetNumCreatedEncoders());
415
416 // Remove stream previously added to free the external encoder instance.
417 EXPECT_TRUE(channel->RemoveSendStream(kSsrc));
418 EXPECT_EQ(0u, encoder_factory.encoders().size());
419 }
420
421 // Test that when an external encoder factory supports a codec we don't
422 // internally support, we still add an RTX codec for it.
423 // TODO(deadbeef): Currently this test is only effective if WebRTC is
424 // built with no internal H264 support. This test should be updated
425 // if/when we start adding RTX codecs for unrecognized codec names.
426 TEST_F(WebRtcVideoEngine2Test, RtxCodecAddedForExternalCodec) {
427 using webrtc::H264::ProfileLevelIdToString;
428 using webrtc::H264::ProfileLevelId;
429 using webrtc::H264::kLevel1;
430 cricket::VideoCodec h264_constrained_baseline("H264");
431 h264_constrained_baseline.params[kH264FmtpProfileLevelId] =
432 *ProfileLevelIdToString(
433 ProfileLevelId(webrtc::H264::kProfileConstrainedBaseline, kLevel1));
434 cricket::VideoCodec h264_constrained_high("H264");
435 h264_constrained_high.params[kH264FmtpProfileLevelId] =
436 *ProfileLevelIdToString(
437 ProfileLevelId(webrtc::H264::kProfileConstrainedHigh, kLevel1));
438 cricket::VideoCodec h264_high("H264");
439 h264_high.params[kH264FmtpProfileLevelId] = *ProfileLevelIdToString(
440 ProfileLevelId(webrtc::H264::kProfileHigh, kLevel1));
441
442 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
443 encoder_factory.AddSupportedVideoCodec(h264_constrained_baseline);
444 encoder_factory.AddSupportedVideoCodec(h264_constrained_high);
445 encoder_factory.AddSupportedVideoCodec(h264_high);
446 engine_.SetExternalEncoderFactory(&encoder_factory);
447 engine_.Init();
448
449 // First figure out what payload types the test codecs got assigned.
450 const std::vector<cricket::VideoCodec> codecs = engine_.codecs();
451 // Now search for RTX codecs for them. Expect that they all have associated
452 // RTX codecs.
453 EXPECT_TRUE(HasRtxCodec(
454 codecs, FindMatchingCodec(codecs, h264_constrained_baseline)->id));
455 EXPECT_TRUE(HasRtxCodec(
456 codecs, FindMatchingCodec(codecs, h264_constrained_high)->id));
457 EXPECT_TRUE(HasRtxCodec(
458 codecs, FindMatchingCodec(codecs, h264_high)->id));
459 }
460
461 void WebRtcVideoEngine2Test::TestExtendedEncoderOveruse(
462 bool use_external_encoder) {
463 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
464 encoder_factory.AddSupportedVideoCodecType("VP8");
465 std::unique_ptr<VideoMediaChannel> channel;
466 FakeCall* fake_call = new FakeCall(webrtc::Call::Config(&event_log_));
467 call_.reset(fake_call);
468 if (use_external_encoder) {
469 channel.reset(SetUpForExternalEncoderFactory(&encoder_factory));
470 } else {
471 engine_.Init();
472 channel.reset(
473 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions()));
474 }
475 ASSERT_TRUE(
476 channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
477 cricket::VideoSendParameters parameters;
478 parameters.codecs.push_back(GetEngineCodec("VP8"));
479 EXPECT_TRUE(channel->SetSendParameters(parameters));
480 EXPECT_TRUE(channel->SetSend(true));
481 FakeVideoSendStream* stream = fake_call->GetVideoSendStreams()[0];
482
483 EXPECT_EQ(use_external_encoder,
484 stream->GetConfig().encoder_settings.full_overuse_time);
485 // Remove stream previously added to free the external encoder instance.
486 EXPECT_TRUE(channel->RemoveSendStream(kSsrc));
487 }
488
489 TEST_F(WebRtcVideoEngine2Test, EnablesFullEncoderTimeForExternalEncoders) {
490 TestExtendedEncoderOveruse(true);
491 }
492
493 TEST_F(WebRtcVideoEngine2Test, DisablesFullEncoderTimeForNonExternalEncoders) {
494 TestExtendedEncoderOveruse(false);
495 }
496
497 #if !defined(RTC_DISABLE_VP9)
498 TEST_F(WebRtcVideoEngine2Test, CanConstructDecoderForVp9EncoderFactory) {
499 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
500 encoder_factory.AddSupportedVideoCodecType("VP9");
501
502 std::unique_ptr<VideoMediaChannel> channel(
503 SetUpForExternalEncoderFactory(&encoder_factory));
504
505 EXPECT_TRUE(
506 channel->AddRecvStream(cricket::StreamParams::CreateLegacy(kSsrc)));
507 }
508 #endif // !defined(RTC_DISABLE_VP9)
509
510 TEST_F(WebRtcVideoEngine2Test, PropagatesInputFrameTimestamp) {
511 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
512 encoder_factory.AddSupportedVideoCodecType("VP8");
513 FakeCall* fake_call = new FakeCall(webrtc::Call::Config(&event_log_));
514 call_.reset(fake_call);
515 std::unique_ptr<VideoMediaChannel> channel(
516 SetUpForExternalEncoderFactory(&encoder_factory));
517
518 EXPECT_TRUE(
519 channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
520
521 FakeVideoCapturer capturer;
522 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, nullptr, &capturer));
523 capturer.Start(cricket::VideoFormat(1280, 720,
524 cricket::VideoFormat::FpsToInterval(60),
525 cricket::FOURCC_I420));
526 channel->SetSend(true);
527
528 FakeVideoSendStream* stream = fake_call->GetVideoSendStreams()[0];
529
530 EXPECT_TRUE(capturer.CaptureFrame());
531 int64_t last_timestamp = stream->GetLastTimestamp();
532 for (int i = 0; i < 10; i++) {
533 EXPECT_TRUE(capturer.CaptureFrame());
534 int64_t timestamp = stream->GetLastTimestamp();
535 int64_t interval = timestamp - last_timestamp;
536
537 // Precision changes from nanosecond to millisecond.
538 // Allow error to be no more than 1.
539 EXPECT_NEAR(cricket::VideoFormat::FpsToInterval(60) / 1E6, interval, 1);
540
541 last_timestamp = timestamp;
542 }
543
544 capturer.Start(cricket::VideoFormat(1280, 720,
545 cricket::VideoFormat::FpsToInterval(30),
546 cricket::FOURCC_I420));
547
548 EXPECT_TRUE(capturer.CaptureFrame());
549 last_timestamp = stream->GetLastTimestamp();
550 for (int i = 0; i < 10; i++) {
551 EXPECT_TRUE(capturer.CaptureFrame());
552 int64_t timestamp = stream->GetLastTimestamp();
553 int64_t interval = timestamp - last_timestamp;
554
555 // Precision changes from nanosecond to millisecond.
556 // Allow error to be no more than 1.
557 EXPECT_NEAR(cricket::VideoFormat::FpsToInterval(30) / 1E6, interval, 1);
558
559 last_timestamp = timestamp;
560 }
561
562 // Remove stream previously added to free the external encoder instance.
563 EXPECT_TRUE(channel->RemoveSendStream(kSsrc));
564 }
565
566 cricket::VideoCodec WebRtcVideoEngine2Test::GetEngineCodec(
567 const std::string& name) {
568 for (const cricket::VideoCodec& engine_codec : engine_.codecs()) {
569 if (CodecNamesEq(name, engine_codec.name))
570 return engine_codec;
571 }
572 // This point should never be reached.
573 ADD_FAILURE() << "Unrecognized codec name: " << name;
574 return cricket::VideoCodec();
575 }
576
577 VideoMediaChannel* WebRtcVideoEngine2Test::SetUpForExternalEncoderFactory(
578 cricket::WebRtcVideoEncoderFactory* encoder_factory) {
579 engine_.SetExternalEncoderFactory(encoder_factory);
580 engine_.Init();
581
582 VideoMediaChannel* channel =
583 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions());
584 cricket::VideoSendParameters parameters;
585 // We need to look up the codec in the engine to get the correct payload type.
586 for (const VideoCodec& codec : encoder_factory->supported_codecs())
587 parameters.codecs.push_back(GetEngineCodec(codec.name));
588
589 EXPECT_TRUE(channel->SetSendParameters(parameters));
590
591 return channel;
592 }
593
594 VideoMediaChannel* WebRtcVideoEngine2Test::SetUpForExternalDecoderFactory(
595 cricket::WebRtcVideoDecoderFactory* decoder_factory,
596 const std::vector<VideoCodec>& codecs) {
597 engine_.SetExternalDecoderFactory(decoder_factory);
598 engine_.Init();
599
600 VideoMediaChannel* channel =
601 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions());
602 cricket::VideoRecvParameters parameters;
603 parameters.codecs = codecs;
604 EXPECT_TRUE(channel->SetRecvParameters(parameters));
605
606 return channel;
607 }
608
609 TEST_F(WebRtcVideoEngine2Test, UsesSimulcastAdapterForVp8Factories) {
610 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
611 encoder_factory.AddSupportedVideoCodecType("VP8");
612
613 std::unique_ptr<VideoMediaChannel> channel(
614 SetUpForExternalEncoderFactory(&encoder_factory));
615
616 std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs3);
617
618 EXPECT_TRUE(
619 channel->AddSendStream(CreateSimStreamParams("cname", ssrcs)));
620 EXPECT_TRUE(channel->SetSend(true));
621
622 cricket::FakeVideoCapturer capturer;
623 EXPECT_TRUE(channel->SetVideoSend(ssrcs.front(), true, nullptr, &capturer));
624 EXPECT_EQ(cricket::CS_RUNNING,
625 capturer.Start(capturer.GetSupportedFormats()->front()));
626 EXPECT_TRUE(capturer.CaptureFrame());
627
628 ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(2));
629
630 // Verify that encoders are configured for simulcast through adapter
631 // (increasing resolution and only configured to send one stream each).
632 int prev_width = -1;
633 for (size_t i = 0; i < encoder_factory.encoders().size(); ++i) {
634 ASSERT_TRUE(encoder_factory.encoders()[i]->WaitForInitEncode());
635 webrtc::VideoCodec codec_settings =
636 encoder_factory.encoders()[i]->GetCodecSettings();
637 EXPECT_EQ(0, codec_settings.numberOfSimulcastStreams);
638 EXPECT_GT(codec_settings.width, prev_width);
639 prev_width = codec_settings.width;
640 }
641
642 EXPECT_TRUE(channel->SetVideoSend(ssrcs.front(), true, nullptr, nullptr));
643
644 channel.reset();
645 ASSERT_EQ(0u, encoder_factory.encoders().size());
646 }
647
648 TEST_F(WebRtcVideoEngine2Test, ChannelWithExternalH264CanChangeToInternalVp8) {
649 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
650 encoder_factory.AddSupportedVideoCodecType("H264");
651
652 std::unique_ptr<VideoMediaChannel> channel(
653 SetUpForExternalEncoderFactory(&encoder_factory));
654
655 EXPECT_TRUE(
656 channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
657 ASSERT_EQ(1u, encoder_factory.encoders().size());
658
659 cricket::VideoSendParameters parameters;
660 parameters.codecs.push_back(GetEngineCodec("VP8"));
661 EXPECT_TRUE(channel->SetSendParameters(parameters));
662 ASSERT_EQ(0u, encoder_factory.encoders().size());
663 }
664
665 TEST_F(WebRtcVideoEngine2Test,
666 DontUseExternalEncoderFactoryForUnsupportedCodecs) {
667 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
668 encoder_factory.AddSupportedVideoCodecType("H264");
669
670 engine_.SetExternalEncoderFactory(&encoder_factory);
671 engine_.Init();
672
673 std::unique_ptr<VideoMediaChannel> channel(
674 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions()));
675 cricket::VideoSendParameters parameters;
676 parameters.codecs.push_back(GetEngineCodec("VP8"));
677 EXPECT_TRUE(channel->SetSendParameters(parameters));
678
679 EXPECT_TRUE(
680 channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
681 // Make sure DestroyVideoEncoder was called on the factory.
682 ASSERT_EQ(0u, encoder_factory.encoders().size());
683 }
684
685 TEST_F(WebRtcVideoEngine2Test,
686 UsesSimulcastAdapterForVp8WithCombinedVP8AndH264Factory) {
687 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
688 encoder_factory.AddSupportedVideoCodecType("VP8");
689 encoder_factory.AddSupportedVideoCodecType("H264");
690
691 engine_.SetExternalEncoderFactory(&encoder_factory);
692 engine_.Init();
693
694 std::unique_ptr<VideoMediaChannel> channel(
695 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions()));
696 cricket::VideoSendParameters parameters;
697 parameters.codecs.push_back(GetEngineCodec("VP8"));
698 EXPECT_TRUE(channel->SetSendParameters(parameters));
699
700 std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs3);
701
702 EXPECT_TRUE(
703 channel->AddSendStream(CreateSimStreamParams("cname", ssrcs)));
704 EXPECT_TRUE(channel->SetSend(true));
705
706 // Send a fake frame, or else the media engine will configure the simulcast
707 // encoder adapter at a low-enough size that it'll only create a single
708 // encoder layer.
709 cricket::FakeVideoCapturer capturer;
710 EXPECT_TRUE(channel->SetVideoSend(ssrcs.front(), true, nullptr, &capturer));
711 EXPECT_EQ(cricket::CS_RUNNING,
712 capturer.Start(capturer.GetSupportedFormats()->front()));
713 EXPECT_TRUE(capturer.CaptureFrame());
714
715 ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(2));
716 ASSERT_TRUE(encoder_factory.encoders()[0]->WaitForInitEncode());
717 EXPECT_EQ(webrtc::kVideoCodecVP8,
718 encoder_factory.encoders()[0]->GetCodecSettings().codecType);
719
720 channel.reset();
721 // Make sure DestroyVideoEncoder was called on the factory.
722 EXPECT_EQ(0u, encoder_factory.encoders().size());
723 }
724
725 TEST_F(WebRtcVideoEngine2Test,
726 DestroysNonSimulcastEncoderFromCombinedVP8AndH264Factory) {
727 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
728 encoder_factory.AddSupportedVideoCodecType("VP8");
729 encoder_factory.AddSupportedVideoCodecType("H264");
730
731 engine_.SetExternalEncoderFactory(&encoder_factory);
732 engine_.Init();
733
734 std::unique_ptr<VideoMediaChannel> channel(
735 engine_.CreateChannel(call_.get(), GetMediaConfig(), VideoOptions()));
736 cricket::VideoSendParameters parameters;
737 parameters.codecs.push_back(GetEngineCodec("H264"));
738 EXPECT_TRUE(channel->SetSendParameters(parameters));
739
740 EXPECT_TRUE(
741 channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
742 ASSERT_EQ(1u, encoder_factory.encoders().size());
743
744 // Send a frame of 720p. This should trigger a "real" encoder initialization.
745 cricket::VideoFormat format(
746 1280, 720, cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420);
747 cricket::FakeVideoCapturer capturer;
748 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, nullptr, &capturer));
749 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(format));
750 EXPECT_TRUE(capturer.CaptureFrame());
751 ASSERT_TRUE(encoder_factory.encoders()[0]->WaitForInitEncode());
752 EXPECT_EQ(webrtc::kVideoCodecH264,
753 encoder_factory.encoders()[0]->GetCodecSettings().codecType);
754
755 channel.reset();
756 // Make sure DestroyVideoEncoder was called on the factory.
757 ASSERT_EQ(0u, encoder_factory.encoders().size());
758 }
759
760 TEST_F(WebRtcVideoEngine2Test, SimulcastDisabledForH264) {
761 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
762 encoder_factory.AddSupportedVideoCodecType("H264");
763
764 std::unique_ptr<VideoMediaChannel> channel(
765 SetUpForExternalEncoderFactory(&encoder_factory));
766
767 const std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs3);
768 EXPECT_TRUE(
769 channel->AddSendStream(cricket::CreateSimStreamParams("cname", ssrcs)));
770
771 // Send a frame of 720p. This should trigger a "real" encoder initialization.
772 cricket::VideoFormat format(
773 1280, 720, cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420);
774 cricket::FakeVideoCapturer capturer;
775 EXPECT_TRUE(channel->SetVideoSend(ssrcs[0], true, nullptr, &capturer));
776 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(format));
777 EXPECT_TRUE(capturer.CaptureFrame());
778
779 ASSERT_EQ(1u, encoder_factory.encoders().size());
780 FakeWebRtcVideoEncoder* encoder = encoder_factory.encoders()[0];
781 ASSERT_TRUE(encoder_factory.encoders()[0]->WaitForInitEncode());
782 EXPECT_EQ(webrtc::kVideoCodecH264, encoder->GetCodecSettings().codecType);
783 EXPECT_EQ(1u, encoder->GetCodecSettings().numberOfSimulcastStreams);
784 EXPECT_TRUE(channel->SetVideoSend(ssrcs[0], true, nullptr, nullptr));
785 }
786
787 // Test that the FlexFEC field trial properly alters the output of
788 // WebRtcVideoEngine2::codecs(), for an existing |engine_| object.
789 //
790 // TODO(brandtr): Remove this test, when the FlexFEC field trial is gone.
791 TEST_F(WebRtcVideoEngine2Test,
792 Flexfec03SupportedAsInternalCodecBehindFieldTrial) {
793 auto is_flexfec = [](const VideoCodec& codec) {
794 if (codec.name == "flexfec-03")
795 return true;
796 return false;
797 };
798
799 // FlexFEC is not active without field trial.
800 engine_.Init();
801 const std::vector<VideoCodec> codecs_before = engine_.codecs();
802 EXPECT_EQ(codecs_before.end(), std::find_if(codecs_before.begin(),
803 codecs_before.end(), is_flexfec));
804
805 // FlexFEC is active with field trial.
806 webrtc::test::ScopedFieldTrials override_field_trials_(
807 "WebRTC-FlexFEC-03-Advertised/Enabled/");
808 const std::vector<VideoCodec> codecs_after = engine_.codecs();
809 EXPECT_NE(codecs_after.end(),
810 std::find_if(codecs_after.begin(), codecs_after.end(), is_flexfec));
811 }
812
813 // Test that external codecs are added to the end of the supported codec list.
814 TEST_F(WebRtcVideoEngine2Test, ReportSupportedExternalCodecs) {
815 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
816 encoder_factory.AddSupportedVideoCodecType("FakeExternalCodec");
817 engine_.SetExternalEncoderFactory(&encoder_factory);
818 engine_.Init();
819
820 std::vector<cricket::VideoCodec> codecs(engine_.codecs());
821 ASSERT_GE(codecs.size(), 2u);
822 cricket::VideoCodec internal_codec = codecs.front();
823 cricket::VideoCodec external_codec = codecs.back();
824
825 // The external codec will appear last in the vector.
826 EXPECT_EQ("VP8", internal_codec.name);
827 EXPECT_EQ("FakeExternalCodec", external_codec.name);
828 }
829
830 // Test that an external codec that was added after the engine was initialized
831 // does show up in the codec list after it was added.
832 TEST_F(WebRtcVideoEngine2Test, ReportSupportedExternalCodecsWithAddedCodec) {
833 // Set up external encoder factory with first codec, and initialize engine.
834 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
835 encoder_factory.AddSupportedVideoCodecType("FakeExternalCodec1");
836 engine_.SetExternalEncoderFactory(&encoder_factory);
837 engine_.Init();
838
839 // The first external codec will appear last in the vector.
840 std::vector<cricket::VideoCodec> codecs_before(engine_.codecs());
841 EXPECT_EQ("FakeExternalCodec1", codecs_before.back().name);
842
843 // Add second codec.
844 encoder_factory.AddSupportedVideoCodecType("FakeExternalCodec2");
845 std::vector<cricket::VideoCodec> codecs_after(engine_.codecs());
846 EXPECT_EQ(codecs_before.size() + 1, codecs_after.size());
847 EXPECT_EQ("FakeExternalCodec2", codecs_after.back().name);
848 }
849
850 TEST_F(WebRtcVideoEngine2Test, RegisterExternalDecodersIfSupported) {
851 cricket::FakeWebRtcVideoDecoderFactory decoder_factory;
852 decoder_factory.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8);
853 cricket::VideoRecvParameters parameters;
854 parameters.codecs.push_back(GetEngineCodec("VP8"));
855
856 std::unique_ptr<VideoMediaChannel> channel(
857 SetUpForExternalDecoderFactory(&decoder_factory, parameters.codecs));
858
859 EXPECT_TRUE(
860 channel->AddRecvStream(cricket::StreamParams::CreateLegacy(kSsrc)));
861 ASSERT_EQ(1u, decoder_factory.decoders().size());
862
863 // Setting codecs of the same type should not reallocate the decoder.
864 EXPECT_TRUE(channel->SetRecvParameters(parameters));
865 EXPECT_EQ(1, decoder_factory.GetNumCreatedDecoders());
866
867 // Remove stream previously added to free the external decoder instance.
868 EXPECT_TRUE(channel->RemoveRecvStream(kSsrc));
869 EXPECT_EQ(0u, decoder_factory.decoders().size());
870 }
871
872 // Verifies that we can set up decoders that are not internally supported.
873 TEST_F(WebRtcVideoEngine2Test, RegisterExternalH264DecoderIfSupported) {
874 // TODO(pbos): Do not assume that encoder/decoder support is symmetric. We
875 // can't even query the WebRtcVideoDecoderFactory for supported codecs.
876 // For now we add a FakeWebRtcVideoEncoderFactory to add H264 to supported
877 // codecs.
878 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
879 encoder_factory.AddSupportedVideoCodecType("H264");
880 engine_.SetExternalEncoderFactory(&encoder_factory);
881 cricket::FakeWebRtcVideoDecoderFactory decoder_factory;
882 decoder_factory.AddSupportedVideoCodecType(webrtc::kVideoCodecH264);
883 std::vector<cricket::VideoCodec> codecs;
884 codecs.push_back(GetEngineCodec("H264"));
885
886 std::unique_ptr<VideoMediaChannel> channel(
887 SetUpForExternalDecoderFactory(&decoder_factory, codecs));
888
889 EXPECT_TRUE(
890 channel->AddRecvStream(cricket::StreamParams::CreateLegacy(kSsrc)));
891 ASSERT_EQ(1u, decoder_factory.decoders().size());
892 }
893
894 class WebRtcVideoChannel2BaseTest
895 : public VideoMediaChannelTest<WebRtcVideoEngine2, WebRtcVideoChannel2> {
896 protected:
897 typedef VideoMediaChannelTest<WebRtcVideoEngine2, WebRtcVideoChannel2> Base;
898
899 cricket::VideoCodec GetEngineCodec(const std::string& name) {
900 for (const cricket::VideoCodec& engine_codec : engine_.codecs()) {
901 if (CodecNamesEq(name, engine_codec.name))
902 return engine_codec;
903 }
904 // This point should never be reached.
905 ADD_FAILURE() << "Unrecognized codec name: " << name;
906 return cricket::VideoCodec();
907 }
908
909 cricket::VideoCodec DefaultCodec() override { return GetEngineCodec("VP8"); }
910 };
911
912 // Verifies that id given in stream params is passed to the decoder factory.
913 TEST_F(WebRtcVideoEngine2Test, StreamParamsIdPassedToDecoderFactory) {
914 cricket::FakeWebRtcVideoDecoderFactory decoder_factory;
915 decoder_factory.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8);
916 cricket::VideoRecvParameters parameters;
917 parameters.codecs.push_back(GetEngineCodec("VP8"));
918
919 std::unique_ptr<VideoMediaChannel> channel(
920 SetUpForExternalDecoderFactory(&decoder_factory, parameters.codecs));
921
922 StreamParams sp = cricket::StreamParams::CreateLegacy(kSsrc);
923 sp.id = "FakeStreamParamsId";
924 EXPECT_TRUE(channel->AddRecvStream(sp));
925 EXPECT_EQ(1u, decoder_factory.decoders().size());
926
927 std::vector<cricket::VideoDecoderParams> params = decoder_factory.params();
928 ASSERT_EQ(1u, params.size());
929 EXPECT_EQ(sp.id, params[0].receive_stream_id);
930 }
931
932 TEST_F(WebRtcVideoEngine2Test, DISABLED_RecreatesEncoderOnContentTypeChange) {
933 cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
934 encoder_factory.AddSupportedVideoCodecType("VP8");
935 std::unique_ptr<FakeCall> fake_call(
936 new FakeCall(webrtc::Call::Config(&event_log_)));
937 std::unique_ptr<VideoMediaChannel> channel(
938 SetUpForExternalEncoderFactory(&encoder_factory));
939 ASSERT_TRUE(
940 channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
941 cricket::VideoCodec codec = GetEngineCodec("VP8");
942 cricket::VideoSendParameters parameters;
943 parameters.codecs.push_back(codec);
944 channel->OnReadyToSend(true);
945 channel->SetSend(true);
946 ASSERT_TRUE(channel->SetSendParameters(parameters));
947
948 cricket::FakeVideoCapturer capturer;
949 VideoOptions options;
950 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
951
952 EXPECT_EQ(cricket::CS_RUNNING,
953 capturer.Start(capturer.GetSupportedFormats()->front()));
954 EXPECT_TRUE(capturer.CaptureFrame());
955 ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(1));
956 EXPECT_EQ(webrtc::kRealtimeVideo,
957 encoder_factory.encoders().back()->GetCodecSettings().mode);
958
959 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
960 EXPECT_TRUE(capturer.CaptureFrame());
961 // No change in content type, keep current encoder.
962 EXPECT_EQ(1, encoder_factory.GetNumCreatedEncoders());
963
964 options.is_screencast.emplace(true);
965 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
966 EXPECT_TRUE(capturer.CaptureFrame());
967 // Change to screen content, recreate encoder. For the simulcast encoder
968 // adapter case, this will result in two calls since InitEncode triggers a
969 // a new instance.
970 ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(2));
971 EXPECT_EQ(webrtc::kScreensharing,
972 encoder_factory.encoders().back()->GetCodecSettings().mode);
973
974 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
975 EXPECT_TRUE(capturer.CaptureFrame());
976 // Still screen content, no need to update encoder.
977 EXPECT_EQ(2, encoder_factory.GetNumCreatedEncoders());
978
979 options.is_screencast.emplace(false);
980 options.video_noise_reduction.emplace(false);
981 EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
982 // Change back to regular video content, update encoder. Also change
983 // a non |is_screencast| option just to verify it doesn't affect recreation.
984 EXPECT_TRUE(capturer.CaptureFrame());
985 ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(3));
986 EXPECT_EQ(webrtc::kRealtimeVideo,
987 encoder_factory.encoders().back()->GetCodecSettings().mode);
988
989 // Remove stream previously added to free the external encoder instance.
990 EXPECT_TRUE(channel->RemoveSendStream(kSsrc));
991 EXPECT_EQ(0u, encoder_factory.encoders().size());
992 }
993
994 #define WEBRTC_BASE_TEST(test) \
995 TEST_F(WebRtcVideoChannel2BaseTest, test) { Base::test(); }
996
997 #define WEBRTC_DISABLED_BASE_TEST(test) \
998 TEST_F(WebRtcVideoChannel2BaseTest, DISABLED_##test) { Base::test(); }
999
1000 WEBRTC_BASE_TEST(SetSend);
1001 WEBRTC_BASE_TEST(SetSendWithoutCodecs);
1002 WEBRTC_BASE_TEST(SetSendSetsTransportBufferSizes);
1003
1004 WEBRTC_BASE_TEST(GetStats);
1005 WEBRTC_BASE_TEST(GetStatsMultipleRecvStreams);
1006 WEBRTC_BASE_TEST(GetStatsMultipleSendStreams);
1007
1008 WEBRTC_BASE_TEST(SetSendBandwidth);
1009
1010 WEBRTC_BASE_TEST(SetSendSsrc);
1011 WEBRTC_BASE_TEST(SetSendSsrcAfterSetCodecs);
1012
1013 WEBRTC_BASE_TEST(SetSink);
1014
1015 WEBRTC_BASE_TEST(AddRemoveSendStreams);
1016
1017 WEBRTC_BASE_TEST(SimulateConference);
1018
1019 WEBRTC_DISABLED_BASE_TEST(AddRemoveCapturer);
1020
1021 WEBRTC_BASE_TEST(RemoveCapturerWithoutAdd);
1022
1023 WEBRTC_BASE_TEST(AddRemoveCapturerMultipleSources);
1024
1025 WEBRTC_BASE_TEST(RejectEmptyStreamParams);
1026
1027 WEBRTC_BASE_TEST(MultipleSendStreams);
1028
1029 TEST_F(WebRtcVideoChannel2BaseTest, SendAndReceiveVp8Vga) {
1030 SendAndReceive(GetEngineCodec("VP8"));
1031 }
1032
1033 TEST_F(WebRtcVideoChannel2BaseTest, SendAndReceiveVp8Qvga) {
1034 SendAndReceive(GetEngineCodec("VP8"));
1035 }
1036
1037 TEST_F(WebRtcVideoChannel2BaseTest, SendAndReceiveVp8SvcQqvga) {
1038 SendAndReceive(GetEngineCodec("VP8"));
1039 }
1040
1041 TEST_F(WebRtcVideoChannel2BaseTest, TwoStreamsSendAndReceive) {
1042 // Set a high bitrate to not be downscaled by VP8 due to low initial start
1043 // bitrates. This currently happens at <250k, and two streams sharing 300k
1044 // initially will use QVGA instead of VGA.
1045 // TODO(pbos): Set up the quality scaler so that both senders reliably start
1046 // at QVGA, then verify that instead.
1047 cricket::VideoCodec codec = GetEngineCodec("VP8");
1048 codec.params[kCodecParamStartBitrate] = "1000000";
1049 Base::TwoStreamsSendAndReceive(codec);
1050 }
1051
1052 class WebRtcVideoChannel2Test : public WebRtcVideoEngine2Test {
1053 public:
1054 WebRtcVideoChannel2Test() : WebRtcVideoChannel2Test("") {}
1055 explicit WebRtcVideoChannel2Test(const char* field_trials)
1056 : WebRtcVideoEngine2Test(field_trials), last_ssrc_(0) {}
1057 void SetUp() override {
1058 fake_call_.reset(new FakeCall(webrtc::Call::Config(&event_log_)));
1059 engine_.Init();
1060 channel_.reset(engine_.CreateChannel(fake_call_.get(), GetMediaConfig(),
1061 VideoOptions()));
1062 channel_->OnReadyToSend(true);
1063 last_ssrc_ = 123;
1064 send_parameters_.codecs = engine_.codecs();
1065 recv_parameters_.codecs = engine_.codecs();
1066 ASSERT_TRUE(channel_->SetSendParameters(send_parameters_));
1067 }
1068
1069 protected:
1070 FakeVideoSendStream* AddSendStream() {
1071 return AddSendStream(StreamParams::CreateLegacy(++last_ssrc_));
1072 }
1073
1074 FakeVideoSendStream* AddSendStream(const StreamParams& sp) {
1075 size_t num_streams = fake_call_->GetVideoSendStreams().size();
1076 EXPECT_TRUE(channel_->AddSendStream(sp));
1077 std::vector<FakeVideoSendStream*> streams =
1078 fake_call_->GetVideoSendStreams();
1079 EXPECT_EQ(num_streams + 1, streams.size());
1080 return streams[streams.size() - 1];
1081 }
1082
1083 std::vector<FakeVideoSendStream*> GetFakeSendStreams() {
1084 return fake_call_->GetVideoSendStreams();
1085 }
1086
1087 FakeVideoReceiveStream* AddRecvStream() {
1088 return AddRecvStream(StreamParams::CreateLegacy(++last_ssrc_));
1089 }
1090
1091 FakeVideoReceiveStream* AddRecvStream(const StreamParams& sp) {
1092 size_t num_streams = fake_call_->GetVideoReceiveStreams().size();
1093 EXPECT_TRUE(channel_->AddRecvStream(sp));
1094 std::vector<FakeVideoReceiveStream*> streams =
1095 fake_call_->GetVideoReceiveStreams();
1096 EXPECT_EQ(num_streams + 1, streams.size());
1097 return streams[streams.size() - 1];
1098 }
1099
1100 void SetSendCodecsShouldWorkForBitrates(const char* min_bitrate_kbps,
1101 int expected_min_bitrate_bps,
1102 const char* start_bitrate_kbps,
1103 int expected_start_bitrate_bps,
1104 const char* max_bitrate_kbps,
1105 int expected_max_bitrate_bps) {
1106 auto& codecs = send_parameters_.codecs;
1107 codecs.clear();
1108 codecs.push_back(GetEngineCodec("VP8"));
1109 codecs[0].params[kCodecParamMinBitrate] = min_bitrate_kbps;
1110 codecs[0].params[kCodecParamStartBitrate] = start_bitrate_kbps;
1111 codecs[0].params[kCodecParamMaxBitrate] = max_bitrate_kbps;
1112 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
1113
1114 EXPECT_EQ(expected_min_bitrate_bps,
1115 fake_call_->GetConfig().bitrate_config.min_bitrate_bps);
1116 EXPECT_EQ(expected_start_bitrate_bps,
1117 fake_call_->GetConfig().bitrate_config.start_bitrate_bps);
1118 EXPECT_EQ(expected_max_bitrate_bps,
1119 fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
1120 }
1121
1122 void TestSetSendRtpHeaderExtensions(const std::string& ext_uri) {
1123 // Enable extension.
1124 const int id = 1;
1125 cricket::VideoSendParameters parameters = send_parameters_;
1126 parameters.extensions.push_back(RtpExtension(ext_uri, id));
1127 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1128 FakeVideoSendStream* send_stream =
1129 AddSendStream(cricket::StreamParams::CreateLegacy(123));
1130
1131 // Verify the send extension id.
1132 ASSERT_EQ(1u, send_stream->GetConfig().rtp.extensions.size());
1133 EXPECT_EQ(id, send_stream->GetConfig().rtp.extensions[0].id);
1134 EXPECT_EQ(ext_uri, send_stream->GetConfig().rtp.extensions[0].uri);
1135 // Verify call with same set of extensions returns true.
1136 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1137 // Verify that SetSendRtpHeaderExtensions doesn't implicitly add them for
1138 // receivers.
1139 EXPECT_TRUE(AddRecvStream(cricket::StreamParams::CreateLegacy(123))
1140 ->GetConfig()
1141 .rtp.extensions.empty());
1142
1143 // Verify that existing RTP header extensions can be removed.
1144 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
1145 ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
1146 send_stream = fake_call_->GetVideoSendStreams()[0];
1147 EXPECT_TRUE(send_stream->GetConfig().rtp.extensions.empty());
1148
1149 // Verify that adding receive RTP header extensions adds them for existing
1150 // streams.
1151 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1152 send_stream = fake_call_->GetVideoSendStreams()[0];
1153 ASSERT_EQ(1u, send_stream->GetConfig().rtp.extensions.size());
1154 EXPECT_EQ(id, send_stream->GetConfig().rtp.extensions[0].id);
1155 EXPECT_EQ(ext_uri, send_stream->GetConfig().rtp.extensions[0].uri);
1156 }
1157
1158 void TestSetRecvRtpHeaderExtensions(const std::string& ext_uri) {
1159 // Enable extension.
1160 const int id = 1;
1161 cricket::VideoRecvParameters parameters = recv_parameters_;
1162 parameters.extensions.push_back(RtpExtension(ext_uri, id));
1163 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
1164
1165 FakeVideoReceiveStream* recv_stream =
1166 AddRecvStream(cricket::StreamParams::CreateLegacy(123));
1167
1168 // Verify the recv extension id.
1169 ASSERT_EQ(1u, recv_stream->GetConfig().rtp.extensions.size());
1170 EXPECT_EQ(id, recv_stream->GetConfig().rtp.extensions[0].id);
1171 EXPECT_EQ(ext_uri, recv_stream->GetConfig().rtp.extensions[0].uri);
1172 // Verify call with same set of extensions returns true.
1173 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
1174
1175 // Verify that SetRecvRtpHeaderExtensions doesn't implicitly add them for
1176 // senders.
1177 EXPECT_TRUE(AddSendStream(cricket::StreamParams::CreateLegacy(123))
1178 ->GetConfig()
1179 .rtp.extensions.empty());
1180
1181 // Verify that existing RTP header extensions can be removed.
1182 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
1183 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size());
1184 recv_stream = fake_call_->GetVideoReceiveStreams()[0];
1185 EXPECT_TRUE(recv_stream->GetConfig().rtp.extensions.empty());
1186
1187 // Verify that adding receive RTP header extensions adds them for existing
1188 // streams.
1189 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
1190 recv_stream = fake_call_->GetVideoReceiveStreams()[0];
1191 ASSERT_EQ(1u, recv_stream->GetConfig().rtp.extensions.size());
1192 EXPECT_EQ(id, recv_stream->GetConfig().rtp.extensions[0].id);
1193 EXPECT_EQ(ext_uri, recv_stream->GetConfig().rtp.extensions[0].uri);
1194 }
1195
1196 void TestExtensionFilter(const std::vector<std::string>& extensions,
1197 const std::string& expected_extension) {
1198 cricket::VideoSendParameters parameters = send_parameters_;
1199 int expected_id = -1;
1200 int id = 1;
1201 for (const std::string& extension : extensions) {
1202 if (extension == expected_extension)
1203 expected_id = id;
1204 parameters.extensions.push_back(RtpExtension(extension, id++));
1205 }
1206 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1207 FakeVideoSendStream* send_stream =
1208 AddSendStream(cricket::StreamParams::CreateLegacy(123));
1209
1210 // Verify that only one of them has been set, and that it is the one with
1211 // highest priority (transport sequence number).
1212 ASSERT_EQ(1u, send_stream->GetConfig().rtp.extensions.size());
1213 EXPECT_EQ(expected_id, send_stream->GetConfig().rtp.extensions[0].id);
1214 EXPECT_EQ(expected_extension,
1215 send_stream->GetConfig().rtp.extensions[0].uri);
1216 }
1217
1218 void TestCpuAdaptation(bool enable_overuse, bool is_screenshare);
1219 void TestReceiverLocalSsrcConfiguration(bool receiver_first);
1220 void TestReceiveUnsignaledSsrcPacket(uint8_t payload_type,
1221 bool expect_created_receive_stream);
1222
1223 FakeVideoSendStream* SetDenoisingOption(
1224 uint32_t ssrc,
1225 cricket::FakeVideoCapturer* capturer,
1226 bool enabled) {
1227 cricket::VideoOptions options;
1228 options.video_noise_reduction = rtc::Optional<bool>(enabled);
1229 EXPECT_TRUE(channel_->SetVideoSend(ssrc, true, &options, capturer));
1230 // Options only take effect on the next frame.
1231 EXPECT_TRUE(capturer->CaptureFrame());
1232
1233 return fake_call_->GetVideoSendStreams().back();
1234 }
1235
1236 FakeVideoSendStream* SetUpSimulcast(bool enabled, bool with_rtx) {
1237 const int kRtxSsrcOffset = 0xDEADBEEF;
1238 last_ssrc_ += 3;
1239 std::vector<uint32_t> ssrcs;
1240 std::vector<uint32_t> rtx_ssrcs;
1241 uint32_t num_streams = enabled ? 3 : 1;
1242 for (uint32_t i = 0; i < num_streams; ++i) {
1243 uint32_t ssrc = last_ssrc_ + i;
1244 ssrcs.push_back(ssrc);
1245 if (with_rtx) {
1246 rtx_ssrcs.push_back(ssrc + kRtxSsrcOffset);
1247 }
1248 }
1249 if (with_rtx) {
1250 return AddSendStream(
1251 cricket::CreateSimWithRtxStreamParams("cname", ssrcs, rtx_ssrcs));
1252 }
1253 return AddSendStream(CreateSimStreamParams("cname", ssrcs));
1254 }
1255
1256 int GetMaxEncoderBitrate() {
1257 std::vector<FakeVideoSendStream*> streams =
1258 fake_call_->GetVideoSendStreams();
1259 EXPECT_EQ(1u, streams.size());
1260 FakeVideoSendStream* stream = streams[streams.size() - 1];
1261 EXPECT_EQ(1, stream->GetEncoderConfig().number_of_streams);
1262 return stream->GetVideoStreams()[0].max_bitrate_bps;
1263 }
1264
1265 void SetAndExpectMaxBitrate(int global_max,
1266 int stream_max,
1267 int expected_encoder_bitrate) {
1268 VideoSendParameters limited_send_params = send_parameters_;
1269 limited_send_params.max_bandwidth_bps = global_max;
1270 EXPECT_TRUE(channel_->SetSendParameters(limited_send_params));
1271 webrtc::RtpParameters parameters =
1272 channel_->GetRtpSendParameters(last_ssrc_);
1273 EXPECT_EQ(1UL, parameters.encodings.size());
1274 parameters.encodings[0].max_bitrate_bps = rtc::Optional<int>(stream_max);
1275 EXPECT_TRUE(channel_->SetRtpSendParameters(last_ssrc_, parameters));
1276 // Read back the parameteres and verify they have the correct value
1277 parameters = channel_->GetRtpSendParameters(last_ssrc_);
1278 EXPECT_EQ(1UL, parameters.encodings.size());
1279 EXPECT_EQ(rtc::Optional<int>(stream_max),
1280 parameters.encodings[0].max_bitrate_bps);
1281 // Verify that the new value propagated down to the encoder
1282 EXPECT_EQ(expected_encoder_bitrate, GetMaxEncoderBitrate());
1283 }
1284
1285 std::unique_ptr<FakeCall> fake_call_;
1286 std::unique_ptr<VideoMediaChannel> channel_;
1287 cricket::VideoSendParameters send_parameters_;
1288 cricket::VideoRecvParameters recv_parameters_;
1289 uint32_t last_ssrc_;
1290 };
1291
1292 TEST_F(WebRtcVideoChannel2Test, SetsSyncGroupFromSyncLabel) {
1293 const uint32_t kVideoSsrc = 123;
1294 const std::string kSyncLabel = "AvSyncLabel";
1295
1296 cricket::StreamParams sp = cricket::StreamParams::CreateLegacy(kVideoSsrc);
1297 sp.sync_label = kSyncLabel;
1298 EXPECT_TRUE(channel_->AddRecvStream(sp));
1299
1300 EXPECT_EQ(1, fake_call_->GetVideoReceiveStreams().size());
1301 EXPECT_EQ(kSyncLabel,
1302 fake_call_->GetVideoReceiveStreams()[0]->GetConfig().sync_group)
1303 << "SyncGroup should be set based on sync_label";
1304 }
1305
1306 TEST_F(WebRtcVideoChannel2Test, RecvStreamWithSimAndRtx) {
1307 cricket::VideoSendParameters parameters;
1308 parameters.codecs = engine_.codecs();
1309 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1310 EXPECT_TRUE(channel_->SetSend(true));
1311 parameters.conference_mode = true;
1312 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1313
1314 // Send side.
1315 const std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs1);
1316 const std::vector<uint32_t> rtx_ssrcs = MAKE_VECTOR(kRtxSsrcs1);
1317 FakeVideoSendStream* send_stream = AddSendStream(
1318 cricket::CreateSimWithRtxStreamParams("cname", ssrcs, rtx_ssrcs));
1319
1320 ASSERT_EQ(rtx_ssrcs.size(), send_stream->GetConfig().rtp.rtx.ssrcs.size());
1321 for (size_t i = 0; i < rtx_ssrcs.size(); ++i)
1322 EXPECT_EQ(rtx_ssrcs[i], send_stream->GetConfig().rtp.rtx.ssrcs[i]);
1323
1324 // Receiver side.
1325 FakeVideoReceiveStream* recv_stream = AddRecvStream(
1326 cricket::CreateSimWithRtxStreamParams("cname", ssrcs, rtx_ssrcs));
1327 EXPECT_FALSE(recv_stream->GetConfig().rtp.rtx_payload_types.empty());
1328 EXPECT_EQ(recv_stream->GetConfig().decoders.size(),
1329 recv_stream->GetConfig().rtp.rtx_payload_types.size())
1330 << "RTX should be mapped for all decoders/payload types.";
1331 EXPECT_EQ(rtx_ssrcs[0], recv_stream->GetConfig().rtp.rtx_ssrc);
1332 }
1333
1334 TEST_F(WebRtcVideoChannel2Test, RecvStreamWithRtx) {
1335 // Setup one channel with an associated RTX stream.
1336 cricket::StreamParams params =
1337 cricket::StreamParams::CreateLegacy(kSsrcs1[0]);
1338 params.AddFidSsrc(kSsrcs1[0], kRtxSsrcs1[0]);
1339 FakeVideoReceiveStream* recv_stream = AddRecvStream(params);
1340 EXPECT_EQ(kRtxSsrcs1[0], recv_stream->GetConfig().rtp.rtx_ssrc);
1341 }
1342
1343 TEST_F(WebRtcVideoChannel2Test, RecvStreamNoRtx) {
1344 // Setup one channel without an associated RTX stream.
1345 cricket::StreamParams params =
1346 cricket::StreamParams::CreateLegacy(kSsrcs1[0]);
1347 FakeVideoReceiveStream* recv_stream = AddRecvStream(params);
1348 ASSERT_EQ(0U, recv_stream->GetConfig().rtp.rtx_ssrc);
1349 }
1350
1351 TEST_F(WebRtcVideoChannel2Test, NoHeaderExtesionsByDefault) {
1352 FakeVideoSendStream* send_stream =
1353 AddSendStream(cricket::StreamParams::CreateLegacy(kSsrcs1[0]));
1354 ASSERT_TRUE(send_stream->GetConfig().rtp.extensions.empty());
1355
1356 FakeVideoReceiveStream* recv_stream =
1357 AddRecvStream(cricket::StreamParams::CreateLegacy(kSsrcs1[0]));
1358 ASSERT_TRUE(recv_stream->GetConfig().rtp.extensions.empty());
1359 }
1360
1361 // Test support for RTP timestamp offset header extension.
1362 TEST_F(WebRtcVideoChannel2Test, SendRtpTimestampOffsetHeaderExtensions) {
1363 TestSetSendRtpHeaderExtensions(RtpExtension::kTimestampOffsetUri);
1364 }
1365
1366 TEST_F(WebRtcVideoChannel2Test, RecvRtpTimestampOffsetHeaderExtensions) {
1367 TestSetRecvRtpHeaderExtensions(RtpExtension::kTimestampOffsetUri);
1368 }
1369
1370 // Test support for absolute send time header extension.
1371 TEST_F(WebRtcVideoChannel2Test, SendAbsoluteSendTimeHeaderExtensions) {
1372 TestSetSendRtpHeaderExtensions(RtpExtension::kAbsSendTimeUri);
1373 }
1374
1375 TEST_F(WebRtcVideoChannel2Test, RecvAbsoluteSendTimeHeaderExtensions) {
1376 TestSetRecvRtpHeaderExtensions(RtpExtension::kAbsSendTimeUri);
1377 }
1378
1379 TEST_F(WebRtcVideoChannel2Test, FiltersExtensionsPicksTransportSeqNum) {
1380 // Enable three redundant extensions.
1381 std::vector<std::string> extensions;
1382 extensions.push_back(RtpExtension::kAbsSendTimeUri);
1383 extensions.push_back(RtpExtension::kTimestampOffsetUri);
1384 extensions.push_back(RtpExtension::kTransportSequenceNumberUri);
1385 TestExtensionFilter(extensions, RtpExtension::kTransportSequenceNumberUri);
1386 }
1387
1388 TEST_F(WebRtcVideoChannel2Test, FiltersExtensionsPicksAbsSendTime) {
1389 // Enable two redundant extensions.
1390 std::vector<std::string> extensions;
1391 extensions.push_back(RtpExtension::kAbsSendTimeUri);
1392 extensions.push_back(RtpExtension::kTimestampOffsetUri);
1393 TestExtensionFilter(extensions, RtpExtension::kAbsSendTimeUri);
1394 }
1395
1396 // Test support for transport sequence number header extension.
1397 TEST_F(WebRtcVideoChannel2Test, SendTransportSequenceNumberHeaderExtensions) {
1398 TestSetSendRtpHeaderExtensions(RtpExtension::kTransportSequenceNumberUri);
1399 }
1400 TEST_F(WebRtcVideoChannel2Test, RecvTransportSequenceNumberHeaderExtensions) {
1401 TestSetRecvRtpHeaderExtensions(RtpExtension::kTransportSequenceNumberUri);
1402 }
1403
1404 // Test support for video rotation header extension.
1405 TEST_F(WebRtcVideoChannel2Test, SendVideoRotationHeaderExtensions) {
1406 TestSetSendRtpHeaderExtensions(RtpExtension::kVideoRotationUri);
1407 }
1408 TEST_F(WebRtcVideoChannel2Test, RecvVideoRotationHeaderExtensions) {
1409 TestSetRecvRtpHeaderExtensions(RtpExtension::kVideoRotationUri);
1410 }
1411
1412 TEST_F(WebRtcVideoChannel2Test, IdenticalSendExtensionsDoesntRecreateStream) {
1413 const int kAbsSendTimeId = 1;
1414 const int kVideoRotationId = 2;
1415 send_parameters_.extensions.push_back(
1416 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
1417 send_parameters_.extensions.push_back(
1418 RtpExtension(RtpExtension::kVideoRotationUri, kVideoRotationId));
1419
1420 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
1421 FakeVideoSendStream* send_stream =
1422 AddSendStream(cricket::StreamParams::CreateLegacy(123));
1423
1424 EXPECT_EQ(1, fake_call_->GetNumCreatedSendStreams());
1425 ASSERT_EQ(2u, send_stream->GetConfig().rtp.extensions.size());
1426
1427 // Setting the same extensions (even if in different order) shouldn't
1428 // reallocate the stream.
1429 std::reverse(send_parameters_.extensions.begin(),
1430 send_parameters_.extensions.end());
1431 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
1432
1433 EXPECT_EQ(1, fake_call_->GetNumCreatedSendStreams());
1434
1435 // Setting different extensions should recreate the stream.
1436 send_parameters_.extensions.resize(1);
1437 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
1438
1439 EXPECT_EQ(2, fake_call_->GetNumCreatedSendStreams());
1440 }
1441
1442 TEST_F(WebRtcVideoChannel2Test, IdenticalRecvExtensionsDoesntRecreateStream) {
1443 const int kTOffsetId = 1;
1444 const int kAbsSendTimeId = 2;
1445 const int kVideoRotationId = 3;
1446 recv_parameters_.extensions.push_back(
1447 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
1448 recv_parameters_.extensions.push_back(
1449 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOffsetId));
1450 recv_parameters_.extensions.push_back(
1451 RtpExtension(RtpExtension::kVideoRotationUri, kVideoRotationId));
1452
1453 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
1454 FakeVideoReceiveStream* recv_stream =
1455 AddRecvStream(cricket::StreamParams::CreateLegacy(123));
1456
1457 EXPECT_EQ(1, fake_call_->GetNumCreatedReceiveStreams());
1458 ASSERT_EQ(3u, recv_stream->GetConfig().rtp.extensions.size());
1459
1460 // Setting the same extensions (even if in different order) shouldn't
1461 // reallocate the stream.
1462 std::reverse(recv_parameters_.extensions.begin(),
1463 recv_parameters_.extensions.end());
1464 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
1465
1466 EXPECT_EQ(1, fake_call_->GetNumCreatedReceiveStreams());
1467
1468 // Setting different extensions should recreate the stream.
1469 recv_parameters_.extensions.resize(1);
1470 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
1471
1472 EXPECT_EQ(2, fake_call_->GetNumCreatedReceiveStreams());
1473 }
1474
1475 TEST_F(WebRtcVideoChannel2Test,
1476 SetSendRtpHeaderExtensionsExcludeUnsupportedExtensions) {
1477 const int kUnsupportedId = 1;
1478 const int kTOffsetId = 2;
1479
1480 send_parameters_.extensions.push_back(
1481 RtpExtension(kUnsupportedExtensionName, kUnsupportedId));
1482 send_parameters_.extensions.push_back(
1483 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOffsetId));
1484 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
1485 FakeVideoSendStream* send_stream =
1486 AddSendStream(cricket::StreamParams::CreateLegacy(123));
1487
1488 // Only timestamp offset extension is set to send stream,
1489 // unsupported rtp extension is ignored.
1490 ASSERT_EQ(1u, send_stream->GetConfig().rtp.extensions.size());
1491 EXPECT_STREQ(RtpExtension::kTimestampOffsetUri,
1492 send_stream->GetConfig().rtp.extensions[0].uri.c_str());
1493 }
1494
1495 TEST_F(WebRtcVideoChannel2Test,
1496 SetRecvRtpHeaderExtensionsExcludeUnsupportedExtensions) {
1497 const int kUnsupportedId = 1;
1498 const int kTOffsetId = 2;
1499
1500 recv_parameters_.extensions.push_back(
1501 RtpExtension(kUnsupportedExtensionName, kUnsupportedId));
1502 recv_parameters_.extensions.push_back(
1503 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOffsetId));
1504 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
1505 FakeVideoReceiveStream* recv_stream =
1506 AddRecvStream(cricket::StreamParams::CreateLegacy(123));
1507
1508 // Only timestamp offset extension is set to receive stream,
1509 // unsupported rtp extension is ignored.
1510 ASSERT_EQ(1u, recv_stream->GetConfig().rtp.extensions.size());
1511 EXPECT_STREQ(RtpExtension::kTimestampOffsetUri,
1512 recv_stream->GetConfig().rtp.extensions[0].uri.c_str());
1513 }
1514
1515 TEST_F(WebRtcVideoChannel2Test, SetSendRtpHeaderExtensionsRejectsIncorrectIds) {
1516 const int kIncorrectIds[] = {-2, -1, 0, 15, 16};
1517 for (size_t i = 0; i < arraysize(kIncorrectIds); ++i) {
1518 send_parameters_.extensions.push_back(
1519 RtpExtension(RtpExtension::kTimestampOffsetUri, kIncorrectIds[i]));
1520 EXPECT_FALSE(channel_->SetSendParameters(send_parameters_))
1521 << "Bad extension id '" << kIncorrectIds[i] << "' accepted.";
1522 }
1523 }
1524
1525 TEST_F(WebRtcVideoChannel2Test, SetRecvRtpHeaderExtensionsRejectsIncorrectIds) {
1526 const int kIncorrectIds[] = {-2, -1, 0, 15, 16};
1527 for (size_t i = 0; i < arraysize(kIncorrectIds); ++i) {
1528 recv_parameters_.extensions.push_back(
1529 RtpExtension(RtpExtension::kTimestampOffsetUri, kIncorrectIds[i]));
1530 EXPECT_FALSE(channel_->SetRecvParameters(recv_parameters_))
1531 << "Bad extension id '" << kIncorrectIds[i] << "' accepted.";
1532 }
1533 }
1534
1535 TEST_F(WebRtcVideoChannel2Test, SetSendRtpHeaderExtensionsRejectsDuplicateIds) {
1536 const int id = 1;
1537 send_parameters_.extensions.push_back(
1538 RtpExtension(RtpExtension::kTimestampOffsetUri, id));
1539 send_parameters_.extensions.push_back(
1540 RtpExtension(RtpExtension::kAbsSendTimeUri, id));
1541 EXPECT_FALSE(channel_->SetSendParameters(send_parameters_));
1542
1543 // Duplicate entries are also not supported.
1544 send_parameters_.extensions.clear();
1545 send_parameters_.extensions.push_back(
1546 RtpExtension(RtpExtension::kTimestampOffsetUri, id));
1547 send_parameters_.extensions.push_back(send_parameters_.extensions.back());
1548 EXPECT_FALSE(channel_->SetSendParameters(send_parameters_));
1549 }
1550
1551 TEST_F(WebRtcVideoChannel2Test, SetRecvRtpHeaderExtensionsRejectsDuplicateIds) {
1552 const int id = 1;
1553 recv_parameters_.extensions.push_back(
1554 RtpExtension(RtpExtension::kTimestampOffsetUri, id));
1555 recv_parameters_.extensions.push_back(
1556 RtpExtension(RtpExtension::kAbsSendTimeUri, id));
1557 EXPECT_FALSE(channel_->SetRecvParameters(recv_parameters_));
1558
1559 // Duplicate entries are also not supported.
1560 recv_parameters_.extensions.clear();
1561 recv_parameters_.extensions.push_back(
1562 RtpExtension(RtpExtension::kTimestampOffsetUri, id));
1563 recv_parameters_.extensions.push_back(recv_parameters_.extensions.back());
1564 EXPECT_FALSE(channel_->SetRecvParameters(recv_parameters_));
1565 }
1566
1567 TEST_F(WebRtcVideoChannel2Test, AddRecvStreamOnlyUsesOneReceiveStream) {
1568 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
1569 EXPECT_EQ(1u, fake_call_->GetVideoReceiveStreams().size());
1570 }
1571
1572 TEST_F(WebRtcVideoChannel2Test, RtcpIsCompoundByDefault) {
1573 FakeVideoReceiveStream* stream = AddRecvStream();
1574 EXPECT_EQ(webrtc::RtcpMode::kCompound, stream->GetConfig().rtp.rtcp_mode);
1575 }
1576
1577 TEST_F(WebRtcVideoChannel2Test, RembIsEnabledByDefault) {
1578 FakeVideoReceiveStream* stream = AddRecvStream();
1579 EXPECT_TRUE(stream->GetConfig().rtp.remb);
1580 }
1581
1582 TEST_F(WebRtcVideoChannel2Test, TransportCcIsEnabledByDefault) {
1583 FakeVideoReceiveStream* stream = AddRecvStream();
1584 EXPECT_TRUE(stream->GetConfig().rtp.transport_cc);
1585 }
1586
1587 TEST_F(WebRtcVideoChannel2Test, RembCanBeEnabledAndDisabled) {
1588 FakeVideoReceiveStream* stream = AddRecvStream();
1589 EXPECT_TRUE(stream->GetConfig().rtp.remb);
1590
1591 // Verify that REMB is turned off when send(!) codecs without REMB are set.
1592 cricket::VideoSendParameters parameters;
1593 parameters.codecs.push_back(RemoveFeedbackParams(GetEngineCodec("VP8")));
1594 EXPECT_TRUE(parameters.codecs[0].feedback_params.params().empty());
1595 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1596 stream = fake_call_->GetVideoReceiveStreams()[0];
1597 EXPECT_FALSE(stream->GetConfig().rtp.remb);
1598
1599 // Verify that REMB is turned on when setting default codecs since the
1600 // default codecs have REMB enabled.
1601 parameters.codecs = engine_.codecs();
1602 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1603 stream = fake_call_->GetVideoReceiveStreams()[0];
1604 EXPECT_TRUE(stream->GetConfig().rtp.remb);
1605 }
1606
1607 TEST_F(WebRtcVideoChannel2Test, TransportCcCanBeEnabledAndDisabled) {
1608 FakeVideoReceiveStream* stream = AddRecvStream();
1609 EXPECT_TRUE(stream->GetConfig().rtp.transport_cc);
1610
1611 // Verify that transport cc feedback is turned off when send(!) codecs without
1612 // transport cc feedback are set.
1613 cricket::VideoSendParameters parameters;
1614 parameters.codecs.push_back(RemoveFeedbackParams(GetEngineCodec("VP8")));
1615 EXPECT_TRUE(parameters.codecs[0].feedback_params.params().empty());
1616 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1617 stream = fake_call_->GetVideoReceiveStreams()[0];
1618 EXPECT_FALSE(stream->GetConfig().rtp.transport_cc);
1619
1620 // Verify that transport cc feedback is turned on when setting default codecs
1621 // since the default codecs have transport cc feedback enabled.
1622 parameters.codecs = engine_.codecs();
1623 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1624 stream = fake_call_->GetVideoReceiveStreams()[0];
1625 EXPECT_TRUE(stream->GetConfig().rtp.transport_cc);
1626 }
1627
1628 TEST_F(WebRtcVideoChannel2Test, NackIsEnabledByDefault) {
1629 VerifyCodecHasDefaultFeedbackParams(default_codec_);
1630
1631 cricket::VideoSendParameters parameters;
1632 parameters.codecs = engine_.codecs();
1633 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1634 EXPECT_TRUE(channel_->SetSend(true));
1635
1636 // Send side.
1637 FakeVideoSendStream* send_stream =
1638 AddSendStream(cricket::StreamParams::CreateLegacy(1));
1639 EXPECT_GT(send_stream->GetConfig().rtp.nack.rtp_history_ms, 0);
1640
1641 // Receiver side.
1642 FakeVideoReceiveStream* recv_stream =
1643 AddRecvStream(cricket::StreamParams::CreateLegacy(1));
1644 EXPECT_GT(recv_stream->GetConfig().rtp.nack.rtp_history_ms, 0);
1645
1646 // Nack history size should match between sender and receiver.
1647 EXPECT_EQ(send_stream->GetConfig().rtp.nack.rtp_history_ms,
1648 recv_stream->GetConfig().rtp.nack.rtp_history_ms);
1649 }
1650
1651 TEST_F(WebRtcVideoChannel2Test, NackCanBeEnabledAndDisabled) {
1652 FakeVideoSendStream* send_stream = AddSendStream();
1653 FakeVideoReceiveStream* recv_stream = AddRecvStream();
1654
1655 EXPECT_GT(recv_stream->GetConfig().rtp.nack.rtp_history_ms, 0);
1656 EXPECT_GT(send_stream->GetConfig().rtp.nack.rtp_history_ms, 0);
1657
1658 // Verify that NACK is turned off when send(!) codecs without NACK are set.
1659 cricket::VideoSendParameters parameters;
1660 parameters.codecs.push_back(RemoveFeedbackParams(GetEngineCodec("VP8")));
1661 EXPECT_TRUE(parameters.codecs[0].feedback_params.params().empty());
1662 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1663 recv_stream = fake_call_->GetVideoReceiveStreams()[0];
1664 EXPECT_EQ(0, recv_stream->GetConfig().rtp.nack.rtp_history_ms);
1665 send_stream = fake_call_->GetVideoSendStreams()[0];
1666 EXPECT_EQ(0, send_stream->GetConfig().rtp.nack.rtp_history_ms);
1667
1668 // Verify that NACK is turned on when setting default codecs since the
1669 // default codecs have NACK enabled.
1670 parameters.codecs = engine_.codecs();
1671 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1672 recv_stream = fake_call_->GetVideoReceiveStreams()[0];
1673 EXPECT_GT(recv_stream->GetConfig().rtp.nack.rtp_history_ms, 0);
1674 send_stream = fake_call_->GetVideoSendStreams()[0];
1675 EXPECT_GT(send_stream->GetConfig().rtp.nack.rtp_history_ms, 0);
1676 }
1677
1678 // This test verifies that new frame sizes reconfigures encoders even though not
1679 // (yet) sending. The purpose of this is to permit encoding as quickly as
1680 // possible once we start sending. Likely the frames being input are from the
1681 // same source that will be sent later, which just means that we're ready
1682 // earlier.
1683 TEST_F(WebRtcVideoChannel2Test, ReconfiguresEncodersWhenNotSending) {
1684 cricket::VideoSendParameters parameters;
1685 parameters.codecs.push_back(GetEngineCodec("VP8"));
1686 ASSERT_TRUE(channel_->SetSendParameters(parameters));
1687 channel_->SetSend(false);
1688
1689 FakeVideoSendStream* stream = AddSendStream();
1690
1691 // No frames entered.
1692 std::vector<webrtc::VideoStream> streams = stream->GetVideoStreams();
1693 EXPECT_EQ(0u, streams[0].width);
1694 EXPECT_EQ(0u, streams[0].height);
1695
1696 cricket::FakeVideoCapturer capturer;
1697 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
1698 VideoFormat capture_format = capturer.GetSupportedFormats()->front();
1699 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(capture_format));
1700 EXPECT_TRUE(capturer.CaptureFrame());
1701
1702 // Frame entered, should be reconfigured to new dimensions.
1703 streams = stream->GetVideoStreams();
1704 EXPECT_EQ(capture_format.width, streams[0].width);
1705 EXPECT_EQ(capture_format.height, streams[0].height);
1706
1707 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
1708 }
1709
1710 TEST_F(WebRtcVideoChannel2Test, UsesCorrectSettingsForScreencast) {
1711 static const int kScreenshareMinBitrateKbps = 800;
1712 cricket::VideoCodec codec = GetEngineCodec("VP8");
1713 cricket::VideoSendParameters parameters;
1714 parameters.codecs.push_back(codec);
1715 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1716 AddSendStream();
1717
1718 cricket::FakeVideoCapturer capturer;
1719 VideoOptions min_bitrate_options;
1720 min_bitrate_options.screencast_min_bitrate_kbps =
1721 rtc::Optional<int>(kScreenshareMinBitrateKbps);
1722 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &min_bitrate_options,
1723 &capturer));
1724 cricket::VideoFormat capture_format_hd =
1725 capturer.GetSupportedFormats()->front();
1726 EXPECT_EQ(1280, capture_format_hd.width);
1727 EXPECT_EQ(720, capture_format_hd.height);
1728 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(capture_format_hd));
1729
1730 EXPECT_TRUE(channel_->SetSend(true));
1731
1732 EXPECT_TRUE(capturer.CaptureFrame());
1733 ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
1734 FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
1735
1736 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
1737
1738 // Verify non-screencast settings.
1739 webrtc::VideoEncoderConfig encoder_config =
1740 send_stream->GetEncoderConfig().Copy();
1741 EXPECT_EQ(webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo,
1742 encoder_config.content_type);
1743 std::vector<webrtc::VideoStream> streams = send_stream->GetVideoStreams();
1744 EXPECT_EQ(capture_format_hd.width, streams.front().width);
1745 EXPECT_EQ(capture_format_hd.height, streams.front().height);
1746 EXPECT_EQ(0, encoder_config.min_transmit_bitrate_bps)
1747 << "Non-screenshare shouldn't use min-transmit bitrate.";
1748
1749 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
1750 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
1751 VideoOptions screencast_options;
1752 screencast_options.is_screencast = rtc::Optional<bool>(true);
1753 EXPECT_TRUE(
1754 channel_->SetVideoSend(last_ssrc_, true, &screencast_options, &capturer));
1755 EXPECT_TRUE(capturer.CaptureFrame());
1756 // Send stream recreated after option change.
1757 ASSERT_EQ(2, fake_call_->GetNumCreatedSendStreams());
1758 send_stream = fake_call_->GetVideoSendStreams().front();
1759 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
1760
1761 // Verify screencast settings.
1762 encoder_config = send_stream->GetEncoderConfig().Copy();
1763 EXPECT_EQ(webrtc::VideoEncoderConfig::ContentType::kScreen,
1764 encoder_config.content_type);
1765 EXPECT_EQ(kScreenshareMinBitrateKbps * 1000,
1766 encoder_config.min_transmit_bitrate_bps);
1767
1768 streams = send_stream->GetVideoStreams();
1769 EXPECT_EQ(capture_format_hd.width, streams.front().width);
1770 EXPECT_EQ(capture_format_hd.height, streams.front().height);
1771 EXPECT_TRUE(streams[0].temporal_layer_thresholds_bps.empty());
1772 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
1773 }
1774
1775 TEST_F(WebRtcVideoChannel2Test,
1776 ConferenceModeScreencastConfiguresTemporalLayer) {
1777 static const int kConferenceScreencastTemporalBitrateBps =
1778 ScreenshareLayerConfig::GetDefault().tl0_bitrate_kbps * 1000;
1779 send_parameters_.conference_mode = true;
1780 channel_->SetSendParameters(send_parameters_);
1781
1782 AddSendStream();
1783 VideoOptions options;
1784 options.is_screencast = rtc::Optional<bool>(true);
1785 cricket::FakeVideoCapturer capturer;
1786 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
1787 cricket::VideoFormat capture_format_hd =
1788 capturer.GetSupportedFormats()->front();
1789 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(capture_format_hd));
1790
1791 EXPECT_TRUE(channel_->SetSend(true));
1792
1793 EXPECT_TRUE(capturer.CaptureFrame());
1794 ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
1795 FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
1796
1797 webrtc::VideoEncoderConfig encoder_config =
1798 send_stream->GetEncoderConfig().Copy();
1799
1800 // Verify screencast settings.
1801 encoder_config = send_stream->GetEncoderConfig().Copy();
1802 EXPECT_EQ(webrtc::VideoEncoderConfig::ContentType::kScreen,
1803 encoder_config.content_type);
1804
1805 std::vector<webrtc::VideoStream> streams = send_stream->GetVideoStreams();
1806 ASSERT_EQ(1u, streams.size());
1807 ASSERT_EQ(1u, streams[0].temporal_layer_thresholds_bps.size());
1808 EXPECT_EQ(kConferenceScreencastTemporalBitrateBps,
1809 streams[0].temporal_layer_thresholds_bps[0]);
1810
1811 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
1812 }
1813
1814 TEST_F(WebRtcVideoChannel2Test, SuspendBelowMinBitrateDisabledByDefault) {
1815 FakeVideoSendStream* stream = AddSendStream();
1816 EXPECT_FALSE(stream->GetConfig().suspend_below_min_bitrate);
1817 }
1818
1819 TEST_F(WebRtcVideoChannel2Test, SetMediaConfigSuspendBelowMinBitrate) {
1820 MediaConfig media_config = GetMediaConfig();
1821 media_config.video.suspend_below_min_bitrate = true;
1822
1823 channel_.reset(
1824 engine_.CreateChannel(fake_call_.get(), media_config, VideoOptions()));
1825 channel_->OnReadyToSend(true);
1826
1827 channel_->SetSendParameters(send_parameters_);
1828
1829 FakeVideoSendStream* stream = AddSendStream();
1830 EXPECT_TRUE(stream->GetConfig().suspend_below_min_bitrate);
1831
1832 media_config.video.suspend_below_min_bitrate = false;
1833 channel_.reset(
1834 engine_.CreateChannel(fake_call_.get(), media_config, VideoOptions()));
1835 channel_->OnReadyToSend(true);
1836
1837 channel_->SetSendParameters(send_parameters_);
1838
1839 stream = AddSendStream();
1840 EXPECT_FALSE(stream->GetConfig().suspend_below_min_bitrate);
1841 }
1842
1843 TEST_F(WebRtcVideoChannel2Test, Vp8DenoisingEnabledByDefault) {
1844 FakeVideoSendStream* stream = AddSendStream();
1845 webrtc::VideoCodecVP8 vp8_settings;
1846 ASSERT_TRUE(stream->GetVp8Settings(&vp8_settings)) << "No VP8 config set.";
1847 EXPECT_TRUE(vp8_settings.denoisingOn);
1848 }
1849
1850 TEST_F(WebRtcVideoChannel2Test, VerifyVp8SpecificSettings) {
1851 cricket::VideoSendParameters parameters;
1852 parameters.codecs.push_back(GetEngineCodec("VP8"));
1853 ASSERT_TRUE(channel_->SetSendParameters(parameters));
1854
1855 // Single-stream settings should apply with RTX as well (verifies that we
1856 // check number of regular SSRCs and not StreamParams::ssrcs which contains
1857 // both RTX and regular SSRCs).
1858 FakeVideoSendStream* stream = SetUpSimulcast(false, true);
1859
1860 cricket::FakeVideoCapturer capturer;
1861 EXPECT_EQ(cricket::CS_RUNNING,
1862 capturer.Start(capturer.GetSupportedFormats()->front()));
1863 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
1864 channel_->SetSend(true);
1865
1866 EXPECT_TRUE(capturer.CaptureFrame());
1867
1868 webrtc::VideoCodecVP8 vp8_settings;
1869 ASSERT_TRUE(stream->GetVp8Settings(&vp8_settings)) << "No VP8 config set.";
1870 EXPECT_TRUE(vp8_settings.denoisingOn)
1871 << "VP8 denoising should be on by default.";
1872
1873 stream = SetDenoisingOption(last_ssrc_, &capturer, false);
1874
1875 ASSERT_TRUE(stream->GetVp8Settings(&vp8_settings)) << "No VP8 config set.";
1876 EXPECT_FALSE(vp8_settings.denoisingOn);
1877 EXPECT_TRUE(vp8_settings.automaticResizeOn);
1878 EXPECT_TRUE(vp8_settings.frameDroppingOn);
1879
1880 stream = SetDenoisingOption(last_ssrc_, &capturer, true);
1881
1882 ASSERT_TRUE(stream->GetVp8Settings(&vp8_settings)) << "No VP8 config set.";
1883 EXPECT_TRUE(vp8_settings.denoisingOn);
1884 EXPECT_TRUE(vp8_settings.automaticResizeOn);
1885 EXPECT_TRUE(vp8_settings.frameDroppingOn);
1886
1887 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
1888 stream = SetUpSimulcast(true, false);
1889 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
1890 channel_->SetSend(true);
1891 EXPECT_TRUE(capturer.CaptureFrame());
1892
1893 EXPECT_EQ(3, stream->GetVideoStreams().size());
1894 ASSERT_TRUE(stream->GetVp8Settings(&vp8_settings)) << "No VP8 config set.";
1895 // Autmatic resize off when using simulcast.
1896 EXPECT_FALSE(vp8_settings.automaticResizeOn);
1897 EXPECT_TRUE(vp8_settings.frameDroppingOn);
1898
1899 // In screen-share mode, denoising is forced off and simulcast disabled.
1900 VideoOptions options;
1901 options.is_screencast = rtc::Optional<bool>(true);
1902 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
1903
1904 stream = SetDenoisingOption(last_ssrc_, &capturer, false);
1905
1906 EXPECT_EQ(1, stream->GetVideoStreams().size());
1907 ASSERT_TRUE(stream->GetVp8Settings(&vp8_settings)) << "No VP8 config set.";
1908 EXPECT_FALSE(vp8_settings.denoisingOn);
1909 // Resizing and frame dropping always off for screen sharing.
1910 EXPECT_FALSE(vp8_settings.automaticResizeOn);
1911 EXPECT_FALSE(vp8_settings.frameDroppingOn);
1912
1913 stream = SetDenoisingOption(last_ssrc_, &capturer, true);
1914
1915 ASSERT_TRUE(stream->GetVp8Settings(&vp8_settings)) << "No VP8 config set.";
1916 EXPECT_FALSE(vp8_settings.denoisingOn);
1917 EXPECT_FALSE(vp8_settings.automaticResizeOn);
1918 EXPECT_FALSE(vp8_settings.frameDroppingOn);
1919
1920 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
1921 }
1922
1923 // Test that setting the same options doesn't result in the encoder being
1924 // reconfigured.
1925 TEST_F(WebRtcVideoChannel2Test, SetIdenticalOptionsDoesntReconfigureEncoder) {
1926 VideoOptions options;
1927 cricket::FakeVideoCapturer capturer;
1928
1929 AddSendStream();
1930 EXPECT_EQ(cricket::CS_RUNNING,
1931 capturer.Start(capturer.GetSupportedFormats()->front()));
1932 cricket::VideoSendParameters parameters;
1933 parameters.codecs.push_back(GetEngineCodec("VP8"));
1934 ASSERT_TRUE(channel_->SetSendParameters(parameters));
1935 FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
1936
1937 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
1938 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
1939 EXPECT_TRUE(capturer.CaptureFrame());
1940 // Expect 1 reconfigurations at this point from the initial configuration.
1941 EXPECT_EQ(1, send_stream->num_encoder_reconfigurations());
1942
1943 // Set the options one more time and expect no additional reconfigurations.
1944 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
1945 EXPECT_EQ(1, send_stream->num_encoder_reconfigurations());
1946
1947 // Change |options| and expect 2 reconfigurations.
1948 options.video_noise_reduction = rtc::Optional<bool>(true);
1949 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
1950 EXPECT_EQ(2, send_stream->num_encoder_reconfigurations());
1951
1952 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
1953 }
1954
1955 class Vp9SettingsTest : public WebRtcVideoChannel2Test {
1956 public:
1957 Vp9SettingsTest() : Vp9SettingsTest("") {}
1958 explicit Vp9SettingsTest(const char* field_trials)
1959 : WebRtcVideoChannel2Test(field_trials) {
1960 encoder_factory_.AddSupportedVideoCodecType("VP9");
1961 }
1962 virtual ~Vp9SettingsTest() {}
1963
1964 protected:
1965 void SetUp() override {
1966 engine_.SetExternalEncoderFactory(&encoder_factory_);
1967
1968 WebRtcVideoChannel2Test::SetUp();
1969 }
1970
1971 void TearDown() override {
1972 // Remove references to encoder_factory_ since this will be destroyed
1973 // before channel_ and engine_.
1974 ASSERT_TRUE(channel_->SetSendParameters(send_parameters_));
1975 }
1976
1977 cricket::FakeWebRtcVideoEncoderFactory encoder_factory_;
1978 };
1979
1980 TEST_F(Vp9SettingsTest, VerifyVp9SpecificSettings) {
1981 cricket::VideoSendParameters parameters;
1982 parameters.codecs.push_back(GetEngineCodec("VP9"));
1983 ASSERT_TRUE(channel_->SetSendParameters(parameters));
1984
1985 FakeVideoSendStream* stream = SetUpSimulcast(false, false);
1986
1987 cricket::FakeVideoCapturer capturer;
1988 EXPECT_EQ(cricket::CS_RUNNING,
1989 capturer.Start(capturer.GetSupportedFormats()->front()));
1990 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
1991 channel_->SetSend(true);
1992
1993 EXPECT_TRUE(capturer.CaptureFrame());
1994
1995 webrtc::VideoCodecVP9 vp9_settings;
1996 ASSERT_TRUE(stream->GetVp9Settings(&vp9_settings)) << "No VP9 config set.";
1997 EXPECT_TRUE(vp9_settings.denoisingOn)
1998 << "VP9 denoising should be on by default.";
1999
2000 stream = SetDenoisingOption(last_ssrc_, &capturer, false);
2001
2002 ASSERT_TRUE(stream->GetVp9Settings(&vp9_settings)) << "No VP9 config set.";
2003 EXPECT_FALSE(vp9_settings.denoisingOn);
2004 // Frame dropping always on for real time video.
2005 EXPECT_TRUE(vp9_settings.frameDroppingOn);
2006
2007 stream = SetDenoisingOption(last_ssrc_, &capturer, true);
2008
2009 ASSERT_TRUE(stream->GetVp9Settings(&vp9_settings)) << "No VP9 config set.";
2010 EXPECT_TRUE(vp9_settings.denoisingOn);
2011 EXPECT_TRUE(vp9_settings.frameDroppingOn);
2012
2013 // In screen-share mode, denoising is forced off.
2014 VideoOptions options;
2015 options.is_screencast = rtc::Optional<bool>(true);
2016 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
2017
2018 stream = SetDenoisingOption(last_ssrc_, &capturer, false);
2019
2020 ASSERT_TRUE(stream->GetVp9Settings(&vp9_settings)) << "No VP9 config set.";
2021 EXPECT_FALSE(vp9_settings.denoisingOn);
2022 // Frame dropping always off for screen sharing.
2023 EXPECT_FALSE(vp9_settings.frameDroppingOn);
2024
2025 stream = SetDenoisingOption(last_ssrc_, &capturer, false);
2026
2027 ASSERT_TRUE(stream->GetVp9Settings(&vp9_settings)) << "No VP9 config set.";
2028 EXPECT_FALSE(vp9_settings.denoisingOn);
2029 EXPECT_FALSE(vp9_settings.frameDroppingOn);
2030
2031 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2032 }
2033
2034 class Vp9SettingsTestWithFieldTrial : public Vp9SettingsTest {
2035 public:
2036 explicit Vp9SettingsTestWithFieldTrial(const char* field_trials)
2037 : Vp9SettingsTest(field_trials) {}
2038
2039 protected:
2040 void VerifySettings(int num_spatial_layers, int num_temporal_layers) {
2041 cricket::VideoSendParameters parameters;
2042 parameters.codecs.push_back(GetEngineCodec("VP9"));
2043 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2044
2045 FakeVideoSendStream* stream = SetUpSimulcast(false, false);
2046
2047 cricket::FakeVideoCapturer capturer;
2048 EXPECT_EQ(cricket::CS_RUNNING,
2049 capturer.Start(capturer.GetSupportedFormats()->front()));
2050 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
2051 channel_->SetSend(true);
2052
2053 EXPECT_TRUE(capturer.CaptureFrame());
2054
2055 webrtc::VideoCodecVP9 vp9_settings;
2056 ASSERT_TRUE(stream->GetVp9Settings(&vp9_settings)) << "No VP9 config set.";
2057 EXPECT_EQ(num_spatial_layers, vp9_settings.numberOfSpatialLayers);
2058 EXPECT_EQ(num_temporal_layers, vp9_settings.numberOfTemporalLayers);
2059
2060 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2061 }
2062 };
2063
2064 class Vp9SettingsTestWithNoFlag : public Vp9SettingsTestWithFieldTrial {
2065 public:
2066 Vp9SettingsTestWithNoFlag() : Vp9SettingsTestWithFieldTrial("") {}
2067 };
2068
2069 TEST_F(Vp9SettingsTestWithNoFlag, VerifySettings) {
2070 const int kNumSpatialLayers = 1;
2071 const int kNumTemporalLayers = 1;
2072 VerifySettings(kNumSpatialLayers, kNumTemporalLayers);
2073 }
2074
2075 class Vp9SettingsTestWithInvalidFlag : public Vp9SettingsTestWithFieldTrial {
2076 public:
2077 Vp9SettingsTestWithInvalidFlag()
2078 : Vp9SettingsTestWithFieldTrial("WebRTC-SupportVP9SVC/Default/") {}
2079 };
2080
2081 TEST_F(Vp9SettingsTestWithInvalidFlag, VerifySettings) {
2082 const int kNumSpatialLayers = 1;
2083 const int kNumTemporalLayers = 1;
2084 VerifySettings(kNumSpatialLayers, kNumTemporalLayers);
2085 }
2086
2087 class Vp9SettingsTestWith2SL3TLFlag : public Vp9SettingsTestWithFieldTrial {
2088 public:
2089 Vp9SettingsTestWith2SL3TLFlag()
2090 : Vp9SettingsTestWithFieldTrial(
2091 "WebRTC-SupportVP9SVC/EnabledByFlag_2SL3TL/") {}
2092 };
2093
2094 TEST_F(Vp9SettingsTestWith2SL3TLFlag, VerifySettings) {
2095 const int kNumSpatialLayers = 2;
2096 const int kNumTemporalLayers = 3;
2097 VerifySettings(kNumSpatialLayers, kNumTemporalLayers);
2098 }
2099
2100 TEST_F(WebRtcVideoChannel2Test, AdaptsOnOveruse) {
2101 TestCpuAdaptation(true, false);
2102 }
2103
2104 TEST_F(WebRtcVideoChannel2Test, DoesNotAdaptOnOveruseWhenDisabled) {
2105 TestCpuAdaptation(false, false);
2106 }
2107
2108 TEST_F(WebRtcVideoChannel2Test, DoesNotAdaptOnOveruseWhenScreensharing) {
2109 TestCpuAdaptation(true, true);
2110 }
2111
2112 TEST_F(WebRtcVideoChannel2Test, AdaptsOnOveruseAndChangeResolution) {
2113 cricket::VideoCodec codec = GetEngineCodec("VP8");
2114 cricket::VideoSendParameters parameters;
2115 parameters.codecs.push_back(codec);
2116
2117 MediaConfig media_config = GetMediaConfig();
2118 channel_.reset(
2119 engine_.CreateChannel(fake_call_.get(), media_config, VideoOptions()));
2120 channel_->OnReadyToSend(true);
2121 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2122
2123 AddSendStream();
2124
2125 cricket::FakeVideoCapturer capturer;
2126 ASSERT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
2127 ASSERT_EQ(cricket::CS_RUNNING,
2128 capturer.Start(capturer.GetSupportedFormats()->front()));
2129 ASSERT_TRUE(channel_->SetSend(true));
2130
2131 ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
2132 FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
2133
2134 EXPECT_TRUE(capturer.CaptureCustomFrame(1280, 720, cricket::FOURCC_I420));
2135 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
2136 EXPECT_EQ(1280, send_stream->GetLastWidth());
2137 EXPECT_EQ(720, send_stream->GetLastHeight());
2138
2139 // Trigger overuse.
2140 rtc::VideoSinkWants wants;
2141 wants.max_pixel_count =
2142 send_stream->GetLastWidth() * send_stream->GetLastHeight() - 1;
2143 send_stream->InjectVideoSinkWants(wants);
2144 EXPECT_TRUE(capturer.CaptureCustomFrame(1280, 720, cricket::FOURCC_I420));
2145 EXPECT_EQ(2, send_stream->GetNumberOfSwappedFrames());
2146 EXPECT_EQ(1280 * 3 / 4, send_stream->GetLastWidth());
2147 EXPECT_EQ(720 * 3 / 4, send_stream->GetLastHeight());
2148
2149 // Trigger overuse again.
2150 wants.max_pixel_count =
2151 send_stream->GetLastWidth() * send_stream->GetLastHeight() - 1;
2152 send_stream->InjectVideoSinkWants(wants);
2153 EXPECT_TRUE(capturer.CaptureCustomFrame(1280, 720, cricket::FOURCC_I420));
2154 EXPECT_EQ(3, send_stream->GetNumberOfSwappedFrames());
2155 EXPECT_EQ(1280 * 2 / 4, send_stream->GetLastWidth());
2156 EXPECT_EQ(720 * 2 / 4, send_stream->GetLastHeight());
2157
2158 // Change input resolution.
2159 EXPECT_TRUE(capturer.CaptureCustomFrame(1284, 724, cricket::FOURCC_I420));
2160 EXPECT_EQ(4, send_stream->GetNumberOfSwappedFrames());
2161 EXPECT_EQ(1284 / 2, send_stream->GetLastWidth());
2162 EXPECT_EQ(724 / 2, send_stream->GetLastHeight());
2163
2164 // Trigger underuse which should go back up in resolution.
2165 int current_pixel_count =
2166 send_stream->GetLastWidth() * send_stream->GetLastHeight();
2167 // Cap the max to 4x the pixel count (assuming max 1/2 x 1/2 scale downs)
2168 // of the current stream, so we don't take too large steps.
2169 wants.max_pixel_count = current_pixel_count * 4;
2170 // Default step down is 3/5 pixel count, so go up by 5/3.
2171 wants.target_pixel_count = rtc::Optional<int>((current_pixel_count * 5) / 3);
2172 send_stream->InjectVideoSinkWants(wants);
2173 EXPECT_TRUE(capturer.CaptureCustomFrame(1284, 724, cricket::FOURCC_I420));
2174 EXPECT_EQ(5, send_stream->GetNumberOfSwappedFrames());
2175 EXPECT_EQ(1284 * 3 / 4, send_stream->GetLastWidth());
2176 EXPECT_EQ(724 * 3 / 4, send_stream->GetLastHeight());
2177
2178 // Trigger underuse again, should go back up to full resolution.
2179 current_pixel_count =
2180 send_stream->GetLastWidth() * send_stream->GetLastHeight();
2181 wants.max_pixel_count = current_pixel_count * 4;
2182 wants.target_pixel_count = rtc::Optional<int>((current_pixel_count * 5) / 3);
2183 send_stream->InjectVideoSinkWants(wants);
2184 EXPECT_TRUE(capturer.CaptureCustomFrame(1284, 724, cricket::FOURCC_I420));
2185 EXPECT_EQ(6, send_stream->GetNumberOfSwappedFrames());
2186 EXPECT_EQ(1284, send_stream->GetLastWidth());
2187 EXPECT_EQ(724, send_stream->GetLastHeight());
2188
2189 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2190 }
2191
2192 TEST_F(WebRtcVideoChannel2Test, PreviousAdaptationDoesNotApplyToScreenshare) {
2193 cricket::VideoCodec codec = GetEngineCodec("VP8");
2194 cricket::VideoSendParameters parameters;
2195 parameters.codecs.push_back(codec);
2196
2197 MediaConfig media_config = GetMediaConfig();
2198 media_config.video.enable_cpu_overuse_detection = true;
2199 channel_.reset(
2200 engine_.CreateChannel(fake_call_.get(), media_config, VideoOptions()));
2201 channel_->OnReadyToSend(true);
2202 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2203
2204 AddSendStream();
2205
2206 cricket::FakeVideoCapturer capturer;
2207 ASSERT_EQ(cricket::CS_RUNNING,
2208 capturer.Start(capturer.GetSupportedFormats()->front()));
2209 ASSERT_TRUE(channel_->SetSend(true));
2210 cricket::VideoOptions camera_options;
2211 camera_options.is_screencast = rtc::Optional<bool>(false);
2212 channel_->SetVideoSend(last_ssrc_, true /* enable */, &camera_options,
2213 &capturer);
2214
2215 ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
2216 FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
2217
2218 EXPECT_TRUE(capturer.CaptureCustomFrame(1280, 720, cricket::FOURCC_I420));
2219 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
2220 EXPECT_EQ(1280, send_stream->GetLastWidth());
2221 EXPECT_EQ(720, send_stream->GetLastHeight());
2222
2223 // Trigger overuse.
2224 rtc::VideoSinkWants wants;
2225 wants.max_pixel_count =
2226 send_stream->GetLastWidth() * send_stream->GetLastHeight() - 1;
2227 send_stream->InjectVideoSinkWants(wants);
2228 EXPECT_TRUE(capturer.CaptureCustomFrame(1280, 720, cricket::FOURCC_I420));
2229 EXPECT_EQ(2, send_stream->GetNumberOfSwappedFrames());
2230 EXPECT_EQ(1280 * 3 / 4, send_stream->GetLastWidth());
2231 EXPECT_EQ(720 * 3 / 4, send_stream->GetLastHeight());
2232
2233 // Switch to screen share. Expect no CPU adaptation.
2234 cricket::FakeVideoCapturer screen_share(true);
2235 ASSERT_EQ(cricket::CS_RUNNING,
2236 screen_share.Start(screen_share.GetSupportedFormats()->front()));
2237 cricket::VideoOptions screenshare_options;
2238 screenshare_options.is_screencast = rtc::Optional<bool>(true);
2239 channel_->SetVideoSend(last_ssrc_, true /* enable */, &screenshare_options,
2240 &screen_share);
2241 EXPECT_TRUE(screen_share.CaptureCustomFrame(1284, 724, cricket::FOURCC_I420));
2242 ASSERT_EQ(2, fake_call_->GetNumCreatedSendStreams());
2243 send_stream = fake_call_->GetVideoSendStreams().front();
2244 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
2245 EXPECT_EQ(1284, send_stream->GetLastWidth());
2246 EXPECT_EQ(724, send_stream->GetLastHeight());
2247
2248 // Switch back to the normal capturer. Expect the frame to be CPU adapted.
2249 channel_->SetVideoSend(last_ssrc_, true /* enable */, &camera_options,
2250 &capturer);
2251 send_stream = fake_call_->GetVideoSendStreams().front();
2252 // We have a new fake send stream, so it doesn't remember the old sink wants.
2253 // In practice, it will be populated from
2254 // ViEEncoder::VideoSourceProxy::SetSource(), so simulate that here.
2255 send_stream->InjectVideoSinkWants(wants);
2256 EXPECT_TRUE(capturer.CaptureCustomFrame(1280, 720, cricket::FOURCC_I420));
2257 ASSERT_EQ(3, fake_call_->GetNumCreatedSendStreams());
2258 send_stream = fake_call_->GetVideoSendStreams().front();
2259 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
2260 EXPECT_EQ(1280 * 3 / 4, send_stream->GetLastWidth());
2261 EXPECT_EQ(720 * 3 / 4, send_stream->GetLastHeight());
2262
2263 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2264 }
2265
2266 void WebRtcVideoChannel2Test::TestCpuAdaptation(bool enable_overuse,
2267 bool is_screenshare) {
2268 const int kDefaultFps = 30;
2269 cricket::VideoCodec codec = GetEngineCodec("VP8");
2270 cricket::VideoSendParameters parameters;
2271 parameters.codecs.push_back(codec);
2272
2273 MediaConfig media_config = GetMediaConfig();
2274 if (enable_overuse) {
2275 media_config.video.enable_cpu_overuse_detection = true;
2276 }
2277 channel_.reset(
2278 engine_.CreateChannel(fake_call_.get(), media_config, VideoOptions()));
2279 channel_->OnReadyToSend(true);
2280
2281 EXPECT_TRUE(channel_->SetSendParameters(parameters));
2282
2283 AddSendStream();
2284
2285 cricket::FakeVideoCapturer capturer;
2286 VideoOptions options;
2287 options.is_screencast = rtc::Optional<bool>(is_screenshare);
2288 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
2289 cricket::VideoFormat capture_format = capturer.GetSupportedFormats()->front();
2290 capture_format.interval = rtc::kNumNanosecsPerSec / kDefaultFps;
2291 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(capture_format));
2292
2293 EXPECT_TRUE(channel_->SetSend(true));
2294
2295 FakeVideoSendStream* send_stream = fake_call_->GetVideoSendStreams().front();
2296
2297 if (!enable_overuse) {
2298 EXPECT_FALSE(send_stream->resolution_scaling_enabled());
2299 EXPECT_FALSE(send_stream->framerate_scaling_enabled());
2300 EXPECT_EQ(is_screenshare, send_stream->framerate_scaling_enabled());
2301
2302 EXPECT_TRUE(capturer.CaptureFrame());
2303 EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
2304
2305 EXPECT_EQ(capture_format.width, send_stream->GetLastWidth());
2306 EXPECT_EQ(capture_format.height, send_stream->GetLastHeight());
2307
2308 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2309 return;
2310 }
2311
2312 if (is_screenshare) {
2313 EXPECT_FALSE(send_stream->resolution_scaling_enabled());
2314 EXPECT_TRUE(send_stream->framerate_scaling_enabled());
2315 } else {
2316 EXPECT_TRUE(send_stream->resolution_scaling_enabled());
2317 EXPECT_FALSE(send_stream->framerate_scaling_enabled());
2318 }
2319
2320 // Trigger overuse.
2321 ASSERT_EQ(1u, fake_call_->GetVideoSendStreams().size());
2322
2323 rtc::VideoSinkWants wants;
2324 if (is_screenshare) {
2325 wants.max_framerate_fps = (kDefaultFps * 2) / 3;
2326 } else {
2327 wants.max_pixel_count = capture_format.width * capture_format.height - 1;
2328 }
2329 send_stream->InjectVideoSinkWants(wants);
2330
2331 for (int i = 0; i < kDefaultFps; ++i)
2332 EXPECT_TRUE(capturer.CaptureFrame());
2333
2334 if (is_screenshare) {
2335 // Drops every third frame.
2336 EXPECT_EQ(kDefaultFps * 2 / 3, send_stream->GetNumberOfSwappedFrames());
2337 EXPECT_EQ(send_stream->GetLastWidth(), capture_format.width);
2338 EXPECT_EQ(send_stream->GetLastHeight(), capture_format.height);
2339 } else {
2340 EXPECT_EQ(kDefaultFps, send_stream->GetNumberOfSwappedFrames());
2341 EXPECT_LT(send_stream->GetLastWidth(), capture_format.width);
2342 EXPECT_LT(send_stream->GetLastHeight(), capture_format.height);
2343 }
2344
2345 // Trigger underuse which should go back to normal resolution.
2346 int last_pixel_count =
2347 send_stream->GetLastWidth() * send_stream->GetLastHeight();
2348 if (is_screenshare) {
2349 wants.max_framerate_fps = kDefaultFps;
2350 } else {
2351 wants.max_pixel_count = last_pixel_count * 4;
2352 wants.target_pixel_count.emplace((last_pixel_count * 5) / 3);
2353 }
2354 send_stream->InjectVideoSinkWants(wants);
2355
2356 for (int i = 0; i < kDefaultFps; ++i)
2357 EXPECT_TRUE(capturer.CaptureFrame());
2358
2359 if (is_screenshare) {
2360 EXPECT_EQ(kDefaultFps + (kDefaultFps * 2 / 3),
2361 send_stream->GetNumberOfSwappedFrames());
2362 } else {
2363 EXPECT_EQ(kDefaultFps * 2, send_stream->GetNumberOfSwappedFrames());
2364 }
2365
2366 EXPECT_EQ(capture_format.width, send_stream->GetLastWidth());
2367 EXPECT_EQ(capture_format.height, send_stream->GetLastHeight());
2368
2369 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2370 }
2371
2372 TEST_F(WebRtcVideoChannel2Test, EstimatesNtpStartTimeCorrectly) {
2373 // Start at last timestamp to verify that wraparounds are estimated correctly.
2374 static const uint32_t kInitialTimestamp = 0xFFFFFFFFu;
2375 static const int64_t kInitialNtpTimeMs = 1247891230;
2376 static const int kFrameOffsetMs = 20;
2377 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
2378
2379 FakeVideoReceiveStream* stream = AddRecvStream();
2380 cricket::FakeVideoRenderer renderer;
2381 EXPECT_TRUE(channel_->SetSink(last_ssrc_, &renderer));
2382
2383 webrtc::VideoFrame video_frame(CreateBlackFrameBuffer(4, 4),
2384 kInitialTimestamp, 0,
2385 webrtc::kVideoRotation_0);
2386 // Initial NTP time is not available on the first frame, but should still be
2387 // able to be estimated.
2388 stream->InjectFrame(video_frame);
2389
2390 EXPECT_EQ(1, renderer.num_rendered_frames());
2391
2392 // This timestamp is kInitialTimestamp (-1) + kFrameOffsetMs * 90, which
2393 // triggers a constant-overflow warning, hence we're calculating it explicitly
2394 // here.
2395 video_frame.set_timestamp(kFrameOffsetMs * 90 - 1);
2396 video_frame.set_ntp_time_ms(kInitialNtpTimeMs + kFrameOffsetMs);
2397 stream->InjectFrame(video_frame);
2398
2399 EXPECT_EQ(2, renderer.num_rendered_frames());
2400
2401 // Verify that NTP time has been correctly deduced.
2402 cricket::VideoMediaInfo info;
2403 ASSERT_TRUE(channel_->GetStats(&info));
2404 ASSERT_EQ(1u, info.receivers.size());
2405 EXPECT_EQ(kInitialNtpTimeMs, info.receivers[0].capture_start_ntp_time_ms);
2406 }
2407
2408 TEST_F(WebRtcVideoChannel2Test, SetDefaultSendCodecs) {
2409 ASSERT_TRUE(channel_->SetSendParameters(send_parameters_));
2410
2411 VideoCodec codec;
2412 EXPECT_TRUE(channel_->GetSendCodec(&codec));
2413 EXPECT_TRUE(codec.Matches(engine_.codecs()[0]));
2414
2415 // Using a RTX setup to verify that the default RTX payload type is good.
2416 const std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs1);
2417 const std::vector<uint32_t> rtx_ssrcs = MAKE_VECTOR(kRtxSsrcs1);
2418 FakeVideoSendStream* stream = AddSendStream(
2419 cricket::CreateSimWithRtxStreamParams("cname", ssrcs, rtx_ssrcs));
2420 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2421
2422 // Make sure NACK and FEC are enabled on the correct payload types.
2423 EXPECT_EQ(1000, config.rtp.nack.rtp_history_ms);
2424 EXPECT_EQ(GetEngineCodec("ulpfec").id, config.rtp.ulpfec.ulpfec_payload_type);
2425 EXPECT_EQ(GetEngineCodec("red").id, config.rtp.ulpfec.red_payload_type);
2426
2427 EXPECT_EQ(1u, config.rtp.rtx.ssrcs.size());
2428 EXPECT_EQ(kRtxSsrcs1[0], config.rtp.rtx.ssrcs[0]);
2429 VerifySendStreamHasRtxTypes(config, default_apt_rtx_types_);
2430 // TODO(juberti): Check RTCP, PLI, TMMBR.
2431 }
2432
2433 // The following four tests ensures that FlexFEC is not activated by default
2434 // when the field trials are not enabled.
2435 // TODO(brandtr): Remove or update these tests when FlexFEC _is_ enabled by
2436 // default.
2437 TEST_F(WebRtcVideoChannel2Test,
2438 FlexfecSendCodecWithoutSsrcNotExposedByDefault) {
2439 FakeVideoSendStream* stream = AddSendStream();
2440 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2441
2442 EXPECT_EQ(-1, config.rtp.flexfec.payload_type);
2443 EXPECT_EQ(0U, config.rtp.flexfec.ssrc);
2444 EXPECT_TRUE(config.rtp.flexfec.protected_media_ssrcs.empty());
2445 }
2446
2447 TEST_F(WebRtcVideoChannel2Test, FlexfecSendCodecWithSsrcNotExposedByDefault) {
2448 FakeVideoSendStream* stream = AddSendStream(
2449 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2450 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2451
2452 EXPECT_EQ(-1, config.rtp.flexfec.payload_type);
2453 EXPECT_EQ(0U, config.rtp.flexfec.ssrc);
2454 EXPECT_TRUE(config.rtp.flexfec.protected_media_ssrcs.empty());
2455 }
2456
2457 TEST_F(WebRtcVideoChannel2Test,
2458 FlexfecRecvCodecWithoutSsrcNotExposedByDefault) {
2459 AddRecvStream();
2460
2461 const std::vector<FakeFlexfecReceiveStream*>& streams =
2462 fake_call_->GetFlexfecReceiveStreams();
2463 EXPECT_TRUE(streams.empty());
2464 }
2465
2466 TEST_F(WebRtcVideoChannel2Test, FlexfecRecvCodecWithSsrcNotExposedByDefault) {
2467 AddRecvStream(
2468 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2469
2470 const std::vector<FakeFlexfecReceiveStream*>& streams =
2471 fake_call_->GetFlexfecReceiveStreams();
2472 EXPECT_TRUE(streams.empty());
2473 }
2474
2475 // TODO(brandtr): When FlexFEC is no longer behind a field trial, merge all
2476 // tests that use this test fixture into the corresponding "non-field trial"
2477 // tests.
2478 class WebRtcVideoChannel2FlexfecRecvTest : public WebRtcVideoChannel2Test {
2479 public:
2480 WebRtcVideoChannel2FlexfecRecvTest()
2481 : WebRtcVideoChannel2Test("WebRTC-FlexFEC-03-Advertised/Enabled/") {}
2482 };
2483
2484 TEST_F(WebRtcVideoChannel2FlexfecRecvTest,
2485 DefaultFlexfecCodecHasTransportCcAndRembFeedbackParam) {
2486 EXPECT_TRUE(cricket::HasTransportCc(GetEngineCodec("flexfec-03")));
2487 EXPECT_TRUE(cricket::HasRemb(GetEngineCodec("flexfec-03")));
2488 }
2489
2490 TEST_F(WebRtcVideoChannel2FlexfecRecvTest, SetDefaultRecvCodecsWithoutSsrc) {
2491 AddRecvStream();
2492
2493 const std::vector<FakeFlexfecReceiveStream*>& streams =
2494 fake_call_->GetFlexfecReceiveStreams();
2495 EXPECT_TRUE(streams.empty());
2496 }
2497
2498 TEST_F(WebRtcVideoChannel2FlexfecRecvTest, SetDefaultRecvCodecsWithSsrc) {
2499 AddRecvStream(
2500 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2501
2502 const std::vector<FakeFlexfecReceiveStream*>& streams =
2503 fake_call_->GetFlexfecReceiveStreams();
2504 ASSERT_EQ(1U, streams.size());
2505 const FakeFlexfecReceiveStream* stream = streams.front();
2506 const webrtc::FlexfecReceiveStream::Config& config = stream->GetConfig();
2507 EXPECT_EQ(GetEngineCodec("flexfec-03").id, config.payload_type);
2508 EXPECT_EQ(kFlexfecSsrc, config.remote_ssrc);
2509 ASSERT_EQ(1U, config.protected_media_ssrcs.size());
2510 EXPECT_EQ(kSsrcs1[0], config.protected_media_ssrcs[0]);
2511 }
2512
2513 TEST_F(WebRtcVideoChannel2FlexfecRecvTest,
2514 EnablingFlexfecDoesNotRecreateVideoReceiveStream) {
2515 cricket::VideoRecvParameters recv_parameters;
2516 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
2517 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
2518
2519 AddRecvStream(
2520 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2521 EXPECT_EQ(1, fake_call_->GetNumCreatedReceiveStreams());
2522 EXPECT_EQ(1U, fake_call_->GetVideoReceiveStreams().size());
2523
2524 // Enable FlexFEC.
2525 recv_parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
2526 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
2527 EXPECT_EQ(2, fake_call_->GetNumCreatedReceiveStreams())
2528 << "Enabling FlexFEC should create FlexfecReceiveStream.";
2529 EXPECT_EQ(1U, fake_call_->GetVideoReceiveStreams().size())
2530 << "Enabling FlexFEC should not create VideoReceiveStream.";
2531 EXPECT_EQ(1U, fake_call_->GetFlexfecReceiveStreams().size())
2532 << "Enabling FlexFEC should create a single FlexfecReceiveStream.";
2533 }
2534
2535 TEST_F(WebRtcVideoChannel2FlexfecRecvTest,
2536 DisablingFlexfecDoesNotRecreateVideoReceiveStream) {
2537 cricket::VideoRecvParameters recv_parameters;
2538 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
2539 recv_parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
2540 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
2541
2542 AddRecvStream(
2543 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2544 EXPECT_EQ(2, fake_call_->GetNumCreatedReceiveStreams());
2545 EXPECT_EQ(1U, fake_call_->GetVideoReceiveStreams().size());
2546 EXPECT_EQ(1U, fake_call_->GetFlexfecReceiveStreams().size());
2547
2548 // Disable FlexFEC.
2549 recv_parameters.codecs.clear();
2550 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
2551 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
2552 EXPECT_EQ(2, fake_call_->GetNumCreatedReceiveStreams())
2553 << "Disabling FlexFEC should not recreate VideoReceiveStream.";
2554 EXPECT_EQ(1U, fake_call_->GetVideoReceiveStreams().size())
2555 << "Disabling FlexFEC should not destroy VideoReceiveStream.";
2556 EXPECT_TRUE(fake_call_->GetFlexfecReceiveStreams().empty())
2557 << "Disabling FlexFEC should destroy FlexfecReceiveStream.";
2558 }
2559
2560 // TODO(brandtr): When FlexFEC is no longer behind a field trial, merge all
2561 // tests that use this test fixture into the corresponding "non-field trial"
2562 // tests.
2563 class WebRtcVideoChannel2FlexfecSendRecvTest : public WebRtcVideoChannel2Test {
2564 public:
2565 WebRtcVideoChannel2FlexfecSendRecvTest()
2566 : WebRtcVideoChannel2Test(
2567 "WebRTC-FlexFEC-03-Advertised/Enabled/WebRTC-FlexFEC-03/Enabled/") {
2568 }
2569 };
2570
2571 TEST_F(WebRtcVideoChannel2FlexfecSendRecvTest,
2572 SetDefaultSendCodecsWithoutSsrc) {
2573 FakeVideoSendStream* stream = AddSendStream();
2574 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2575
2576 EXPECT_EQ(GetEngineCodec("flexfec-03").id, config.rtp.flexfec.payload_type);
2577 EXPECT_EQ(0U, config.rtp.flexfec.ssrc);
2578 EXPECT_TRUE(config.rtp.flexfec.protected_media_ssrcs.empty());
2579 }
2580
2581 TEST_F(WebRtcVideoChannel2FlexfecSendRecvTest, SetDefaultSendCodecsWithSsrc) {
2582 FakeVideoSendStream* stream = AddSendStream(
2583 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2584 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2585
2586 EXPECT_EQ(GetEngineCodec("flexfec-03").id, config.rtp.flexfec.payload_type);
2587 EXPECT_EQ(kFlexfecSsrc, config.rtp.flexfec.ssrc);
2588 ASSERT_EQ(1U, config.rtp.flexfec.protected_media_ssrcs.size());
2589 EXPECT_EQ(kSsrcs1[0], config.rtp.flexfec.protected_media_ssrcs[0]);
2590 }
2591
2592 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsWithoutFec) {
2593 cricket::VideoSendParameters parameters;
2594 parameters.codecs.push_back(GetEngineCodec("VP8"));
2595 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2596
2597 FakeVideoSendStream* stream = AddSendStream();
2598 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2599
2600 EXPECT_EQ(-1, config.rtp.ulpfec.ulpfec_payload_type);
2601 EXPECT_EQ(-1, config.rtp.ulpfec.red_payload_type);
2602 }
2603
2604 TEST_F(WebRtcVideoChannel2FlexfecSendRecvTest, SetSendCodecsWithoutFec) {
2605 cricket::VideoSendParameters parameters;
2606 parameters.codecs.push_back(GetEngineCodec("VP8"));
2607 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2608
2609 FakeVideoSendStream* stream = AddSendStream();
2610 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2611
2612 EXPECT_EQ(-1, config.rtp.flexfec.payload_type);
2613 }
2614
2615 TEST_F(WebRtcVideoChannel2FlexfecRecvTest, SetRecvCodecsWithFec) {
2616 AddRecvStream(
2617 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2618
2619 cricket::VideoRecvParameters recv_parameters;
2620 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
2621 recv_parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
2622 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
2623
2624 const std::vector<FakeFlexfecReceiveStream*>& flexfec_streams =
2625 fake_call_->GetFlexfecReceiveStreams();
2626 ASSERT_EQ(1U, flexfec_streams.size());
2627 const FakeFlexfecReceiveStream* flexfec_stream = flexfec_streams.front();
2628 const webrtc::FlexfecReceiveStream::Config& flexfec_stream_config =
2629 flexfec_stream->GetConfig();
2630 EXPECT_EQ(GetEngineCodec("flexfec-03").id,
2631 flexfec_stream_config.payload_type);
2632 EXPECT_EQ(kFlexfecSsrc, flexfec_stream_config.remote_ssrc);
2633 ASSERT_EQ(1U, flexfec_stream_config.protected_media_ssrcs.size());
2634 EXPECT_EQ(kSsrcs1[0], flexfec_stream_config.protected_media_ssrcs[0]);
2635 const std::vector<FakeVideoReceiveStream*>& video_streams =
2636 fake_call_->GetVideoReceiveStreams();
2637 const FakeVideoReceiveStream* video_stream = video_streams.front();
2638 const webrtc::VideoReceiveStream::Config& video_stream_config =
2639 video_stream->GetConfig();
2640 EXPECT_EQ(video_stream_config.rtp.local_ssrc,
2641 flexfec_stream_config.local_ssrc);
2642 EXPECT_EQ(video_stream_config.rtp.rtcp_mode, flexfec_stream_config.rtcp_mode);
2643 EXPECT_EQ(video_stream_config.rtcp_send_transport,
2644 flexfec_stream_config.rtcp_send_transport);
2645 // TODO(brandtr): Update this EXPECT when we set |transport_cc| in a
2646 // spec-compliant way.
2647 EXPECT_EQ(video_stream_config.rtp.transport_cc,
2648 flexfec_stream_config.transport_cc);
2649 EXPECT_EQ(video_stream_config.rtp.rtcp_mode, flexfec_stream_config.rtcp_mode);
2650 EXPECT_EQ(video_stream_config.rtp.extensions,
2651 flexfec_stream_config.rtp_header_extensions);
2652 }
2653
2654 // We should not send FlexFEC, even if we advertise it, unless the right
2655 // field trial is set.
2656 // TODO(brandtr): Remove when FlexFEC is enabled by default.
2657 TEST_F(WebRtcVideoChannel2FlexfecRecvTest,
2658 SetSendCodecsWithoutSsrcWithFecDoesNotEnableFec) {
2659 cricket::VideoSendParameters parameters;
2660 parameters.codecs.push_back(GetEngineCodec("VP8"));
2661 parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
2662 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2663
2664 FakeVideoSendStream* stream = AddSendStream();
2665 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2666
2667 EXPECT_EQ(-1, config.rtp.flexfec.payload_type);
2668 EXPECT_EQ(0, config.rtp.flexfec.ssrc);
2669 EXPECT_TRUE(config.rtp.flexfec.protected_media_ssrcs.empty());
2670 }
2671
2672 TEST_F(WebRtcVideoChannel2FlexfecRecvTest,
2673 SetSendCodecsWithSsrcWithFecDoesNotEnableFec) {
2674 cricket::VideoSendParameters parameters;
2675 parameters.codecs.push_back(GetEngineCodec("VP8"));
2676 parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
2677 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2678
2679 FakeVideoSendStream* stream = AddSendStream(
2680 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2681 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2682
2683 EXPECT_EQ(-1, config.rtp.flexfec.payload_type);
2684 EXPECT_EQ(0, config.rtp.flexfec.ssrc);
2685 EXPECT_TRUE(config.rtp.flexfec.protected_media_ssrcs.empty());
2686 }
2687
2688 TEST_F(WebRtcVideoChannel2Test,
2689 SetSendCodecRejectsRtxWithoutAssociatedPayloadType) {
2690 const int kUnusedPayloadType = 127;
2691 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType));
2692
2693 cricket::VideoSendParameters parameters;
2694 cricket::VideoCodec rtx_codec(kUnusedPayloadType, "rtx");
2695 parameters.codecs.push_back(rtx_codec);
2696 EXPECT_FALSE(channel_->SetSendParameters(parameters))
2697 << "RTX codec without associated payload type should be rejected.";
2698 }
2699
2700 TEST_F(WebRtcVideoChannel2Test,
2701 SetSendCodecRejectsRtxWithoutMatchingVideoCodec) {
2702 const int kUnusedPayloadType1 = 126;
2703 const int kUnusedPayloadType2 = 127;
2704 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType1));
2705 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType2));
2706 {
2707 cricket::VideoCodec rtx_codec = cricket::VideoCodec::CreateRtxCodec(
2708 kUnusedPayloadType1, GetEngineCodec("VP8").id);
2709 cricket::VideoSendParameters parameters;
2710 parameters.codecs.push_back(GetEngineCodec("VP8"));
2711 parameters.codecs.push_back(rtx_codec);
2712 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2713 }
2714 {
2715 cricket::VideoCodec rtx_codec = cricket::VideoCodec::CreateRtxCodec(
2716 kUnusedPayloadType1, kUnusedPayloadType2);
2717 cricket::VideoSendParameters parameters;
2718 parameters.codecs.push_back(GetEngineCodec("VP8"));
2719 parameters.codecs.push_back(rtx_codec);
2720 EXPECT_FALSE(channel_->SetSendParameters(parameters))
2721 << "RTX without matching video codec should be rejected.";
2722 }
2723 }
2724
2725 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsWithChangedRtxPayloadType) {
2726 const int kUnusedPayloadType1 = 126;
2727 const int kUnusedPayloadType2 = 127;
2728 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType1));
2729 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType2));
2730
2731 // SSRCs for RTX.
2732 cricket::StreamParams params =
2733 cricket::StreamParams::CreateLegacy(kSsrcs1[0]);
2734 params.AddFidSsrc(kSsrcs1[0], kRtxSsrcs1[0]);
2735 AddSendStream(params);
2736
2737 // Original payload type for RTX.
2738 cricket::VideoSendParameters parameters;
2739 parameters.codecs.push_back(GetEngineCodec("VP8"));
2740 cricket::VideoCodec rtx_codec(kUnusedPayloadType1, "rtx");
2741 rtx_codec.SetParam("apt", GetEngineCodec("VP8").id);
2742 parameters.codecs.push_back(rtx_codec);
2743 EXPECT_TRUE(channel_->SetSendParameters(parameters));
2744 ASSERT_EQ(1U, fake_call_->GetVideoSendStreams().size());
2745 const webrtc::VideoSendStream::Config& config_before =
2746 fake_call_->GetVideoSendStreams()[0]->GetConfig();
2747 EXPECT_EQ(kUnusedPayloadType1, config_before.rtp.rtx.payload_type);
2748 ASSERT_EQ(1U, config_before.rtp.rtx.ssrcs.size());
2749 EXPECT_EQ(kRtxSsrcs1[0], config_before.rtp.rtx.ssrcs[0]);
2750
2751 // Change payload type for RTX.
2752 parameters.codecs[1].id = kUnusedPayloadType2;
2753 EXPECT_TRUE(channel_->SetSendParameters(parameters));
2754 ASSERT_EQ(1U, fake_call_->GetVideoSendStreams().size());
2755 const webrtc::VideoSendStream::Config& config_after =
2756 fake_call_->GetVideoSendStreams()[0]->GetConfig();
2757 EXPECT_EQ(kUnusedPayloadType2, config_after.rtp.rtx.payload_type);
2758 ASSERT_EQ(1U, config_after.rtp.rtx.ssrcs.size());
2759 EXPECT_EQ(kRtxSsrcs1[0], config_after.rtp.rtx.ssrcs[0]);
2760 }
2761
2762 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsWithoutFecDisablesFec) {
2763 cricket::VideoSendParameters parameters;
2764 parameters.codecs.push_back(GetEngineCodec("VP8"));
2765 parameters.codecs.push_back(GetEngineCodec("ulpfec"));
2766 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2767
2768 FakeVideoSendStream* stream = AddSendStream();
2769 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2770
2771 EXPECT_EQ(GetEngineCodec("ulpfec").id, config.rtp.ulpfec.ulpfec_payload_type);
2772
2773 parameters.codecs.pop_back();
2774 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2775 stream = fake_call_->GetVideoSendStreams()[0];
2776 ASSERT_TRUE(stream != nullptr);
2777 config = stream->GetConfig().Copy();
2778 EXPECT_EQ(-1, config.rtp.ulpfec.ulpfec_payload_type)
2779 << "SetSendCodec without ULPFEC should disable current ULPFEC.";
2780 }
2781
2782 TEST_F(WebRtcVideoChannel2FlexfecSendRecvTest,
2783 SetSendCodecsWithoutFecDisablesFec) {
2784 cricket::VideoSendParameters parameters;
2785 parameters.codecs.push_back(GetEngineCodec("VP8"));
2786 parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
2787 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2788
2789 FakeVideoSendStream* stream = AddSendStream(
2790 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
2791 webrtc::VideoSendStream::Config config = stream->GetConfig().Copy();
2792
2793 EXPECT_EQ(GetEngineCodec("flexfec-03").id, config.rtp.flexfec.payload_type);
2794 EXPECT_EQ(kFlexfecSsrc, config.rtp.flexfec.ssrc);
2795 ASSERT_EQ(1U, config.rtp.flexfec.protected_media_ssrcs.size());
2796 EXPECT_EQ(kSsrcs1[0], config.rtp.flexfec.protected_media_ssrcs[0]);
2797
2798 parameters.codecs.pop_back();
2799 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2800 stream = fake_call_->GetVideoSendStreams()[0];
2801 ASSERT_TRUE(stream != nullptr);
2802 config = stream->GetConfig().Copy();
2803 EXPECT_EQ(-1, config.rtp.flexfec.payload_type)
2804 << "SetSendCodec without FlexFEC should disable current FlexFEC.";
2805 }
2806
2807 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsChangesExistingStreams) {
2808 cricket::VideoSendParameters parameters;
2809 cricket::VideoCodec codec(100, "VP8");
2810 codec.SetParam(kCodecParamMaxQuantization, kDefaultQpMax);
2811 parameters.codecs.push_back(codec);
2812
2813 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2814 channel_->SetSend(true);
2815
2816 FakeVideoSendStream* stream = AddSendStream();
2817 cricket::FakeVideoCapturer capturer;
2818 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
2819
2820 std::vector<webrtc::VideoStream> streams = stream->GetVideoStreams();
2821 EXPECT_EQ(kDefaultQpMax, streams[0].max_qp);
2822
2823 parameters.codecs.clear();
2824 codec.SetParam(kCodecParamMaxQuantization, kDefaultQpMax + 1);
2825 parameters.codecs.push_back(codec);
2826 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2827 streams = fake_call_->GetVideoSendStreams()[0]->GetVideoStreams();
2828 EXPECT_EQ(kDefaultQpMax + 1, streams[0].max_qp);
2829 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2830 }
2831
2832 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsWithBitrates) {
2833 SetSendCodecsShouldWorkForBitrates("100", 100000, "150", 150000, "200",
2834 200000);
2835 }
2836
2837 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsWithHighMaxBitrate) {
2838 SetSendCodecsShouldWorkForBitrates("", 0, "", -1, "10000", 10000000);
2839 std::vector<webrtc::VideoStream> streams = AddSendStream()->GetVideoStreams();
2840 ASSERT_EQ(1u, streams.size());
2841 EXPECT_EQ(10000000, streams[0].max_bitrate_bps);
2842 }
2843
2844 TEST_F(WebRtcVideoChannel2Test,
2845 SetSendCodecsWithoutBitratesUsesCorrectDefaults) {
2846 SetSendCodecsShouldWorkForBitrates(
2847 "", 0, "", -1, "", -1);
2848 }
2849
2850 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsCapsMinAndStartBitrate) {
2851 SetSendCodecsShouldWorkForBitrates("-1", 0, "-100", -1, "", -1);
2852 }
2853
2854 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsRejectsMaxLessThanMinBitrate) {
2855 send_parameters_.codecs[0].params[kCodecParamMinBitrate] = "300";
2856 send_parameters_.codecs[0].params[kCodecParamMaxBitrate] = "200";
2857 EXPECT_FALSE(channel_->SetSendParameters(send_parameters_));
2858 }
2859
2860 // Test that when both the codec-specific bitrate params and max_bandwidth_bps
2861 // are present in the same send parameters, the settings are combined correctly.
2862 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsWithBitratesAndMaxSendBandwidth) {
2863 send_parameters_.codecs[0].params[kCodecParamMinBitrate] = "100";
2864 send_parameters_.codecs[0].params[kCodecParamStartBitrate] = "200";
2865 send_parameters_.codecs[0].params[kCodecParamMaxBitrate] = "300";
2866 send_parameters_.max_bandwidth_bps = 400000;
2867 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2868 EXPECT_EQ(100000, fake_call_->GetConfig().bitrate_config.min_bitrate_bps);
2869 EXPECT_EQ(200000, fake_call_->GetConfig().bitrate_config.start_bitrate_bps);
2870 // We expect max_bandwidth_bps to take priority, if set.
2871 EXPECT_EQ(400000, fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2872
2873 // Decrease max_bandwidth_bps.
2874 send_parameters_.max_bandwidth_bps = 350000;
2875 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2876 EXPECT_EQ(100000, fake_call_->GetConfig().bitrate_config.min_bitrate_bps);
2877 // Since the codec isn't changing, start_bitrate_bps should be -1.
2878 EXPECT_EQ(-1, fake_call_->GetConfig().bitrate_config.start_bitrate_bps);
2879 EXPECT_EQ(350000, fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2880
2881 // Now try again with the values flipped around.
2882 send_parameters_.codecs[0].params[kCodecParamMaxBitrate] = "400";
2883 send_parameters_.max_bandwidth_bps = 300000;
2884 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2885 EXPECT_EQ(100000, fake_call_->GetConfig().bitrate_config.min_bitrate_bps);
2886 EXPECT_EQ(200000, fake_call_->GetConfig().bitrate_config.start_bitrate_bps);
2887 EXPECT_EQ(300000, fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2888
2889 // If we change the codec max, max_bandwidth_bps should still apply.
2890 send_parameters_.codecs[0].params[kCodecParamMaxBitrate] = "350";
2891 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2892 EXPECT_EQ(100000, fake_call_->GetConfig().bitrate_config.min_bitrate_bps);
2893 EXPECT_EQ(200000, fake_call_->GetConfig().bitrate_config.start_bitrate_bps);
2894 EXPECT_EQ(300000, fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2895 }
2896
2897 TEST_F(WebRtcVideoChannel2Test,
2898 SetMaxSendBandwidthShouldPreserveOtherBitrates) {
2899 SetSendCodecsShouldWorkForBitrates("100", 100000, "150", 150000, "200",
2900 200000);
2901 send_parameters_.max_bandwidth_bps = 300000;
2902 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2903 EXPECT_EQ(100000, fake_call_->GetConfig().bitrate_config.min_bitrate_bps)
2904 << "Setting max bitrate should keep previous min bitrate.";
2905 EXPECT_EQ(-1, fake_call_->GetConfig().bitrate_config.start_bitrate_bps)
2906 << "Setting max bitrate should not reset start bitrate.";
2907 EXPECT_EQ(300000, fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2908 }
2909
2910 TEST_F(WebRtcVideoChannel2Test, SetMaxSendBandwidthShouldBeRemovable) {
2911 send_parameters_.max_bandwidth_bps = 300000;
2912 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2913 EXPECT_EQ(300000, fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2914 // -1 means to disable max bitrate (set infinite).
2915 send_parameters_.max_bandwidth_bps = -1;
2916 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2917 EXPECT_EQ(-1, fake_call_->GetConfig().bitrate_config.max_bitrate_bps)
2918 << "Setting zero max bitrate did not reset start bitrate.";
2919 }
2920
2921 TEST_F(WebRtcVideoChannel2Test, SetMaxSendBandwidthAndAddSendStream) {
2922 send_parameters_.max_bandwidth_bps = 99999;
2923 FakeVideoSendStream* stream = AddSendStream();
2924 ASSERT_TRUE(channel_->SetSendParameters(send_parameters_));
2925 EXPECT_EQ(send_parameters_.max_bandwidth_bps,
2926 fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2927 ASSERT_EQ(1u, stream->GetVideoStreams().size());
2928 EXPECT_EQ(send_parameters_.max_bandwidth_bps,
2929 stream->GetVideoStreams()[0].max_bitrate_bps);
2930
2931 send_parameters_.max_bandwidth_bps = 77777;
2932 ASSERT_TRUE(channel_->SetSendParameters(send_parameters_));
2933 EXPECT_EQ(send_parameters_.max_bandwidth_bps,
2934 fake_call_->GetConfig().bitrate_config.max_bitrate_bps);
2935 EXPECT_EQ(send_parameters_.max_bandwidth_bps,
2936 stream->GetVideoStreams()[0].max_bitrate_bps);
2937 }
2938
2939 TEST_F(WebRtcVideoChannel2Test, SetMaxSendBitrateCanIncreaseSenderBitrate) {
2940 cricket::VideoSendParameters parameters;
2941 parameters.codecs.push_back(GetEngineCodec("VP8"));
2942 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2943 channel_->SetSend(true);
2944
2945 FakeVideoSendStream* stream = AddSendStream();
2946
2947 cricket::FakeVideoCapturer capturer;
2948 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
2949 EXPECT_EQ(cricket::CS_RUNNING,
2950 capturer.Start(capturer.GetSupportedFormats()->front()));
2951
2952 std::vector<webrtc::VideoStream> streams = stream->GetVideoStreams();
2953 int initial_max_bitrate_bps = streams[0].max_bitrate_bps;
2954 EXPECT_GT(initial_max_bitrate_bps, 0);
2955
2956 parameters.max_bandwidth_bps = initial_max_bitrate_bps * 2;
2957 EXPECT_TRUE(channel_->SetSendParameters(parameters));
2958 // Insert a frame to update the encoder config.
2959 EXPECT_TRUE(capturer.CaptureFrame());
2960 streams = stream->GetVideoStreams();
2961 EXPECT_EQ(initial_max_bitrate_bps * 2, streams[0].max_bitrate_bps);
2962 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
2963 }
2964
2965 TEST_F(WebRtcVideoChannel2Test,
2966 SetMaxSendBitrateCanIncreaseSimulcastSenderBitrate) {
2967 cricket::VideoSendParameters parameters;
2968 parameters.codecs.push_back(GetEngineCodec("VP8"));
2969 ASSERT_TRUE(channel_->SetSendParameters(parameters));
2970 channel_->SetSend(true);
2971
2972 FakeVideoSendStream* stream = AddSendStream(
2973 cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kSsrcs3)));
2974
2975 // Send a frame to make sure this scales up to >1 stream (simulcast).
2976 cricket::FakeVideoCapturer capturer;
2977 EXPECT_TRUE(channel_->SetVideoSend(kSsrcs3[0], true, nullptr, &capturer));
2978 EXPECT_EQ(cricket::CS_RUNNING,
2979 capturer.Start(capturer.GetSupportedFormats()->front()));
2980 EXPECT_TRUE(capturer.CaptureFrame());
2981
2982 std::vector<webrtc::VideoStream> streams = stream->GetVideoStreams();
2983 ASSERT_GT(streams.size(), 1u)
2984 << "Without simulcast this test doesn't make sense.";
2985 int initial_max_bitrate_bps = GetTotalMaxBitrateBps(streams);
2986 EXPECT_GT(initial_max_bitrate_bps, 0);
2987
2988 parameters.max_bandwidth_bps = initial_max_bitrate_bps * 2;
2989 EXPECT_TRUE(channel_->SetSendParameters(parameters));
2990 // Insert a frame to update the encoder config.
2991 EXPECT_TRUE(capturer.CaptureFrame());
2992 streams = stream->GetVideoStreams();
2993 int increased_max_bitrate_bps = GetTotalMaxBitrateBps(streams);
2994 EXPECT_EQ(initial_max_bitrate_bps * 2, increased_max_bitrate_bps);
2995
2996 EXPECT_TRUE(channel_->SetVideoSend(kSsrcs3[0], true, nullptr, nullptr));
2997 }
2998
2999 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsWithMaxQuantization) {
3000 static const char* kMaxQuantization = "21";
3001 cricket::VideoSendParameters parameters;
3002 parameters.codecs.push_back(GetEngineCodec("VP8"));
3003 parameters.codecs[0].params[kCodecParamMaxQuantization] = kMaxQuantization;
3004 EXPECT_TRUE(channel_->SetSendParameters(parameters));
3005 EXPECT_EQ(static_cast<unsigned int>(atoi(kMaxQuantization)),
3006 AddSendStream()->GetVideoStreams().back().max_qp);
3007
3008 VideoCodec codec;
3009 EXPECT_TRUE(channel_->GetSendCodec(&codec));
3010 EXPECT_EQ(kMaxQuantization, codec.params[kCodecParamMaxQuantization]);
3011 }
3012
3013 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsRejectBadPayloadTypes) {
3014 // TODO(pbos): Should we only allow the dynamic range?
3015 static const int kIncorrectPayloads[] = {-2, -1, 128, 129};
3016 cricket::VideoSendParameters parameters;
3017 parameters.codecs.push_back(GetEngineCodec("VP8"));
3018 for (size_t i = 0; i < arraysize(kIncorrectPayloads); ++i) {
3019 parameters.codecs[0].id = kIncorrectPayloads[i];
3020 EXPECT_FALSE(channel_->SetSendParameters(parameters))
3021 << "Bad payload type '" << kIncorrectPayloads[i] << "' accepted.";
3022 }
3023 }
3024
3025 TEST_F(WebRtcVideoChannel2Test, SetSendCodecsAcceptAllValidPayloadTypes) {
3026 cricket::VideoSendParameters parameters;
3027 parameters.codecs.push_back(GetEngineCodec("VP8"));
3028 for (int payload_type = 96; payload_type <= 127; ++payload_type) {
3029 parameters.codecs[0].id = payload_type;
3030 EXPECT_TRUE(channel_->SetSendParameters(parameters))
3031 << "Payload type '" << payload_type << "' rejected.";
3032 }
3033 }
3034
3035 // Test that setting the a different set of codecs but with an identical front
3036 // codec doesn't result in the stream being recreated.
3037 // This may happen when a subsequent negotiation includes fewer codecs, as a
3038 // result of one of the codecs being rejected.
3039 TEST_F(WebRtcVideoChannel2Test,
3040 SetSendCodecsIdenticalFirstCodecDoesntRecreateStream) {
3041 cricket::VideoSendParameters parameters1;
3042 parameters1.codecs.push_back(GetEngineCodec("VP8"));
3043 parameters1.codecs.push_back(GetEngineCodec("VP9"));
3044 EXPECT_TRUE(channel_->SetSendParameters(parameters1));
3045
3046 AddSendStream();
3047 EXPECT_EQ(1, fake_call_->GetNumCreatedSendStreams());
3048
3049 cricket::VideoSendParameters parameters2;
3050 parameters2.codecs.push_back(GetEngineCodec("VP8"));
3051 EXPECT_TRUE(channel_->SetSendParameters(parameters2));
3052 EXPECT_EQ(1, fake_call_->GetNumCreatedSendStreams());
3053 }
3054
3055 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsWithOnlyVp8) {
3056 cricket::VideoRecvParameters parameters;
3057 parameters.codecs.push_back(GetEngineCodec("VP8"));
3058 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3059 }
3060
3061 // Test that we set our inbound RTX codecs properly.
3062 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsWithRtx) {
3063 const int kUnusedPayloadType1 = 126;
3064 const int kUnusedPayloadType2 = 127;
3065 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType1));
3066 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType2));
3067
3068 cricket::VideoRecvParameters parameters;
3069 parameters.codecs.push_back(GetEngineCodec("VP8"));
3070 cricket::VideoCodec rtx_codec(kUnusedPayloadType1, "rtx");
3071 parameters.codecs.push_back(rtx_codec);
3072 EXPECT_FALSE(channel_->SetRecvParameters(parameters))
3073 << "RTX codec without associated payload should be rejected.";
3074
3075 parameters.codecs[1].SetParam("apt", kUnusedPayloadType2);
3076 EXPECT_FALSE(channel_->SetRecvParameters(parameters))
3077 << "RTX codec with invalid associated payload type should be rejected.";
3078
3079 parameters.codecs[1].SetParam("apt", GetEngineCodec("VP8").id);
3080 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3081
3082 cricket::VideoCodec rtx_codec2(kUnusedPayloadType2, "rtx");
3083 rtx_codec2.SetParam("apt", rtx_codec.id);
3084 parameters.codecs.push_back(rtx_codec2);
3085
3086 EXPECT_FALSE(channel_->SetRecvParameters(parameters)) <<
3087 "RTX codec with another RTX as associated payload type should be "
3088 "rejected.";
3089 }
3090
3091 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsWithChangedRtxPayloadType) {
3092 const int kUnusedPayloadType1 = 126;
3093 const int kUnusedPayloadType2 = 127;
3094 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType1));
3095 EXPECT_FALSE(FindCodecById(engine_.codecs(), kUnusedPayloadType2));
3096
3097 // SSRCs for RTX.
3098 cricket::StreamParams params =
3099 cricket::StreamParams::CreateLegacy(kSsrcs1[0]);
3100 params.AddFidSsrc(kSsrcs1[0], kRtxSsrcs1[0]);
3101 AddRecvStream(params);
3102
3103 // Original payload type for RTX.
3104 cricket::VideoRecvParameters parameters;
3105 parameters.codecs.push_back(GetEngineCodec("VP8"));
3106 cricket::VideoCodec rtx_codec(kUnusedPayloadType1, "rtx");
3107 rtx_codec.SetParam("apt", GetEngineCodec("VP8").id);
3108 parameters.codecs.push_back(rtx_codec);
3109 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3110 ASSERT_EQ(1U, fake_call_->GetVideoReceiveStreams().size());
3111 const webrtc::VideoReceiveStream::Config& config_before =
3112 fake_call_->GetVideoReceiveStreams()[0]->GetConfig();
3113 EXPECT_EQ(1U, config_before.rtp.rtx_payload_types.size());
3114 auto it_before =
3115 config_before.rtp.rtx_payload_types.find(GetEngineCodec("VP8").id);
3116 ASSERT_NE(it_before, config_before.rtp.rtx_payload_types.end());
3117 EXPECT_EQ(kUnusedPayloadType1, it_before->second);
3118 EXPECT_EQ(kRtxSsrcs1[0], config_before.rtp.rtx_ssrc);
3119
3120 // Change payload type for RTX.
3121 parameters.codecs[1].id = kUnusedPayloadType2;
3122 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3123 ASSERT_EQ(1U, fake_call_->GetVideoReceiveStreams().size());
3124 const webrtc::VideoReceiveStream::Config& config_after =
3125 fake_call_->GetVideoReceiveStreams()[0]->GetConfig();
3126 EXPECT_EQ(1U, config_after.rtp.rtx_payload_types.size());
3127 auto it_after =
3128 config_after.rtp.rtx_payload_types.find(GetEngineCodec("VP8").id);
3129 ASSERT_NE(it_after, config_after.rtp.rtx_payload_types.end());
3130 EXPECT_EQ(kUnusedPayloadType2, it_after->second);
3131 EXPECT_EQ(kRtxSsrcs1[0], config_after.rtp.rtx_ssrc);
3132 }
3133
3134 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsDifferentPayloadType) {
3135 cricket::VideoRecvParameters parameters;
3136 parameters.codecs.push_back(GetEngineCodec("VP8"));
3137 parameters.codecs[0].id = 99;
3138 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3139 }
3140
3141 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsAcceptDefaultCodecs) {
3142 cricket::VideoRecvParameters parameters;
3143 parameters.codecs = engine_.codecs();
3144 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3145
3146 FakeVideoReceiveStream* stream = AddRecvStream();
3147 const webrtc::VideoReceiveStream::Config& config = stream->GetConfig();
3148 EXPECT_EQ(engine_.codecs()[0].name, config.decoders[0].payload_name);
3149 EXPECT_EQ(engine_.codecs()[0].id, config.decoders[0].payload_type);
3150 }
3151
3152 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsRejectUnsupportedCodec) {
3153 cricket::VideoRecvParameters parameters;
3154 parameters.codecs.push_back(GetEngineCodec("VP8"));
3155 parameters.codecs.push_back(VideoCodec(101, "WTF3"));
3156 EXPECT_FALSE(channel_->SetRecvParameters(parameters));
3157 }
3158
3159 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsAcceptsMultipleVideoCodecs) {
3160 cricket::VideoRecvParameters parameters;
3161 parameters.codecs.push_back(GetEngineCodec("VP8"));
3162 parameters.codecs.push_back(GetEngineCodec("VP9"));
3163 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3164 }
3165
3166 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsWithoutFecDisablesFec) {
3167 cricket::VideoSendParameters send_parameters;
3168 send_parameters.codecs.push_back(GetEngineCodec("VP8"));
3169 send_parameters.codecs.push_back(GetEngineCodec("red"));
3170 send_parameters.codecs.push_back(GetEngineCodec("ulpfec"));
3171 ASSERT_TRUE(channel_->SetSendParameters(send_parameters));
3172
3173 FakeVideoReceiveStream* stream = AddRecvStream();
3174
3175 EXPECT_EQ(GetEngineCodec("ulpfec").id,
3176 stream->GetConfig().rtp.ulpfec.ulpfec_payload_type);
3177
3178 cricket::VideoRecvParameters recv_parameters;
3179 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
3180 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
3181 stream = fake_call_->GetVideoReceiveStreams()[0];
3182 ASSERT_TRUE(stream != nullptr);
3183 EXPECT_EQ(-1, stream->GetConfig().rtp.ulpfec.ulpfec_payload_type)
3184 << "SetSendCodec without ULPFEC should disable current ULPFEC.";
3185 }
3186
3187 TEST_F(WebRtcVideoChannel2FlexfecRecvTest, SetRecvParamsWithoutFecDisablesFec) {
3188 AddRecvStream(
3189 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
3190 const std::vector<FakeFlexfecReceiveStream*>& streams =
3191 fake_call_->GetFlexfecReceiveStreams();
3192
3193 ASSERT_EQ(1U, streams.size());
3194 const FakeFlexfecReceiveStream* stream = streams.front();
3195 EXPECT_EQ(GetEngineCodec("flexfec-03").id, stream->GetConfig().payload_type);
3196 EXPECT_EQ(kFlexfecSsrc, stream->GetConfig().remote_ssrc);
3197 ASSERT_EQ(1U, stream->GetConfig().protected_media_ssrcs.size());
3198 EXPECT_EQ(kSsrcs1[0], stream->GetConfig().protected_media_ssrcs[0]);
3199
3200 cricket::VideoRecvParameters recv_parameters;
3201 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
3202 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
3203 EXPECT_TRUE(streams.empty())
3204 << "SetSendCodec without FlexFEC should disable current FlexFEC.";
3205 }
3206
3207 TEST_F(WebRtcVideoChannel2Test, SetSendParamsWithFecEnablesFec) {
3208 FakeVideoReceiveStream* stream = AddRecvStream();
3209 EXPECT_EQ(GetEngineCodec("ulpfec").id,
3210 stream->GetConfig().rtp.ulpfec.ulpfec_payload_type);
3211
3212 cricket::VideoRecvParameters recv_parameters;
3213 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
3214 recv_parameters.codecs.push_back(GetEngineCodec("red"));
3215 recv_parameters.codecs.push_back(GetEngineCodec("ulpfec"));
3216 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
3217 stream = fake_call_->GetVideoReceiveStreams()[0];
3218 ASSERT_TRUE(stream != nullptr);
3219 EXPECT_EQ(GetEngineCodec("ulpfec").id,
3220 stream->GetConfig().rtp.ulpfec.ulpfec_payload_type)
3221 << "ULPFEC should be enabled on the receive stream.";
3222
3223 cricket::VideoSendParameters send_parameters;
3224 send_parameters.codecs.push_back(GetEngineCodec("VP8"));
3225 send_parameters.codecs.push_back(GetEngineCodec("red"));
3226 send_parameters.codecs.push_back(GetEngineCodec("ulpfec"));
3227 ASSERT_TRUE(channel_->SetSendParameters(send_parameters));
3228 stream = fake_call_->GetVideoReceiveStreams()[0];
3229 EXPECT_EQ(GetEngineCodec("ulpfec").id,
3230 stream->GetConfig().rtp.ulpfec.ulpfec_payload_type)
3231 << "ULPFEC should be enabled on the receive stream.";
3232 }
3233
3234 TEST_F(WebRtcVideoChannel2FlexfecSendRecvTest,
3235 SetSendRecvParamsWithFecEnablesFec) {
3236 AddRecvStream(
3237 CreatePrimaryWithFecFrStreamParams("cname", kSsrcs1[0], kFlexfecSsrc));
3238 const std::vector<FakeFlexfecReceiveStream*>& streams =
3239 fake_call_->GetFlexfecReceiveStreams();
3240
3241 cricket::VideoRecvParameters recv_parameters;
3242 recv_parameters.codecs.push_back(GetEngineCodec("VP8"));
3243 recv_parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
3244 ASSERT_TRUE(channel_->SetRecvParameters(recv_parameters));
3245 ASSERT_EQ(1U, streams.size());
3246 const FakeFlexfecReceiveStream* stream_with_recv_params = streams.front();
3247 EXPECT_EQ(GetEngineCodec("flexfec-03").id,
3248 stream_with_recv_params->GetConfig().payload_type);
3249 EXPECT_EQ(kFlexfecSsrc, stream_with_recv_params->GetConfig().remote_ssrc);
3250 EXPECT_EQ(1U,
3251 stream_with_recv_params->GetConfig().protected_media_ssrcs.size());
3252 EXPECT_EQ(kSsrcs1[0],
3253 stream_with_recv_params->GetConfig().protected_media_ssrcs[0]);
3254
3255 cricket::VideoSendParameters send_parameters;
3256 send_parameters.codecs.push_back(GetEngineCodec("VP8"));
3257 send_parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
3258 ASSERT_TRUE(channel_->SetSendParameters(send_parameters));
3259 ASSERT_EQ(1U, streams.size());
3260 const FakeFlexfecReceiveStream* stream_with_send_params = streams.front();
3261 EXPECT_EQ(GetEngineCodec("flexfec-03").id,
3262 stream_with_send_params->GetConfig().payload_type);
3263 EXPECT_EQ(kFlexfecSsrc, stream_with_send_params->GetConfig().remote_ssrc);
3264 EXPECT_EQ(1U,
3265 stream_with_send_params->GetConfig().protected_media_ssrcs.size());
3266 EXPECT_EQ(kSsrcs1[0],
3267 stream_with_send_params->GetConfig().protected_media_ssrcs[0]);
3268 }
3269
3270 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsRejectDuplicateFecPayloads) {
3271 cricket::VideoRecvParameters parameters;
3272 parameters.codecs.push_back(GetEngineCodec("VP8"));
3273 parameters.codecs.push_back(GetEngineCodec("red"));
3274 parameters.codecs[1].id = parameters.codecs[0].id;
3275 EXPECT_FALSE(channel_->SetRecvParameters(parameters));
3276 }
3277
3278 TEST_F(WebRtcVideoChannel2FlexfecRecvTest,
3279 SetRecvCodecsRejectDuplicateFecPayloads) {
3280 cricket::VideoRecvParameters parameters;
3281 parameters.codecs.push_back(GetEngineCodec("VP8"));
3282 parameters.codecs.push_back(GetEngineCodec("flexfec-03"));
3283 parameters.codecs[1].id = parameters.codecs[0].id;
3284 EXPECT_FALSE(channel_->SetRecvParameters(parameters));
3285 }
3286
3287 TEST_F(WebRtcVideoChannel2Test, SetRecvCodecsRejectDuplicateCodecPayloads) {
3288 cricket::VideoRecvParameters parameters;
3289 parameters.codecs.push_back(GetEngineCodec("VP8"));
3290 parameters.codecs.push_back(GetEngineCodec("VP9"));
3291 parameters.codecs[1].id = parameters.codecs[0].id;
3292 EXPECT_FALSE(channel_->SetRecvParameters(parameters));
3293 }
3294
3295 TEST_F(WebRtcVideoChannel2Test,
3296 SetRecvCodecsAcceptSameCodecOnMultiplePayloadTypes) {
3297 cricket::VideoRecvParameters parameters;
3298 parameters.codecs.push_back(GetEngineCodec("VP8"));
3299 parameters.codecs.push_back(GetEngineCodec("VP8"));
3300 parameters.codecs[1].id += 1;
3301 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3302 }
3303
3304 // Test that setting the same codecs but with a different order
3305 // doesn't result in the stream being recreated.
3306 TEST_F(WebRtcVideoChannel2Test,
3307 SetRecvCodecsDifferentOrderDoesntRecreateStream) {
3308 cricket::VideoRecvParameters parameters1;
3309 parameters1.codecs.push_back(GetEngineCodec("VP8"));
3310 parameters1.codecs.push_back(GetEngineCodec("red"));
3311 EXPECT_TRUE(channel_->SetRecvParameters(parameters1));
3312
3313 AddRecvStream(cricket::StreamParams::CreateLegacy(123));
3314 EXPECT_EQ(1, fake_call_->GetNumCreatedReceiveStreams());
3315
3316 cricket::VideoRecvParameters parameters2;
3317 parameters2.codecs.push_back(GetEngineCodec("red"));
3318 parameters2.codecs.push_back(GetEngineCodec("VP8"));
3319 EXPECT_TRUE(channel_->SetRecvParameters(parameters2));
3320 EXPECT_EQ(1, fake_call_->GetNumCreatedReceiveStreams());
3321 }
3322
3323 TEST_F(WebRtcVideoChannel2Test, SendStreamNotSendingByDefault) {
3324 EXPECT_FALSE(AddSendStream()->IsSending());
3325 }
3326
3327 TEST_F(WebRtcVideoChannel2Test, ReceiveStreamReceivingByDefault) {
3328 EXPECT_TRUE(AddRecvStream()->IsReceiving());
3329 }
3330
3331 TEST_F(WebRtcVideoChannel2Test, SetSend) {
3332 FakeVideoSendStream* stream = AddSendStream();
3333 EXPECT_FALSE(stream->IsSending());
3334
3335 // false->true
3336 EXPECT_TRUE(channel_->SetSend(true));
3337 EXPECT_TRUE(stream->IsSending());
3338 // true->true
3339 EXPECT_TRUE(channel_->SetSend(true));
3340 EXPECT_TRUE(stream->IsSending());
3341 // true->false
3342 EXPECT_TRUE(channel_->SetSend(false));
3343 EXPECT_FALSE(stream->IsSending());
3344 // false->false
3345 EXPECT_TRUE(channel_->SetSend(false));
3346 EXPECT_FALSE(stream->IsSending());
3347
3348 EXPECT_TRUE(channel_->SetSend(true));
3349 FakeVideoSendStream* new_stream = AddSendStream();
3350 EXPECT_TRUE(new_stream->IsSending())
3351 << "Send stream created after SetSend(true) not sending initially.";
3352 }
3353
3354 // This test verifies DSCP settings are properly applied on video media channel.
3355 TEST_F(WebRtcVideoChannel2Test, TestSetDscpOptions) {
3356 std::unique_ptr<cricket::FakeNetworkInterface> network_interface(
3357 new cricket::FakeNetworkInterface);
3358 MediaConfig config;
3359 std::unique_ptr<VideoMediaChannel> channel;
3360
3361 channel.reset(engine_.CreateChannel(call_.get(), config, VideoOptions()));
3362 channel->SetInterface(network_interface.get());
3363 // Default value when DSCP is disabled should be DSCP_DEFAULT.
3364 EXPECT_EQ(rtc::DSCP_DEFAULT, network_interface->dscp());
3365
3366 config.enable_dscp = true;
3367 channel.reset(engine_.CreateChannel(call_.get(), config, VideoOptions()));
3368 channel->SetInterface(network_interface.get());
3369 EXPECT_EQ(rtc::DSCP_AF41, network_interface->dscp());
3370
3371 // Verify that setting the option to false resets the
3372 // DiffServCodePoint.
3373 config.enable_dscp = false;
3374 channel.reset(engine_.CreateChannel(call_.get(), config, VideoOptions()));
3375 channel->SetInterface(network_interface.get());
3376 EXPECT_EQ(rtc::DSCP_DEFAULT, network_interface->dscp());
3377 }
3378
3379 // This test verifies that the RTCP reduced size mode is properly applied to
3380 // send video streams.
3381 TEST_F(WebRtcVideoChannel2Test, TestSetSendRtcpReducedSize) {
3382 // Create stream, expecting that default mode is "compound".
3383 FakeVideoSendStream* stream1 = AddSendStream();
3384 EXPECT_EQ(webrtc::RtcpMode::kCompound, stream1->GetConfig().rtp.rtcp_mode);
3385
3386 // Now enable reduced size mode.
3387 send_parameters_.rtcp.reduced_size = true;
3388 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3389 stream1 = fake_call_->GetVideoSendStreams()[0];
3390 EXPECT_EQ(webrtc::RtcpMode::kReducedSize, stream1->GetConfig().rtp.rtcp_mode);
3391
3392 // Create a new stream and ensure it picks up the reduced size mode.
3393 FakeVideoSendStream* stream2 = AddSendStream();
3394 EXPECT_EQ(webrtc::RtcpMode::kReducedSize, stream2->GetConfig().rtp.rtcp_mode);
3395 }
3396
3397 // This test verifies that the RTCP reduced size mode is properly applied to
3398 // receive video streams.
3399 TEST_F(WebRtcVideoChannel2Test, TestSetRecvRtcpReducedSize) {
3400 // Create stream, expecting that default mode is "compound".
3401 FakeVideoReceiveStream* stream1 = AddRecvStream();
3402 EXPECT_EQ(webrtc::RtcpMode::kCompound, stream1->GetConfig().rtp.rtcp_mode);
3403
3404 // Now enable reduced size mode.
3405 // TODO(deadbeef): Once "recv_parameters" becomes "receiver_parameters",
3406 // the reduced_size flag should come from that.
3407 send_parameters_.rtcp.reduced_size = true;
3408 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3409 stream1 = fake_call_->GetVideoReceiveStreams()[0];
3410 EXPECT_EQ(webrtc::RtcpMode::kReducedSize, stream1->GetConfig().rtp.rtcp_mode);
3411
3412 // Create a new stream and ensure it picks up the reduced size mode.
3413 FakeVideoReceiveStream* stream2 = AddRecvStream();
3414 EXPECT_EQ(webrtc::RtcpMode::kReducedSize, stream2->GetConfig().rtp.rtcp_mode);
3415 }
3416
3417 TEST_F(WebRtcVideoChannel2Test, OnReadyToSendSignalsNetworkState) {
3418 EXPECT_EQ(webrtc::kNetworkUp,
3419 fake_call_->GetNetworkState(webrtc::MediaType::VIDEO));
3420 EXPECT_EQ(webrtc::kNetworkUp,
3421 fake_call_->GetNetworkState(webrtc::MediaType::AUDIO));
3422
3423 channel_->OnReadyToSend(false);
3424 EXPECT_EQ(webrtc::kNetworkDown,
3425 fake_call_->GetNetworkState(webrtc::MediaType::VIDEO));
3426 EXPECT_EQ(webrtc::kNetworkUp,
3427 fake_call_->GetNetworkState(webrtc::MediaType::AUDIO));
3428
3429 channel_->OnReadyToSend(true);
3430 EXPECT_EQ(webrtc::kNetworkUp,
3431 fake_call_->GetNetworkState(webrtc::MediaType::VIDEO));
3432 EXPECT_EQ(webrtc::kNetworkUp,
3433 fake_call_->GetNetworkState(webrtc::MediaType::AUDIO));
3434 }
3435
3436 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsSentCodecName) {
3437 cricket::VideoSendParameters parameters;
3438 parameters.codecs.push_back(GetEngineCodec("VP8"));
3439 EXPECT_TRUE(channel_->SetSendParameters(parameters));
3440
3441 AddSendStream();
3442
3443 cricket::VideoMediaInfo info;
3444 ASSERT_TRUE(channel_->GetStats(&info));
3445 EXPECT_EQ("VP8", info.senders[0].codec_name);
3446 }
3447
3448 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsEncoderImplementationName) {
3449 FakeVideoSendStream* stream = AddSendStream();
3450 webrtc::VideoSendStream::Stats stats;
3451 stats.encoder_implementation_name = "encoder_implementation_name";
3452 stream->SetStats(stats);
3453
3454 cricket::VideoMediaInfo info;
3455 ASSERT_TRUE(channel_->GetStats(&info));
3456 EXPECT_EQ(stats.encoder_implementation_name,
3457 info.senders[0].encoder_implementation_name);
3458 }
3459
3460 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsCpuOveruseMetrics) {
3461 FakeVideoSendStream* stream = AddSendStream();
3462 webrtc::VideoSendStream::Stats stats;
3463 stats.avg_encode_time_ms = 13;
3464 stats.encode_usage_percent = 42;
3465 stream->SetStats(stats);
3466
3467 cricket::VideoMediaInfo info;
3468 ASSERT_TRUE(channel_->GetStats(&info));
3469 EXPECT_EQ(stats.avg_encode_time_ms, info.senders[0].avg_encode_ms);
3470 EXPECT_EQ(stats.encode_usage_percent, info.senders[0].encode_usage_percent);
3471 }
3472
3473 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsFramesEncoded) {
3474 FakeVideoSendStream* stream = AddSendStream();
3475 webrtc::VideoSendStream::Stats stats;
3476 stats.frames_encoded = 13;
3477 stream->SetStats(stats);
3478
3479 cricket::VideoMediaInfo info;
3480 ASSERT_TRUE(channel_->GetStats(&info));
3481 EXPECT_EQ(stats.frames_encoded, info.senders[0].frames_encoded);
3482 }
3483
3484 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsQpSum) {
3485 FakeVideoSendStream* stream = AddSendStream();
3486 webrtc::VideoSendStream::Stats stats;
3487 stats.qp_sum = rtc::Optional<uint64_t>(13);
3488 stream->SetStats(stats);
3489
3490 cricket::VideoMediaInfo info;
3491 ASSERT_TRUE(channel_->GetStats(&info));
3492 EXPECT_EQ(stats.qp_sum, info.senders[0].qp_sum);
3493 }
3494
3495 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsUpperResolution) {
3496 FakeVideoSendStream* stream = AddSendStream();
3497 webrtc::VideoSendStream::Stats stats;
3498 stats.substreams[17].width = 123;
3499 stats.substreams[17].height = 40;
3500 stats.substreams[42].width = 80;
3501 stats.substreams[42].height = 31;
3502 stats.substreams[11].width = 20;
3503 stats.substreams[11].height = 90;
3504 stream->SetStats(stats);
3505
3506 cricket::VideoMediaInfo info;
3507 ASSERT_TRUE(channel_->GetStats(&info));
3508 ASSERT_EQ(1u, info.senders.size());
3509 EXPECT_EQ(123, info.senders[0].send_frame_width);
3510 EXPECT_EQ(90, info.senders[0].send_frame_height);
3511 }
3512
3513 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsPreferredBitrate) {
3514 FakeVideoSendStream* stream = AddSendStream();
3515 webrtc::VideoSendStream::Stats stats;
3516 stats.preferred_media_bitrate_bps = 5;
3517 stream->SetStats(stats);
3518
3519 cricket::VideoMediaInfo info;
3520 ASSERT_TRUE(channel_->GetStats(&info));
3521 ASSERT_EQ(1u, info.senders.size());
3522 EXPECT_EQ(5, info.senders[0].preferred_bitrate);
3523 }
3524
3525 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsCpuAdaptationStats) {
3526 FakeVideoSendStream* stream = AddSendStream();
3527 webrtc::VideoSendStream::Stats stats;
3528 stats.number_of_cpu_adapt_changes = 2;
3529 stats.cpu_limited_resolution = true;
3530 stream->SetStats(stats);
3531
3532 cricket::VideoMediaInfo info;
3533 EXPECT_TRUE(channel_->GetStats(&info));
3534 ASSERT_EQ(1U, info.senders.size());
3535 EXPECT_EQ(WebRtcVideoChannel2::ADAPTREASON_CPU, info.senders[0].adapt_reason);
3536 EXPECT_EQ(stats.number_of_cpu_adapt_changes, info.senders[0].adapt_changes);
3537 }
3538
3539 TEST_F(WebRtcVideoChannel2Test, GetStatsReportsAdaptationAndBandwidthStats) {
3540 FakeVideoSendStream* stream = AddSendStream();
3541 webrtc::VideoSendStream::Stats stats;
3542 stats.number_of_cpu_adapt_changes = 2;
3543 stats.cpu_limited_resolution = true;
3544 stats.bw_limited_resolution = true;
3545 stream->SetStats(stats);
3546
3547 cricket::VideoMediaInfo info;
3548 EXPECT_TRUE(channel_->GetStats(&info));
3549 ASSERT_EQ(1U, info.senders.size());
3550 EXPECT_EQ(WebRtcVideoChannel2::ADAPTREASON_CPU |
3551 WebRtcVideoChannel2::ADAPTREASON_BANDWIDTH,
3552 info.senders[0].adapt_reason);
3553 EXPECT_EQ(stats.number_of_cpu_adapt_changes, info.senders[0].adapt_changes);
3554 }
3555
3556 TEST_F(WebRtcVideoChannel2Test,
3557 GetStatsTranslatesBandwidthLimitedResolutionCorrectly) {
3558 FakeVideoSendStream* stream = AddSendStream();
3559 webrtc::VideoSendStream::Stats stats;
3560 stats.bw_limited_resolution = true;
3561 stream->SetStats(stats);
3562
3563 cricket::VideoMediaInfo info;
3564 EXPECT_TRUE(channel_->GetStats(&info));
3565 ASSERT_EQ(1U, info.senders.size());
3566 EXPECT_EQ(WebRtcVideoChannel2::ADAPTREASON_BANDWIDTH,
3567 info.senders[0].adapt_reason);
3568 }
3569
3570 TEST_F(WebRtcVideoChannel2Test,
3571 GetStatsTranslatesSendRtcpPacketTypesCorrectly) {
3572 FakeVideoSendStream* stream = AddSendStream();
3573 webrtc::VideoSendStream::Stats stats;
3574 stats.substreams[17].rtcp_packet_type_counts.fir_packets = 2;
3575 stats.substreams[17].rtcp_packet_type_counts.nack_packets = 3;
3576 stats.substreams[17].rtcp_packet_type_counts.pli_packets = 4;
3577
3578 stats.substreams[42].rtcp_packet_type_counts.fir_packets = 5;
3579 stats.substreams[42].rtcp_packet_type_counts.nack_packets = 7;
3580 stats.substreams[42].rtcp_packet_type_counts.pli_packets = 9;
3581
3582 stream->SetStats(stats);
3583
3584 cricket::VideoMediaInfo info;
3585 ASSERT_TRUE(channel_->GetStats(&info));
3586 EXPECT_EQ(7, info.senders[0].firs_rcvd);
3587 EXPECT_EQ(10, info.senders[0].nacks_rcvd);
3588 EXPECT_EQ(13, info.senders[0].plis_rcvd);
3589 }
3590
3591 TEST_F(WebRtcVideoChannel2Test,
3592 GetStatsTranslatesReceiveRtcpPacketTypesCorrectly) {
3593 FakeVideoReceiveStream* stream = AddRecvStream();
3594 webrtc::VideoReceiveStream::Stats stats;
3595 stats.rtcp_packet_type_counts.fir_packets = 2;
3596 stats.rtcp_packet_type_counts.nack_packets = 3;
3597 stats.rtcp_packet_type_counts.pli_packets = 4;
3598 stream->SetStats(stats);
3599
3600 cricket::VideoMediaInfo info;
3601 ASSERT_TRUE(channel_->GetStats(&info));
3602 EXPECT_EQ(stats.rtcp_packet_type_counts.fir_packets,
3603 info.receivers[0].firs_sent);
3604 EXPECT_EQ(stats.rtcp_packet_type_counts.nack_packets,
3605 info.receivers[0].nacks_sent);
3606 EXPECT_EQ(stats.rtcp_packet_type_counts.pli_packets,
3607 info.receivers[0].plis_sent);
3608 }
3609
3610 TEST_F(WebRtcVideoChannel2Test, GetStatsTranslatesDecodeStatsCorrectly) {
3611 FakeVideoReceiveStream* stream = AddRecvStream();
3612 webrtc::VideoReceiveStream::Stats stats;
3613 stats.decoder_implementation_name = "decoder_implementation_name";
3614 stats.decode_ms = 2;
3615 stats.max_decode_ms = 3;
3616 stats.current_delay_ms = 4;
3617 stats.target_delay_ms = 5;
3618 stats.jitter_buffer_ms = 6;
3619 stats.min_playout_delay_ms = 7;
3620 stats.render_delay_ms = 8;
3621 stats.width = 9;
3622 stats.height = 10;
3623 stats.frame_counts.key_frames = 11;
3624 stats.frame_counts.delta_frames = 12;
3625 stats.frames_rendered = 13;
3626 stats.frames_decoded = 14;
3627 stats.qp_sum = rtc::Optional<uint64_t>(15);
3628 stream->SetStats(stats);
3629
3630 cricket::VideoMediaInfo info;
3631 ASSERT_TRUE(channel_->GetStats(&info));
3632 EXPECT_EQ(stats.decoder_implementation_name,
3633 info.receivers[0].decoder_implementation_name);
3634 EXPECT_EQ(stats.decode_ms, info.receivers[0].decode_ms);
3635 EXPECT_EQ(stats.max_decode_ms, info.receivers[0].max_decode_ms);
3636 EXPECT_EQ(stats.current_delay_ms, info.receivers[0].current_delay_ms);
3637 EXPECT_EQ(stats.target_delay_ms, info.receivers[0].target_delay_ms);
3638 EXPECT_EQ(stats.jitter_buffer_ms, info.receivers[0].jitter_buffer_ms);
3639 EXPECT_EQ(stats.min_playout_delay_ms, info.receivers[0].min_playout_delay_ms);
3640 EXPECT_EQ(stats.render_delay_ms, info.receivers[0].render_delay_ms);
3641 EXPECT_EQ(stats.width, info.receivers[0].frame_width);
3642 EXPECT_EQ(stats.height, info.receivers[0].frame_height);
3643 EXPECT_EQ(stats.frame_counts.key_frames + stats.frame_counts.delta_frames,
3644 info.receivers[0].frames_received);
3645 EXPECT_EQ(stats.frames_rendered, info.receivers[0].frames_rendered);
3646 EXPECT_EQ(stats.frames_decoded, info.receivers[0].frames_decoded);
3647 EXPECT_EQ(stats.qp_sum, info.receivers[0].qp_sum);
3648 }
3649
3650 TEST_F(WebRtcVideoChannel2Test, GetStatsTranslatesReceivePacketStatsCorrectly) {
3651 FakeVideoReceiveStream* stream = AddRecvStream();
3652 webrtc::VideoReceiveStream::Stats stats;
3653 stats.rtp_stats.transmitted.payload_bytes = 2;
3654 stats.rtp_stats.transmitted.header_bytes = 3;
3655 stats.rtp_stats.transmitted.padding_bytes = 4;
3656 stats.rtp_stats.transmitted.packets = 5;
3657 stats.rtcp_stats.cumulative_lost = 6;
3658 stats.rtcp_stats.fraction_lost = 7;
3659 stream->SetStats(stats);
3660
3661 cricket::VideoMediaInfo info;
3662 ASSERT_TRUE(channel_->GetStats(&info));
3663 EXPECT_EQ(stats.rtp_stats.transmitted.payload_bytes +
3664 stats.rtp_stats.transmitted.header_bytes +
3665 stats.rtp_stats.transmitted.padding_bytes,
3666 info.receivers[0].bytes_rcvd);
3667 EXPECT_EQ(stats.rtp_stats.transmitted.packets,
3668 info.receivers[0].packets_rcvd);
3669 EXPECT_EQ(stats.rtcp_stats.cumulative_lost, info.receivers[0].packets_lost);
3670 EXPECT_EQ(static_cast<float>(stats.rtcp_stats.fraction_lost) / (1 << 8),
3671 info.receivers[0].fraction_lost);
3672 }
3673
3674 TEST_F(WebRtcVideoChannel2Test, TranslatesCallStatsCorrectly) {
3675 AddSendStream();
3676 AddSendStream();
3677 webrtc::Call::Stats stats;
3678 stats.rtt_ms = 123;
3679 fake_call_->SetStats(stats);
3680
3681 cricket::VideoMediaInfo info;
3682 ASSERT_TRUE(channel_->GetStats(&info));
3683 ASSERT_EQ(2u, info.senders.size());
3684 EXPECT_EQ(stats.rtt_ms, info.senders[0].rtt_ms);
3685 EXPECT_EQ(stats.rtt_ms, info.senders[1].rtt_ms);
3686 }
3687
3688 TEST_F(WebRtcVideoChannel2Test, TranslatesSenderBitrateStatsCorrectly) {
3689 FakeVideoSendStream* stream = AddSendStream();
3690 webrtc::VideoSendStream::Stats stats;
3691 stats.target_media_bitrate_bps = 156;
3692 stats.media_bitrate_bps = 123;
3693 stats.substreams[17].total_bitrate_bps = 1;
3694 stats.substreams[17].retransmit_bitrate_bps = 2;
3695 stats.substreams[42].total_bitrate_bps = 3;
3696 stats.substreams[42].retransmit_bitrate_bps = 4;
3697 stream->SetStats(stats);
3698
3699 FakeVideoSendStream* stream2 = AddSendStream();
3700 webrtc::VideoSendStream::Stats stats2;
3701 stats2.target_media_bitrate_bps = 200;
3702 stats2.media_bitrate_bps = 321;
3703 stats2.substreams[13].total_bitrate_bps = 5;
3704 stats2.substreams[13].retransmit_bitrate_bps = 6;
3705 stats2.substreams[21].total_bitrate_bps = 7;
3706 stats2.substreams[21].retransmit_bitrate_bps = 8;
3707 stream2->SetStats(stats2);
3708
3709 cricket::VideoMediaInfo info;
3710 ASSERT_TRUE(channel_->GetStats(&info));
3711 ASSERT_EQ(2u, info.senders.size());
3712 BandwidthEstimationInfo bwe_info;
3713 channel_->FillBitrateInfo(&bwe_info);
3714 // Assuming stream and stream2 corresponds to senders[0] and [1] respectively
3715 // is OK as std::maps are sorted and AddSendStream() gives increasing SSRCs.
3716 EXPECT_EQ(stats.media_bitrate_bps, info.senders[0].nominal_bitrate);
3717 EXPECT_EQ(stats2.media_bitrate_bps, info.senders[1].nominal_bitrate);
3718 EXPECT_EQ(stats.target_media_bitrate_bps + stats2.target_media_bitrate_bps,
3719 bwe_info.target_enc_bitrate);
3720 EXPECT_EQ(stats.media_bitrate_bps + stats2.media_bitrate_bps,
3721 bwe_info.actual_enc_bitrate);
3722 EXPECT_EQ(1 + 3 + 5 + 7, bwe_info.transmit_bitrate)
3723 << "Bandwidth stats should take all streams into account.";
3724 EXPECT_EQ(2 + 4 + 6 + 8, bwe_info.retransmit_bitrate)
3725 << "Bandwidth stats should take all streams into account.";
3726 }
3727
3728 TEST_F(WebRtcVideoChannel2Test, DefaultReceiveStreamReconfiguresToUseRtx) {
3729 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3730
3731 const std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs1);
3732 const std::vector<uint32_t> rtx_ssrcs = MAKE_VECTOR(kRtxSsrcs1);
3733
3734 ASSERT_EQ(0u, fake_call_->GetVideoReceiveStreams().size());
3735 const size_t kDataLength = 12;
3736 uint8_t data[kDataLength];
3737 memset(data, 0, sizeof(data));
3738 rtc::SetBE32(&data[8], ssrcs[0]);
3739 rtc::CopyOnWriteBuffer packet(data, kDataLength);
3740 rtc::PacketTime packet_time;
3741 channel_->OnPacketReceived(&packet, packet_time);
3742
3743 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size())
3744 << "No default receive stream created.";
3745 FakeVideoReceiveStream* recv_stream = fake_call_->GetVideoReceiveStreams()[0];
3746 EXPECT_EQ(0u, recv_stream->GetConfig().rtp.rtx_ssrc)
3747 << "Default receive stream should not have configured RTX";
3748
3749 EXPECT_TRUE(channel_->AddRecvStream(
3750 cricket::CreateSimWithRtxStreamParams("cname", ssrcs, rtx_ssrcs)));
3751 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size())
3752 << "AddRecvStream should have reconfigured, not added a new receiver.";
3753 recv_stream = fake_call_->GetVideoReceiveStreams()[0];
3754 EXPECT_FALSE(recv_stream->GetConfig().rtp.rtx_payload_types.empty());
3755 EXPECT_EQ(recv_stream->GetConfig().decoders.size(),
3756 recv_stream->GetConfig().rtp.rtx_payload_types.size())
3757 << "RTX should be mapped for all decoders/payload types.";
3758 EXPECT_EQ(rtx_ssrcs[0], recv_stream->GetConfig().rtp.rtx_ssrc);
3759 }
3760
3761 TEST_F(WebRtcVideoChannel2Test, RejectsAddingStreamsWithMissingSsrcsForRtx) {
3762 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3763
3764 const std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs1);
3765 const std::vector<uint32_t> rtx_ssrcs = MAKE_VECTOR(kRtxSsrcs1);
3766
3767 StreamParams sp =
3768 cricket::CreateSimWithRtxStreamParams("cname", ssrcs, rtx_ssrcs);
3769 sp.ssrcs = ssrcs; // Without RTXs, this is the important part.
3770
3771 EXPECT_FALSE(channel_->AddSendStream(sp));
3772 EXPECT_FALSE(channel_->AddRecvStream(sp));
3773 }
3774
3775 TEST_F(WebRtcVideoChannel2Test, RejectsAddingStreamsWithOverlappingRtxSsrcs) {
3776 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3777
3778 const std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs1);
3779 const std::vector<uint32_t> rtx_ssrcs = MAKE_VECTOR(kRtxSsrcs1);
3780
3781 StreamParams sp =
3782 cricket::CreateSimWithRtxStreamParams("cname", ssrcs, rtx_ssrcs);
3783
3784 EXPECT_TRUE(channel_->AddSendStream(sp));
3785 EXPECT_TRUE(channel_->AddRecvStream(sp));
3786
3787 // The RTX SSRC is already used in previous streams, using it should fail.
3788 sp = cricket::StreamParams::CreateLegacy(rtx_ssrcs[0]);
3789 EXPECT_FALSE(channel_->AddSendStream(sp));
3790 EXPECT_FALSE(channel_->AddRecvStream(sp));
3791
3792 // After removing the original stream this should be fine to add (makes sure
3793 // that RTX ssrcs are not forever taken).
3794 EXPECT_TRUE(channel_->RemoveSendStream(ssrcs[0]));
3795 EXPECT_TRUE(channel_->RemoveRecvStream(ssrcs[0]));
3796 EXPECT_TRUE(channel_->AddSendStream(sp));
3797 EXPECT_TRUE(channel_->AddRecvStream(sp));
3798 }
3799
3800 TEST_F(WebRtcVideoChannel2Test,
3801 RejectsAddingStreamsWithOverlappingSimulcastSsrcs) {
3802 static const uint32_t kFirstStreamSsrcs[] = {1, 2, 3};
3803 static const uint32_t kOverlappingStreamSsrcs[] = {4, 3, 5};
3804 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3805
3806 StreamParams sp =
3807 cricket::CreateSimStreamParams("cname", MAKE_VECTOR(kFirstStreamSsrcs));
3808
3809 EXPECT_TRUE(channel_->AddSendStream(sp));
3810 EXPECT_TRUE(channel_->AddRecvStream(sp));
3811
3812 // One of the SSRCs is already used in previous streams, using it should fail.
3813 sp = cricket::CreateSimStreamParams("cname",
3814 MAKE_VECTOR(kOverlappingStreamSsrcs));
3815 EXPECT_FALSE(channel_->AddSendStream(sp));
3816 EXPECT_FALSE(channel_->AddRecvStream(sp));
3817
3818 // After removing the original stream this should be fine to add (makes sure
3819 // that RTX ssrcs are not forever taken).
3820 EXPECT_TRUE(channel_->RemoveSendStream(kFirstStreamSsrcs[0]));
3821 EXPECT_TRUE(channel_->RemoveRecvStream(kFirstStreamSsrcs[0]));
3822 EXPECT_TRUE(channel_->AddSendStream(sp));
3823 EXPECT_TRUE(channel_->AddRecvStream(sp));
3824 }
3825
3826 TEST_F(WebRtcVideoChannel2Test, ReportsSsrcGroupsInStats) {
3827 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3828
3829 static const uint32_t kSenderSsrcs[] = {4, 7, 10};
3830 static const uint32_t kSenderRtxSsrcs[] = {5, 8, 11};
3831
3832 StreamParams sender_sp = cricket::CreateSimWithRtxStreamParams(
3833 "cname", MAKE_VECTOR(kSenderSsrcs), MAKE_VECTOR(kSenderRtxSsrcs));
3834
3835 EXPECT_TRUE(channel_->AddSendStream(sender_sp));
3836
3837 static const uint32_t kReceiverSsrcs[] = {3};
3838 static const uint32_t kReceiverRtxSsrcs[] = {2};
3839
3840 StreamParams receiver_sp = cricket::CreateSimWithRtxStreamParams(
3841 "cname", MAKE_VECTOR(kReceiverSsrcs), MAKE_VECTOR(kReceiverRtxSsrcs));
3842 EXPECT_TRUE(channel_->AddRecvStream(receiver_sp));
3843
3844 cricket::VideoMediaInfo info;
3845 ASSERT_TRUE(channel_->GetStats(&info));
3846
3847 ASSERT_EQ(1u, info.senders.size());
3848 ASSERT_EQ(1u, info.receivers.size());
3849
3850 EXPECT_NE(sender_sp.ssrc_groups, receiver_sp.ssrc_groups);
3851 EXPECT_EQ(sender_sp.ssrc_groups, info.senders[0].ssrc_groups);
3852 EXPECT_EQ(receiver_sp.ssrc_groups, info.receivers[0].ssrc_groups);
3853 }
3854
3855 TEST_F(WebRtcVideoChannel2Test, MapsReceivedPayloadTypeToCodecName) {
3856 FakeVideoReceiveStream* stream = AddRecvStream();
3857 webrtc::VideoReceiveStream::Stats stats;
3858 cricket::VideoMediaInfo info;
3859
3860 // Report no codec name before receiving.
3861 stream->SetStats(stats);
3862 ASSERT_TRUE(channel_->GetStats(&info));
3863 EXPECT_STREQ("", info.receivers[0].codec_name.c_str());
3864
3865 // Report VP8 if we're receiving it.
3866 stats.current_payload_type = GetEngineCodec("VP8").id;
3867 stream->SetStats(stats);
3868 ASSERT_TRUE(channel_->GetStats(&info));
3869 EXPECT_STREQ(kVp8CodecName, info.receivers[0].codec_name.c_str());
3870
3871 // Report no codec name for unknown playload types.
3872 stats.current_payload_type = 3;
3873 stream->SetStats(stats);
3874 ASSERT_TRUE(channel_->GetStats(&info));
3875 EXPECT_STREQ("", info.receivers[0].codec_name.c_str());
3876 }
3877
3878 void WebRtcVideoChannel2Test::TestReceiveUnsignaledSsrcPacket(
3879 uint8_t payload_type,
3880 bool expect_created_receive_stream) {
3881 // kRedRtxPayloadType must currently be unused.
3882 EXPECT_FALSE(FindCodecById(engine_.codecs(), kRedRtxPayloadType));
3883
3884 // Add a RED RTX codec.
3885 VideoCodec red_rtx_codec =
3886 VideoCodec::CreateRtxCodec(kRedRtxPayloadType, GetEngineCodec("red").id);
3887 recv_parameters_.codecs.push_back(red_rtx_codec);
3888 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
3889
3890 ASSERT_EQ(0u, fake_call_->GetVideoReceiveStreams().size());
3891 const size_t kDataLength = 12;
3892 uint8_t data[kDataLength];
3893 memset(data, 0, sizeof(data));
3894
3895 rtc::Set8(data, 1, payload_type);
3896 rtc::SetBE32(&data[8], kIncomingUnsignalledSsrc);
3897 rtc::CopyOnWriteBuffer packet(data, kDataLength);
3898 rtc::PacketTime packet_time;
3899 channel_->OnPacketReceived(&packet, packet_time);
3900
3901 if (expect_created_receive_stream) {
3902 EXPECT_EQ(1u, fake_call_->GetVideoReceiveStreams().size())
3903 << "Should have created a receive stream for payload type: "
3904 << payload_type;
3905 } else {
3906 EXPECT_EQ(0u, fake_call_->GetVideoReceiveStreams().size())
3907 << "Shouldn't have created a receive stream for payload type: "
3908 << payload_type;
3909 }
3910 }
3911
3912 TEST_F(WebRtcVideoChannel2Test, Vp8PacketCreatesUnsignalledStream) {
3913 TestReceiveUnsignaledSsrcPacket(GetEngineCodec("VP8").id,
3914 true /* expect_created_receive_stream */);
3915 }
3916
3917 TEST_F(WebRtcVideoChannel2Test, Vp9PacketCreatesUnsignalledStream) {
3918 TestReceiveUnsignaledSsrcPacket(GetEngineCodec("VP9").id,
3919 true /* expect_created_receive_stream */);
3920 }
3921
3922 TEST_F(WebRtcVideoChannel2Test, RtxPacketDoesntCreateUnsignalledStream) {
3923 const cricket::VideoCodec vp8 = GetEngineCodec("VP8");
3924 const int rtx_vp8_payload_type = default_apt_rtx_types_[vp8.id];
3925 TestReceiveUnsignaledSsrcPacket(rtx_vp8_payload_type,
3926 false /* expect_created_receive_stream */);
3927 }
3928
3929 TEST_F(WebRtcVideoChannel2Test, UlpfecPacketDoesntCreateUnsignalledStream) {
3930 TestReceiveUnsignaledSsrcPacket(GetEngineCodec("ulpfec").id,
3931 false /* expect_created_receive_stream */);
3932 }
3933
3934 TEST_F(WebRtcVideoChannel2FlexfecRecvTest,
3935 FlexfecPacketDoesntCreateUnsignalledStream) {
3936 TestReceiveUnsignaledSsrcPacket(GetEngineCodec("flexfec-03").id,
3937 false /* expect_created_receive_stream */);
3938 }
3939
3940 TEST_F(WebRtcVideoChannel2Test, RedRtxPacketDoesntCreateUnsignalledStream) {
3941 TestReceiveUnsignaledSsrcPacket(kRedRtxPayloadType,
3942 false /* expect_created_receive_stream */);
3943 }
3944
3945 // Test that receiving any unsignalled SSRC works even if it changes.
3946 // The first unsignalled SSRC received will create a default receive stream.
3947 // Any different unsignalled SSRC received will replace the default.
3948 TEST_F(WebRtcVideoChannel2Test, ReceiveDifferentUnsignaledSsrc) {
3949 // Allow receiving VP8, VP9, H264 (if enabled).
3950 cricket::VideoRecvParameters parameters;
3951 parameters.codecs.push_back(GetEngineCodec("VP8"));
3952 parameters.codecs.push_back(GetEngineCodec("VP9"));
3953
3954 #if defined(WEBRTC_USE_H264)
3955 cricket::VideoCodec H264codec(126, "H264");
3956 parameters.codecs.push_back(H264codec);
3957 #endif
3958
3959 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
3960 // No receive streams yet.
3961 ASSERT_EQ(0u, fake_call_->GetVideoReceiveStreams().size());
3962 cricket::FakeVideoRenderer renderer;
3963 EXPECT_TRUE(channel_->SetSink(kDefaultRecvSsrc, &renderer));
3964
3965 // Receive VP8 packet on first SSRC.
3966 uint8_t data[kMinRtpPacketLen];
3967 cricket::RtpHeader rtpHeader;
3968 rtpHeader.payload_type = GetEngineCodec("VP8").id;
3969 rtpHeader.seq_num = rtpHeader.timestamp = 0;
3970 rtpHeader.ssrc = kIncomingUnsignalledSsrc+1;
3971 cricket::SetRtpHeader(data, sizeof(data), rtpHeader);
3972 rtc::CopyOnWriteBuffer packet(data, sizeof(data));
3973 rtc::PacketTime packet_time;
3974 channel_->OnPacketReceived(&packet, packet_time);
3975 // VP8 packet should create default receive stream.
3976 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size());
3977 FakeVideoReceiveStream* recv_stream =
3978 fake_call_->GetVideoReceiveStreams()[0];
3979 EXPECT_EQ(rtpHeader.ssrc, recv_stream->GetConfig().rtp.remote_ssrc);
3980 // Verify that the receive stream sinks to a renderer.
3981 webrtc::VideoFrame video_frame(CreateBlackFrameBuffer(4, 4), 100, 0,
3982 webrtc::kVideoRotation_0);
3983 recv_stream->InjectFrame(video_frame);
3984 EXPECT_EQ(1, renderer.num_rendered_frames());
3985
3986 // Receive VP9 packet on second SSRC.
3987 rtpHeader.payload_type = GetEngineCodec("VP9").id;
3988 rtpHeader.ssrc = kIncomingUnsignalledSsrc+2;
3989 cricket::SetRtpHeader(data, sizeof(data), rtpHeader);
3990 rtc::CopyOnWriteBuffer packet2(data, sizeof(data));
3991 channel_->OnPacketReceived(&packet2, packet_time);
3992 // VP9 packet should replace the default receive SSRC.
3993 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size());
3994 recv_stream = fake_call_->GetVideoReceiveStreams()[0];
3995 EXPECT_EQ(rtpHeader.ssrc, recv_stream->GetConfig().rtp.remote_ssrc);
3996 // Verify that the receive stream sinks to a renderer.
3997 webrtc::VideoFrame video_frame2(CreateBlackFrameBuffer(4, 4), 200, 0,
3998 webrtc::kVideoRotation_0);
3999 recv_stream->InjectFrame(video_frame2);
4000 EXPECT_EQ(2, renderer.num_rendered_frames());
4001
4002 #if defined(WEBRTC_USE_H264)
4003 // Receive H264 packet on third SSRC.
4004 rtpHeader.payload_type = 126;
4005 rtpHeader.ssrc = kIncomingUnsignalledSsrc+3;
4006 cricket::SetRtpHeader(data, sizeof(data), rtpHeader);
4007 rtc::CopyOnWriteBuffer packet3(data, sizeof(data));
4008 channel_->OnPacketReceived(&packet3, packet_time);
4009 // H264 packet should replace the default receive SSRC.
4010 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size());
4011 recv_stream = fake_call_->GetVideoReceiveStreams()[0];
4012 EXPECT_EQ(rtpHeader.ssrc, recv_stream->GetConfig().rtp.remote_ssrc);
4013 // Verify that the receive stream sinks to a renderer.
4014 webrtc::VideoFrame video_frame3(CreateBlackFrameBuffer(4, 4), 300, 0,
4015 webrtc::kVideoRotation_0);
4016 recv_stream->InjectFrame(video_frame3);
4017 EXPECT_EQ(3, renderer.num_rendered_frames());
4018 #endif
4019 }
4020
4021 // This test verifies that when a new default stream is created for a new
4022 // unsignaled SSRC, the new stream does not overwrite any old stream that had
4023 // been the default receive stream before being properly signaled.
4024 TEST_F(WebRtcVideoChannel2Test,
4025 NewUnsignaledStreamDoesNotDestroyPreviouslyUnsignaledStream) {
4026 cricket::VideoRecvParameters parameters;
4027 parameters.codecs.push_back(GetEngineCodec("VP8"));
4028 ASSERT_TRUE(channel_->SetRecvParameters(parameters));
4029
4030 // No streams signaled and no packets received, so we should not have any
4031 // stream objects created yet.
4032 EXPECT_EQ(0u, fake_call_->GetVideoReceiveStreams().size());
4033
4034 // Receive packet on an unsignaled SSRC.
4035 uint8_t data[kMinRtpPacketLen];
4036 cricket::RtpHeader rtp_header;
4037 rtp_header.payload_type = GetEngineCodec("VP8").id;
4038 rtp_header.seq_num = rtp_header.timestamp = 0;
4039 rtp_header.ssrc = kSsrcs3[0];
4040 cricket::SetRtpHeader(data, sizeof(data), rtp_header);
4041 rtc::CopyOnWriteBuffer packet(data, sizeof(data));
4042 rtc::PacketTime packet_time;
4043 channel_->OnPacketReceived(&packet, packet_time);
4044 // Default receive stream should be created.
4045 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size());
4046 FakeVideoReceiveStream* recv_stream0 =
4047 fake_call_->GetVideoReceiveStreams()[0];
4048 EXPECT_EQ(kSsrcs3[0], recv_stream0->GetConfig().rtp.remote_ssrc);
4049
4050 // Signal the SSRC.
4051 EXPECT_TRUE(
4052 channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(kSsrcs3[0])));
4053 ASSERT_EQ(1u, fake_call_->GetVideoReceiveStreams().size());
4054 recv_stream0 = fake_call_->GetVideoReceiveStreams()[0];
4055 EXPECT_EQ(kSsrcs3[0], recv_stream0->GetConfig().rtp.remote_ssrc);
4056
4057 // Receive packet on a different unsignaled SSRC.
4058 rtp_header.ssrc = kSsrcs3[1];
4059 cricket::SetRtpHeader(data, sizeof(data), rtp_header);
4060 packet.SetData(data, sizeof(data));
4061 channel_->OnPacketReceived(&packet, packet_time);
4062 // New default receive stream should be created, but old stream should remain.
4063 ASSERT_EQ(2u, fake_call_->GetVideoReceiveStreams().size());
4064 EXPECT_EQ(recv_stream0, fake_call_->GetVideoReceiveStreams()[0]);
4065 FakeVideoReceiveStream* recv_stream1 =
4066 fake_call_->GetVideoReceiveStreams()[1];
4067 EXPECT_EQ(kSsrcs3[1], recv_stream1->GetConfig().rtp.remote_ssrc);
4068 }
4069
4070 TEST_F(WebRtcVideoChannel2Test, CanSentMaxBitrateForExistingStream) {
4071 AddSendStream();
4072
4073 cricket::FakeVideoCapturer capturer;
4074 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, &capturer));
4075 cricket::VideoFormat capture_format_hd =
4076 capturer.GetSupportedFormats()->front();
4077 EXPECT_EQ(1280, capture_format_hd.width);
4078 EXPECT_EQ(720, capture_format_hd.height);
4079 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(capture_format_hd));
4080 EXPECT_TRUE(channel_->SetSend(true));
4081 capturer.CaptureFrame();
4082
4083 int default_encoder_bitrate = GetMaxEncoderBitrate();
4084 EXPECT_GT(default_encoder_bitrate, 1000);
4085
4086 // TODO(skvlad): Resolve the inconsistency between the interpretation
4087 // of the global bitrate limit for audio and video:
4088 // - Audio: max_bandwidth_bps = 0 - fail the operation,
4089 // max_bandwidth_bps = -1 - remove the bandwidth limit
4090 // - Video: max_bandwidth_bps = 0 - remove the bandwidth limit,
4091 // max_bandwidth_bps = -1 - remove the bandwidth limit
4092
4093 SetAndExpectMaxBitrate(1000, 0, 1000);
4094 SetAndExpectMaxBitrate(1000, 800, 800);
4095 SetAndExpectMaxBitrate(600, 800, 600);
4096 SetAndExpectMaxBitrate(0, 800, 800);
4097 SetAndExpectMaxBitrate(0, 0, default_encoder_bitrate);
4098
4099 EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
4100 }
4101
4102 TEST_F(WebRtcVideoChannel2Test, CannotSetMaxBitrateForNonexistentStream) {
4103 webrtc::RtpParameters nonexistent_parameters =
4104 channel_->GetRtpSendParameters(last_ssrc_);
4105 EXPECT_EQ(0, nonexistent_parameters.encodings.size());
4106
4107 nonexistent_parameters.encodings.push_back(webrtc::RtpEncodingParameters());
4108 EXPECT_FALSE(
4109 channel_->SetRtpSendParameters(last_ssrc_, nonexistent_parameters));
4110 }
4111
4112 TEST_F(WebRtcVideoChannel2Test,
4113 CannotSetRtpSendParametersWithIncorrectNumberOfEncodings) {
4114 // This test verifies that setting RtpParameters succeeds only if
4115 // the structure contains exactly one encoding.
4116 // TODO(skvlad): Update this test when we start supporting setting parameters
4117 // for each encoding individually.
4118
4119 AddSendStream();
4120 webrtc::RtpParameters parameters = channel_->GetRtpSendParameters(last_ssrc_);
4121 // Two or more encodings should result in failure.
4122 parameters.encodings.push_back(webrtc::RtpEncodingParameters());
4123 EXPECT_FALSE(channel_->SetRtpSendParameters(last_ssrc_, parameters));
4124 // Zero encodings should also fail.
4125 parameters.encodings.clear();
4126 EXPECT_FALSE(channel_->SetRtpSendParameters(last_ssrc_, parameters));
4127 }
4128
4129 // Changing the SSRC through RtpParameters is not allowed.
4130 TEST_F(WebRtcVideoChannel2Test, CannotSetSsrcInRtpSendParameters) {
4131 AddSendStream();
4132 webrtc::RtpParameters parameters = channel_->GetRtpSendParameters(last_ssrc_);
4133 parameters.encodings[0].ssrc = rtc::Optional<uint32_t>(0xdeadbeef);
4134 EXPECT_FALSE(channel_->SetRtpSendParameters(last_ssrc_, parameters));
4135 }
4136
4137 // Test that a stream will not be sending if its encoding is made inactive
4138 // through SetRtpSendParameters.
4139 // TODO(deadbeef): Update this test when we start supporting setting parameters
4140 // for each encoding individually.
4141 TEST_F(WebRtcVideoChannel2Test, SetRtpSendParametersEncodingsActive) {
4142 FakeVideoSendStream* stream = AddSendStream();
4143 EXPECT_TRUE(channel_->SetSend(true));
4144 EXPECT_TRUE(stream->IsSending());
4145
4146 // Get current parameters and change "active" to false.
4147 webrtc::RtpParameters parameters = channel_->GetRtpSendParameters(last_ssrc_);
4148 ASSERT_EQ(1u, parameters.encodings.size());
4149 ASSERT_TRUE(parameters.encodings[0].active);
4150 parameters.encodings[0].active = false;
4151 EXPECT_TRUE(channel_->SetRtpSendParameters(last_ssrc_, parameters));
4152 EXPECT_FALSE(stream->IsSending());
4153
4154 // Now change it back to active and verify we resume sending.
4155 parameters.encodings[0].active = true;
4156 EXPECT_TRUE(channel_->SetRtpSendParameters(last_ssrc_, parameters));
4157 EXPECT_TRUE(stream->IsSending());
4158 }
4159
4160 // Test that if a stream is reconfigured (due to a codec change or other
4161 // change) while its encoding is still inactive, it doesn't start sending.
4162 TEST_F(WebRtcVideoChannel2Test,
4163 InactiveStreamDoesntStartSendingWhenReconfigured) {
4164 // Set an initial codec list, which will be modified later.
4165 cricket::VideoSendParameters parameters1;
4166 parameters1.codecs.push_back(GetEngineCodec("VP8"));
4167 parameters1.codecs.push_back(GetEngineCodec("VP9"));
4168 EXPECT_TRUE(channel_->SetSendParameters(parameters1));
4169
4170 FakeVideoSendStream* stream = AddSendStream();
4171 EXPECT_TRUE(channel_->SetSend(true));
4172 EXPECT_TRUE(stream->IsSending());
4173
4174 // Get current parameters and change "active" to false.
4175 webrtc::RtpParameters parameters = channel_->GetRtpSendParameters(last_ssrc_);
4176 ASSERT_EQ(1u, parameters.encodings.size());
4177 ASSERT_TRUE(parameters.encodings[0].active);
4178 parameters.encodings[0].active = false;
4179 EXPECT_EQ(1u, GetFakeSendStreams().size());
4180 EXPECT_EQ(1, fake_call_->GetNumCreatedSendStreams());
4181 EXPECT_TRUE(channel_->SetRtpSendParameters(last_ssrc_, parameters));
4182 EXPECT_FALSE(stream->IsSending());
4183
4184 // Reorder the codec list, causing the stream to be reconfigured.
4185 cricket::VideoSendParameters parameters2;
4186 parameters2.codecs.push_back(GetEngineCodec("VP9"));
4187 parameters2.codecs.push_back(GetEngineCodec("VP8"));
4188 EXPECT_TRUE(channel_->SetSendParameters(parameters2));
4189 auto new_streams = GetFakeSendStreams();
4190 // Assert that a new underlying stream was created due to the codec change.
4191 // Otherwise, this test isn't testing what it set out to test.
4192 EXPECT_EQ(1u, GetFakeSendStreams().size());
4193 EXPECT_EQ(2, fake_call_->GetNumCreatedSendStreams());
4194
4195 // Verify that we still are not sending anything, due to the inactive
4196 // encoding.
4197 EXPECT_FALSE(new_streams[0]->IsSending());
4198 }
4199
4200 // Test that GetRtpSendParameters returns the currently configured codecs.
4201 TEST_F(WebRtcVideoChannel2Test, GetRtpSendParametersCodecs) {
4202 AddSendStream();
4203 cricket::VideoSendParameters parameters;
4204 parameters.codecs.push_back(GetEngineCodec("VP8"));
4205 parameters.codecs.push_back(GetEngineCodec("VP9"));
4206 EXPECT_TRUE(channel_->SetSendParameters(parameters));
4207
4208 webrtc::RtpParameters rtp_parameters =
4209 channel_->GetRtpSendParameters(last_ssrc_);
4210 ASSERT_EQ(2u, rtp_parameters.codecs.size());
4211 EXPECT_EQ(GetEngineCodec("VP8").ToCodecParameters(),
4212 rtp_parameters.codecs[0]);
4213 EXPECT_EQ(GetEngineCodec("VP9").ToCodecParameters(),
4214 rtp_parameters.codecs[1]);
4215 }
4216
4217 // Test that RtpParameters for send stream has one encoding and it has
4218 // the correct SSRC.
4219 TEST_F(WebRtcVideoChannel2Test, GetRtpSendParametersSsrc) {
4220 AddSendStream();
4221
4222 webrtc::RtpParameters rtp_parameters =
4223 channel_->GetRtpSendParameters(last_ssrc_);
4224 ASSERT_EQ(1u, rtp_parameters.encodings.size());
4225 EXPECT_EQ(rtc::Optional<uint32_t>(last_ssrc_),
4226 rtp_parameters.encodings[0].ssrc);
4227 }
4228
4229 // Test that if we set/get parameters multiple times, we get the same results.
4230 TEST_F(WebRtcVideoChannel2Test, SetAndGetRtpSendParameters) {
4231 AddSendStream();
4232 cricket::VideoSendParameters parameters;
4233 parameters.codecs.push_back(GetEngineCodec("VP8"));
4234 parameters.codecs.push_back(GetEngineCodec("VP9"));
4235 EXPECT_TRUE(channel_->SetSendParameters(parameters));
4236
4237 webrtc::RtpParameters initial_params =
4238 channel_->GetRtpSendParameters(last_ssrc_);
4239
4240 // We should be able to set the params we just got.
4241 EXPECT_TRUE(channel_->SetRtpSendParameters(last_ssrc_, initial_params));
4242
4243 // ... And this shouldn't change the params returned by GetRtpSendParameters.
4244 EXPECT_EQ(initial_params, channel_->GetRtpSendParameters(last_ssrc_));
4245 }
4246
4247 // Test that GetRtpReceiveParameters returns the currently configured codecs.
4248 TEST_F(WebRtcVideoChannel2Test, GetRtpReceiveParametersCodecs) {
4249 AddRecvStream();
4250 cricket::VideoRecvParameters parameters;
4251 parameters.codecs.push_back(GetEngineCodec("VP8"));
4252 parameters.codecs.push_back(GetEngineCodec("VP9"));
4253 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
4254
4255 webrtc::RtpParameters rtp_parameters =
4256 channel_->GetRtpReceiveParameters(last_ssrc_);
4257 ASSERT_EQ(2u, rtp_parameters.codecs.size());
4258 EXPECT_EQ(GetEngineCodec("VP8").ToCodecParameters(),
4259 rtp_parameters.codecs[0]);
4260 EXPECT_EQ(GetEngineCodec("VP9").ToCodecParameters(),
4261 rtp_parameters.codecs[1]);
4262 }
4263
4264 #if defined(WEBRTC_USE_H264)
4265 TEST_F(WebRtcVideoChannel2Test, GetRtpReceiveFmtpSprop) {
4266 #else
4267 TEST_F(WebRtcVideoChannel2Test, DISABLED_GetRtpReceiveFmtpSprop) {
4268 #endif
4269 cricket::VideoRecvParameters parameters;
4270 cricket::VideoCodec kH264sprop1(101, "H264");
4271 kH264sprop1.SetParam(kH264FmtpSpropParameterSets, "uvw");
4272 parameters.codecs.push_back(kH264sprop1);
4273 cricket::VideoCodec kH264sprop2(102, "H264");
4274 kH264sprop2.SetParam(kH264FmtpSpropParameterSets, "xyz");
4275 parameters.codecs.push_back(kH264sprop2);
4276 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
4277
4278 FakeVideoReceiveStream* recv_stream = AddRecvStream();
4279 const webrtc::VideoReceiveStream::Config& cfg = recv_stream->GetConfig();
4280 webrtc::RtpParameters rtp_parameters =
4281 channel_->GetRtpReceiveParameters(last_ssrc_);
4282 ASSERT_EQ(2u, rtp_parameters.codecs.size());
4283 EXPECT_EQ(kH264sprop1.ToCodecParameters(), rtp_parameters.codecs[0]);
4284 ASSERT_EQ(2u, cfg.decoders.size());
4285 EXPECT_EQ(101, cfg.decoders[0].payload_type);
4286 EXPECT_EQ("H264", cfg.decoders[0].payload_name);
4287 const auto it0 =
4288 cfg.decoders[0].codec_params.find(kH264FmtpSpropParameterSets);
4289 ASSERT_TRUE(it0 != cfg.decoders[0].codec_params.end());
4290 EXPECT_EQ("uvw", it0->second);
4291
4292 EXPECT_EQ(102, cfg.decoders[1].payload_type);
4293 EXPECT_EQ("H264", cfg.decoders[1].payload_name);
4294 const auto it1 =
4295 cfg.decoders[1].codec_params.find(kH264FmtpSpropParameterSets);
4296 ASSERT_TRUE(it1 != cfg.decoders[1].codec_params.end());
4297 EXPECT_EQ("xyz", it1->second);
4298 }
4299
4300 // Test that RtpParameters for receive stream has one encoding and it has
4301 // the correct SSRC.
4302 TEST_F(WebRtcVideoChannel2Test, GetRtpReceiveParametersSsrc) {
4303 AddRecvStream();
4304
4305 webrtc::RtpParameters rtp_parameters =
4306 channel_->GetRtpReceiveParameters(last_ssrc_);
4307 ASSERT_EQ(1u, rtp_parameters.encodings.size());
4308 EXPECT_EQ(rtc::Optional<uint32_t>(last_ssrc_),
4309 rtp_parameters.encodings[0].ssrc);
4310 }
4311
4312 // Test that if we set/get parameters multiple times, we get the same results.
4313 TEST_F(WebRtcVideoChannel2Test, SetAndGetRtpReceiveParameters) {
4314 AddRecvStream();
4315 cricket::VideoRecvParameters parameters;
4316 parameters.codecs.push_back(GetEngineCodec("VP8"));
4317 parameters.codecs.push_back(GetEngineCodec("VP9"));
4318 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
4319
4320 webrtc::RtpParameters initial_params =
4321 channel_->GetRtpReceiveParameters(last_ssrc_);
4322
4323 // We should be able to set the params we just got.
4324 EXPECT_TRUE(channel_->SetRtpReceiveParameters(last_ssrc_, initial_params));
4325
4326 // ... And this shouldn't change the params returned by
4327 // GetRtpReceiveParameters.
4328 EXPECT_EQ(initial_params, channel_->GetRtpReceiveParameters(last_ssrc_));
4329 }
4330
4331 // Test that GetRtpReceiveParameters returns parameters correctly when SSRCs
4332 // aren't signaled. It should always return an empty "RtpEncodingParameters",
4333 // even after a packet is received and the unsignaled SSRC is known.
4334 TEST_F(WebRtcVideoChannel2Test, GetRtpReceiveParametersWithUnsignaledSsrc) {
4335 // Call necessary methods to configure receiving a default stream as
4336 // soon as it arrives.
4337 cricket::VideoRecvParameters parameters;
4338 parameters.codecs.push_back(GetEngineCodec("VP8"));
4339 parameters.codecs.push_back(GetEngineCodec("VP9"));
4340 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
4341
4342 // Call GetRtpReceiveParameters before configured to receive an unsignaled
4343 // stream. Should return nothing.
4344 EXPECT_EQ(webrtc::RtpParameters(), channel_->GetRtpReceiveParameters(0));
4345
4346 // Set a sink for an unsignaled stream.
4347 cricket::FakeVideoRenderer renderer;
4348 // Value of "0" means "unsignaled stream".
4349 EXPECT_TRUE(channel_->SetSink(0, &renderer));
4350
4351 // Call GetRtpReceiveParameters before the SSRC is known. Value of "0"
4352 // in this method means "unsignaled stream".
4353 webrtc::RtpParameters rtp_parameters = channel_->GetRtpReceiveParameters(0);
4354 ASSERT_EQ(1u, rtp_parameters.encodings.size());
4355 EXPECT_FALSE(rtp_parameters.encodings[0].ssrc);
4356
4357 // Receive VP8 packet.
4358 uint8_t data[kMinRtpPacketLen];
4359 cricket::RtpHeader rtpHeader;
4360 rtpHeader.payload_type = GetEngineCodec("VP8").id;
4361 rtpHeader.seq_num = rtpHeader.timestamp = 0;
4362 rtpHeader.ssrc = kIncomingUnsignalledSsrc;
4363 cricket::SetRtpHeader(data, sizeof(data), rtpHeader);
4364 rtc::CopyOnWriteBuffer packet(data, sizeof(data));
4365 rtc::PacketTime packet_time;
4366 channel_->OnPacketReceived(&packet, packet_time);
4367
4368 // The |ssrc| member should still be unset.
4369 rtp_parameters = channel_->GetRtpReceiveParameters(0);
4370 ASSERT_EQ(1u, rtp_parameters.encodings.size());
4371 EXPECT_FALSE(rtp_parameters.encodings[0].ssrc);
4372 }
4373
4374 void WebRtcVideoChannel2Test::TestReceiverLocalSsrcConfiguration(
4375 bool receiver_first) {
4376 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
4377
4378 const uint32_t kSenderSsrc = 0xC0FFEE;
4379 const uint32_t kSecondSenderSsrc = 0xBADCAFE;
4380 const uint32_t kReceiverSsrc = 0x4711;
4381 const uint32_t kExpectedDefaultReceiverSsrc = 1;
4382
4383 if (receiver_first) {
4384 AddRecvStream(StreamParams::CreateLegacy(kReceiverSsrc));
4385 std::vector<FakeVideoReceiveStream*> receive_streams =
4386 fake_call_->GetVideoReceiveStreams();
4387 ASSERT_EQ(1u, receive_streams.size());
4388 // Default local SSRC when we have no sender.
4389 EXPECT_EQ(kExpectedDefaultReceiverSsrc,
4390 receive_streams[0]->GetConfig().rtp.local_ssrc);
4391 }
4392 AddSendStream(StreamParams::CreateLegacy(kSenderSsrc));
4393 if (!receiver_first)
4394 AddRecvStream(StreamParams::CreateLegacy(kReceiverSsrc));
4395 std::vector<FakeVideoReceiveStream*> receive_streams =
4396 fake_call_->GetVideoReceiveStreams();
4397 ASSERT_EQ(1u, receive_streams.size());
4398 EXPECT_EQ(kSenderSsrc, receive_streams[0]->GetConfig().rtp.local_ssrc);
4399
4400 // Removing first sender should fall back to another (in this case the second)
4401 // local send stream's SSRC.
4402 AddSendStream(StreamParams::CreateLegacy(kSecondSenderSsrc));
4403 ASSERT_TRUE(channel_->RemoveSendStream(kSenderSsrc));
4404 receive_streams =
4405 fake_call_->GetVideoReceiveStreams();
4406 ASSERT_EQ(1u, receive_streams.size());
4407 EXPECT_EQ(kSecondSenderSsrc, receive_streams[0]->GetConfig().rtp.local_ssrc);
4408
4409 // Removing the last sender should fall back to default local SSRC.
4410 ASSERT_TRUE(channel_->RemoveSendStream(kSecondSenderSsrc));
4411 receive_streams =
4412 fake_call_->GetVideoReceiveStreams();
4413 ASSERT_EQ(1u, receive_streams.size());
4414 EXPECT_EQ(kExpectedDefaultReceiverSsrc,
4415 receive_streams[0]->GetConfig().rtp.local_ssrc);
4416 }
4417
4418 TEST_F(WebRtcVideoChannel2Test, ConfiguresLocalSsrc) {
4419 TestReceiverLocalSsrcConfiguration(false);
4420 }
4421
4422 TEST_F(WebRtcVideoChannel2Test, ConfiguresLocalSsrcOnExistingReceivers) {
4423 TestReceiverLocalSsrcConfiguration(true);
4424 }
4425
4426 class WebRtcVideoChannel2SimulcastTest : public testing::Test {
4427 public:
4428 WebRtcVideoChannel2SimulcastTest()
4429 : fake_call_(webrtc::Call::Config(&event_log_)), last_ssrc_(0) {}
4430
4431 void SetUp() override {
4432 engine_.Init();
4433 channel_.reset(
4434 engine_.CreateChannel(&fake_call_, GetMediaConfig(), VideoOptions()));
4435 channel_->OnReadyToSend(true);
4436 last_ssrc_ = 123;
4437 }
4438
4439 protected:
4440 void VerifySimulcastSettings(const VideoCodec& codec,
4441 int capture_width,
4442 int capture_height,
4443 size_t num_configured_streams,
4444 size_t expected_num_streams,
4445 bool screenshare,
4446 bool conference_mode) {
4447 cricket::VideoSendParameters parameters;
4448 parameters.codecs.push_back(codec);
4449 parameters.conference_mode = conference_mode;
4450 ASSERT_TRUE(channel_->SetSendParameters(parameters));
4451
4452 std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs3);
4453 RTC_DCHECK(num_configured_streams <= ssrcs.size());
4454 ssrcs.resize(num_configured_streams);
4455
4456 AddSendStream(CreateSimStreamParams("cname", ssrcs));
4457 // Send a full-size frame to trigger a stream reconfiguration to use all
4458 // expected simulcast layers.
4459 cricket::FakeVideoCapturer capturer;
4460 VideoOptions options;
4461 if (screenshare)
4462 options.is_screencast = rtc::Optional<bool>(screenshare);
4463 EXPECT_TRUE(
4464 channel_->SetVideoSend(ssrcs.front(), true, &options, &capturer));
4465 // Fetch the latest stream since SetVideoSend() may recreate it if the
4466 // screen content setting is changed.
4467 FakeVideoSendStream* stream = fake_call_.GetVideoSendStreams().front();
4468 EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(cricket::VideoFormat(
4469 capture_width, capture_height,
4470 cricket::VideoFormat::FpsToInterval(30),
4471 cricket::FOURCC_I420)));
4472 channel_->SetSend(true);
4473 EXPECT_TRUE(capturer.CaptureFrame());
4474
4475 std::vector<webrtc::VideoStream> video_streams = stream->GetVideoStreams();
4476 ASSERT_EQ(expected_num_streams, video_streams.size());
4477
4478 std::vector<webrtc::VideoStream> expected_streams;
4479 if (conference_mode) {
4480 expected_streams = GetSimulcastConfig(
4481 num_configured_streams, capture_width, capture_height, 0,
4482 kDefaultQpMax, kDefaultVideoMaxFramerate, screenshare);
4483 if (screenshare) {
4484 for (const webrtc::VideoStream& stream : expected_streams) {
4485 // Never scale screen content.
4486 EXPECT_EQ(stream.width, capture_width);
4487 EXPECT_EQ(stream.height, capture_height);
4488 }
4489 }
4490 } else {
4491 webrtc::VideoStream stream;
4492 stream.width = capture_width;
4493 stream.height = capture_height;
4494 stream.max_framerate = kDefaultVideoMaxFramerate;
4495 stream.min_bitrate_bps = cricket::kMinVideoBitrateKbps * 1000;
4496 int max_bitrate_kbps;
4497 if (capture_width * capture_height <= 320 * 240) {
4498 max_bitrate_kbps = 600;
4499 } else if (capture_width * capture_height <= 640 * 480) {
4500 max_bitrate_kbps = 1700;
4501 } else if (capture_width * capture_height <= 960 * 540) {
4502 max_bitrate_kbps = 2000;
4503 } else {
4504 max_bitrate_kbps = 2500;
4505 }
4506 stream.target_bitrate_bps = stream.max_bitrate_bps =
4507 max_bitrate_kbps * 1000;
4508 stream.max_qp = kDefaultQpMax;
4509 expected_streams.push_back(stream);
4510 }
4511
4512 ASSERT_EQ(expected_streams.size(), video_streams.size());
4513
4514 size_t num_streams = video_streams.size();
4515 int total_max_bitrate_bps = 0;
4516 for (size_t i = 0; i < num_streams; ++i) {
4517 EXPECT_EQ(expected_streams[i].width, video_streams[i].width);
4518 EXPECT_EQ(expected_streams[i].height, video_streams[i].height);
4519
4520 EXPECT_GT(video_streams[i].max_framerate, 0);
4521 EXPECT_EQ(expected_streams[i].max_framerate,
4522 video_streams[i].max_framerate);
4523
4524 EXPECT_GT(video_streams[i].min_bitrate_bps, 0);
4525 EXPECT_EQ(expected_streams[i].min_bitrate_bps,
4526 video_streams[i].min_bitrate_bps);
4527
4528 EXPECT_GT(video_streams[i].target_bitrate_bps, 0);
4529 EXPECT_EQ(expected_streams[i].target_bitrate_bps,
4530 video_streams[i].target_bitrate_bps);
4531
4532 EXPECT_GT(video_streams[i].max_bitrate_bps, 0);
4533 EXPECT_EQ(expected_streams[i].max_bitrate_bps,
4534 video_streams[i].max_bitrate_bps);
4535
4536 EXPECT_GT(video_streams[i].max_qp, 0);
4537 EXPECT_EQ(expected_streams[i].max_qp, video_streams[i].max_qp);
4538
4539 EXPECT_EQ(!conference_mode,
4540 expected_streams[i].temporal_layer_thresholds_bps.empty());
4541 EXPECT_EQ(expected_streams[i].temporal_layer_thresholds_bps,
4542 video_streams[i].temporal_layer_thresholds_bps);
4543
4544 if (i == num_streams - 1) {
4545 total_max_bitrate_bps += video_streams[i].max_bitrate_bps;
4546 } else {
4547 total_max_bitrate_bps += video_streams[i].target_bitrate_bps;
4548 }
4549 }
4550
4551 EXPECT_TRUE(channel_->SetVideoSend(ssrcs.front(), true, nullptr, nullptr));
4552 }
4553
4554 FakeVideoSendStream* AddSendStream() {
4555 return AddSendStream(StreamParams::CreateLegacy(last_ssrc_++));
4556 }
4557
4558 FakeVideoSendStream* AddSendStream(const StreamParams& sp) {
4559 size_t num_streams =
4560 fake_call_.GetVideoSendStreams().size();
4561 EXPECT_TRUE(channel_->AddSendStream(sp));
4562 std::vector<FakeVideoSendStream*> streams =
4563 fake_call_.GetVideoSendStreams();
4564 EXPECT_EQ(num_streams + 1, streams.size());
4565 return streams[streams.size() - 1];
4566 }
4567
4568 std::vector<FakeVideoSendStream*> GetFakeSendStreams() {
4569 return fake_call_.GetVideoSendStreams();
4570 }
4571
4572 FakeVideoReceiveStream* AddRecvStream() {
4573 return AddRecvStream(StreamParams::CreateLegacy(last_ssrc_++));
4574 }
4575
4576 FakeVideoReceiveStream* AddRecvStream(const StreamParams& sp) {
4577 size_t num_streams =
4578 fake_call_.GetVideoReceiveStreams().size();
4579 EXPECT_TRUE(channel_->AddRecvStream(sp));
4580 std::vector<FakeVideoReceiveStream*> streams =
4581 fake_call_.GetVideoReceiveStreams();
4582 EXPECT_EQ(num_streams + 1, streams.size());
4583 return streams[streams.size() - 1];
4584 }
4585
4586 webrtc::RtcEventLogNullImpl event_log_;
4587 FakeCall fake_call_;
4588 WebRtcVideoEngine2 engine_;
4589 std::unique_ptr<VideoMediaChannel> channel_;
4590 uint32_t last_ssrc_;
4591 };
4592
4593 TEST_F(WebRtcVideoChannel2SimulcastTest, SetSendCodecsWith2SimulcastStreams) {
4594 VerifySimulcastSettings(cricket::VideoCodec("VP8"), 640, 360, 2, 2, false,
4595 true);
4596 }
4597
4598 TEST_F(WebRtcVideoChannel2SimulcastTest, SetSendCodecsWith3SimulcastStreams) {
4599 VerifySimulcastSettings(cricket::VideoCodec("VP8"), 1280, 720, 3, 3, false,
4600 true);
4601 }
4602
4603 // Test that we normalize send codec format size in simulcast.
4604 TEST_F(WebRtcVideoChannel2SimulcastTest, SetSendCodecsWithOddSizeInSimulcast) {
4605 VerifySimulcastSettings(cricket::VideoCodec("VP8"), 541, 271, 2, 2, false,
4606 true);
4607 }
4608
4609 TEST_F(WebRtcVideoChannel2SimulcastTest, SetSendCodecsForScreenshare) {
4610 VerifySimulcastSettings(cricket::VideoCodec("VP8"), 1280, 720, 3, 1, true,
4611 false);
4612 }
4613
4614 TEST_F(WebRtcVideoChannel2SimulcastTest,
4615 SetSendCodecsForConferenceModeScreenshare) {
4616 VerifySimulcastSettings(cricket::VideoCodec("VP8"), 1280, 720, 3, 1, true,
4617 true);
4618 }
4619
4620 TEST_F(WebRtcVideoChannel2SimulcastTest, SetSendCodecsForSimulcastScreenshare) {
4621 webrtc::test::ScopedFieldTrials override_field_trials_(
4622 "WebRTC-SimulcastScreenshare/Enabled/");
4623 VerifySimulcastSettings(cricket::VideoCodec("VP8"), 1280, 720, 3, 2, true,
4624 true);
4625 }
4626
4627 TEST_F(WebRtcVideoChannel2SimulcastTest,
4628 NoSimulcastScreenshareWithoutConference) {
4629 webrtc::test::ScopedFieldTrials override_field_trials_(
4630 "WebRTC-SimulcastScreenshare/Enabled/");
4631 VerifySimulcastSettings(cricket::VideoCodec("VP8"), 1280, 720, 3, 1, true,
4632 false);
4633 }
4634
4635 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/media/engine/webrtcvideoengine2.cc ('k') | webrtc/media/engine/webrtcvideoengine_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698