summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/video/config/simulcast_unittest.cc
blob: 152a0f9525a854d4d74eee8591d59e6e6b6f53b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "video/config/simulcast.h"

#include "api/transport/field_trial_based_config.h"
#include "media/base/media_constants.h"
#include "test/field_trial.h"
#include "test/gtest.h"

namespace webrtc {
namespace {
constexpr int kQpMax = 55;
constexpr double kBitratePriority = 2.0;
constexpr bool kScreenshare = true;
constexpr int kDefaultTemporalLayers = 3;  // Value from simulcast.cc.

// Values from kSimulcastConfigs in simulcast.cc.
const std::vector<VideoStream> GetSimulcastBitrates720p() {
  std::vector<VideoStream> streams(3);
  streams[0].min_bitrate_bps = 30000;
  streams[0].target_bitrate_bps = 150000;
  streams[0].max_bitrate_bps = 200000;
  streams[1].min_bitrate_bps = 150000;
  streams[1].target_bitrate_bps = 500000;
  streams[1].max_bitrate_bps = 700000;
  streams[2].min_bitrate_bps = 600000;
  streams[2].target_bitrate_bps = 2500000;
  streams[2].max_bitrate_bps = 2500000;
  return streams;
}
}  // namespace

TEST(SimulcastTest, TotalMaxBitrateIsZeroForNoStreams) {
  std::vector<VideoStream> streams;
  EXPECT_EQ(0, cricket::GetTotalMaxBitrate(streams).bps());
}

TEST(SimulcastTest, GetTotalMaxBitrateForSingleStream) {
  std::vector<VideoStream> streams(1);
  streams[0].max_bitrate_bps = 100000;
  EXPECT_EQ(100000, cricket::GetTotalMaxBitrate(streams).bps());
}

TEST(SimulcastTest, GetTotalMaxBitrateForMultipleStreams) {
  std::vector<VideoStream> streams(3);
  streams[0].target_bitrate_bps = 100000;
  streams[1].target_bitrate_bps = 200000;
  streams[2].max_bitrate_bps = 400000;
  EXPECT_EQ(700000, cricket::GetTotalMaxBitrate(streams).bps());
}

TEST(SimulcastTest, BandwidthAboveTotalMaxBitrateGivenToHighestStream) {
  std::vector<VideoStream> streams(3);
  streams[0].target_bitrate_bps = 100000;
  streams[1].target_bitrate_bps = 200000;
  streams[2].max_bitrate_bps = 400000;

  const webrtc::DataRate one_bps = webrtc::DataRate::BitsPerSec(1);

  // No bitrate above the total max to give to the highest stream.
  const webrtc::DataRate max_total_bitrate =
      cricket::GetTotalMaxBitrate(streams);
  cricket::BoostMaxSimulcastLayer(max_total_bitrate, &streams);
  EXPECT_EQ(400000, streams[2].max_bitrate_bps);
  EXPECT_EQ(max_total_bitrate, cricket::GetTotalMaxBitrate(streams));

  // The bitrate above the total max should be given to the highest stream.
  cricket::BoostMaxSimulcastLayer(max_total_bitrate + one_bps, &streams);
  EXPECT_EQ(400000 + 1, streams[2].max_bitrate_bps);
  EXPECT_EQ(max_total_bitrate + one_bps, cricket::GetTotalMaxBitrate(streams));
}

TEST(SimulcastTest, GetConfig) {
  const std::vector<VideoStream> kExpected = GetSimulcastBitrates720p();
  const FieldTrialBasedConfig trials;

  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 1280, 720, kBitratePriority, kQpMax,
      !kScreenshare, true, trials);

  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(320u, streams[0].width);
  EXPECT_EQ(180u, streams[0].height);
  EXPECT_EQ(640u, streams[1].width);
  EXPECT_EQ(360u, streams[1].height);
  EXPECT_EQ(1280u, streams[2].width);
  EXPECT_EQ(720u, streams[2].height);

  for (size_t i = 0; i < streams.size(); ++i) {
    EXPECT_EQ(size_t{kDefaultTemporalLayers}, streams[i].num_temporal_layers);
    EXPECT_EQ(cricket::kDefaultVideoMaxFramerate, streams[i].max_framerate);
    EXPECT_EQ(kQpMax, streams[i].max_qp);
    EXPECT_EQ(kExpected[i].min_bitrate_bps, streams[i].min_bitrate_bps);
    EXPECT_EQ(kExpected[i].target_bitrate_bps, streams[i].target_bitrate_bps);
    EXPECT_EQ(kExpected[i].max_bitrate_bps, streams[i].max_bitrate_bps);
    EXPECT_TRUE(streams[i].active);
  }
  // Currently set on lowest stream.
  EXPECT_EQ(kBitratePriority, streams[0].bitrate_priority);
  EXPECT_FALSE(streams[1].bitrate_priority);
  EXPECT_FALSE(streams[2].bitrate_priority);
}

TEST(SimulcastTest, GetConfigWithBaseHeavyVP8TL3RateAllocation) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-UseBaseHeavyVP8TL3RateAllocation/Enabled/");
  FieldTrialBasedConfig trials;

  const std::vector<VideoStream> kExpected = GetSimulcastBitrates720p();

  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 1280, 720, kBitratePriority, kQpMax,
      !kScreenshare, true, trials);

  EXPECT_EQ(kExpected[0].min_bitrate_bps, streams[0].min_bitrate_bps);
  EXPECT_EQ(static_cast<int>(0.4 * kExpected[0].target_bitrate_bps / 0.6),
            streams[0].target_bitrate_bps);
  EXPECT_EQ(static_cast<int>(0.4 * kExpected[0].max_bitrate_bps / 0.6),
            streams[0].max_bitrate_bps);
  for (size_t i = 1; i < streams.size(); ++i) {
    EXPECT_EQ(kExpected[i].min_bitrate_bps, streams[i].min_bitrate_bps);
    EXPECT_EQ(kExpected[i].target_bitrate_bps, streams[i].target_bitrate_bps);
    EXPECT_EQ(kExpected[i].max_bitrate_bps, streams[i].max_bitrate_bps);
  }
}

TEST(SimulcastTest, GetConfigWithLimitedMaxLayers) {
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 2;
  FieldTrialBasedConfig trials;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 1280, 720, kBitratePriority, kQpMax,
      !kScreenshare, true, trials);

  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(640u, streams[0].width);
  EXPECT_EQ(360u, streams[0].height);
  EXPECT_EQ(1280u, streams[1].width);
  EXPECT_EQ(720u, streams[1].height);
}

TEST(SimulcastTest, GetConfigWithLimitedMaxLayersForResolution) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-LegacySimulcastLayerLimit/Enabled/");
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 800, 600, kBitratePriority, kQpMax, !kScreenshare,
      true, trials);

  EXPECT_EQ(2u, streams.size());
  EXPECT_EQ(400u, streams[0].width);
  EXPECT_EQ(300u, streams[0].height);
  EXPECT_EQ(800u, streams[1].width);
  EXPECT_EQ(600u, streams[1].height);
}

TEST(SimulcastTest, GetConfigWithLowResolutionScreenshare) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-LegacySimulcastLayerLimit/Enabled/");
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 100, 100, kBitratePriority, kQpMax, kScreenshare,
      true, trials);

  // Simulcast streams number is never decreased for screenshare,
  // even for very low resolution.
  EXPECT_GT(streams.size(), 1u);
}

TEST(SimulcastTest, GetConfigWithNotLimitedMaxLayersForResolution) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-LegacySimulcastLayerLimit/Disabled/");
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 800, 600, kBitratePriority, kQpMax, !kScreenshare,
      true, trials);

  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(200u, streams[0].width);
  EXPECT_EQ(150u, streams[0].height);
  EXPECT_EQ(400u, streams[1].width);
  EXPECT_EQ(300u, streams[1].height);
  EXPECT_EQ(800u, streams[2].width);
  EXPECT_EQ(600u, streams[2].height);
}

TEST(SimulcastTest, GetConfigWithNormalizedResolution) {
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 2;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 640 + 1, 360 + 1, kBitratePriority, kQpMax,
      !kScreenshare, true, trials);

  // Must be divisible by |2 ^ (num_layers - 1)|.
  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(320u, streams[0].width);
  EXPECT_EQ(180u, streams[0].height);
  EXPECT_EQ(640u, streams[1].width);
  EXPECT_EQ(360u, streams[1].height);
}

TEST(SimulcastTest, GetConfigWithNormalizedResolutionDivisibleBy4) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-NormalizeSimulcastResolution/Enabled-2/");
  FieldTrialBasedConfig trials;

  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 2;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 709, 501, kBitratePriority, kQpMax, !kScreenshare,
      true, trials);

  // Must be divisible by |2 ^ 2|.
  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(354u, streams[0].width);
  EXPECT_EQ(250u, streams[0].height);
  EXPECT_EQ(708u, streams[1].width);
  EXPECT_EQ(500u, streams[1].height);
}

TEST(SimulcastTest, GetConfigWithNormalizedResolutionDivisibleBy8) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-NormalizeSimulcastResolution/Enabled-3/");
  FieldTrialBasedConfig trials;

  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 2;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 709, 501, kBitratePriority, kQpMax, !kScreenshare,
      true, trials);

  // Must be divisible by |2 ^ 3|.
  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(352u, streams[0].width);
  EXPECT_EQ(248u, streams[0].height);
  EXPECT_EQ(704u, streams[1].width);
  EXPECT_EQ(496u, streams[1].height);
}

TEST(SimulcastTest, GetConfigForLegacyLayerLimit) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-LegacySimulcastLayerLimit/Enabled/");
  FieldTrialBasedConfig trials;

  const size_t kMinLayers = 1;
  const int kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 320, 180, kBitratePriority, kQpMax, !kScreenshare,
      true, trials);
  EXPECT_EQ(1u, streams.size());

  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 640, 360,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());

  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 1920, 1080,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(3u, streams.size());
}

TEST(SimulcastTest, GetConfigForLegacyLayerLimitWithRequiredHD) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-LegacySimulcastLayerLimit/Enabled/");
  FieldTrialBasedConfig trials;

  const size_t kMinLayers = 3;  // "HD" layer must be present!
  const int kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 320, 180, kBitratePriority, kQpMax, !kScreenshare,
      true, trials);
  EXPECT_EQ(3u, streams.size());

  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 640, 360,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(3u, streams.size());

  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 1920, 1080,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(3u, streams.size());
}

TEST(SimulcastTest, GetConfigForScreenshareSimulcast) {
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 1400, 800, kBitratePriority, kQpMax, kScreenshare,
      true, trials);

  EXPECT_GT(streams.size(), 1u);
  for (size_t i = 0; i < streams.size(); ++i) {
    EXPECT_EQ(1400u, streams[i].width) << "Screen content never scaled.";
    EXPECT_EQ(800u, streams[i].height) << "Screen content never scaled.";
    EXPECT_EQ(kQpMax, streams[i].max_qp);
    EXPECT_TRUE(streams[i].active);
    EXPECT_GT(streams[i].num_temporal_layers, size_t{1});
    EXPECT_GT(streams[i].max_framerate, 0);
    EXPECT_GT(streams[i].min_bitrate_bps, 0);
    EXPECT_GT(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
    EXPECT_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
  }
}

TEST(SimulcastTest, GetConfigForScreenshareSimulcastWithLimitedMaxLayers) {
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 1;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 1400, 800, kBitratePriority, kQpMax, kScreenshare,
      true, trials);

  EXPECT_EQ(kMaxLayers, streams.size());
}

TEST(SimulcastTest, AveragesBitratesForNonStandardResolution) {
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, 900, 800, kBitratePriority, kQpMax, !kScreenshare,
      true, trials);

  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(900u, streams[2].width);
  EXPECT_EQ(800u, streams[2].height);
  EXPECT_EQ(1850000, streams[2].max_bitrate_bps);
  EXPECT_EQ(1850000, streams[2].target_bitrate_bps);
  EXPECT_EQ(475000, streams[2].min_bitrate_bps);
}

TEST(SimulcastTest, BitratesForCloseToStandardResolution) {
  const size_t kMinLayers = 1;
  const size_t kMaxLayers = 3;
  // Resolution very close to 720p in number of pixels
  const size_t kWidth = 1280;
  const size_t kHeight = 716;
  const std::vector<VideoStream> kExpectedNear = GetSimulcastBitrates720p();
  FieldTrialBasedConfig trials;

  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      kMinLayers, kMaxLayers, kWidth, kHeight, kBitratePriority, kQpMax,
      !kScreenshare, true, trials);

  EXPECT_EQ(kMaxLayers, streams.size());
  EXPECT_EQ(kWidth, streams[2].width);
  EXPECT_EQ(kHeight, streams[2].height);
  for (size_t i = 0; i < streams.size(); ++i) {
    EXPECT_NEAR(kExpectedNear[i].max_bitrate_bps, streams[i].max_bitrate_bps,
                20000);
    EXPECT_NEAR(kExpectedNear[i].target_bitrate_bps,
                streams[i].target_bitrate_bps, 20000);
    EXPECT_NEAR(kExpectedNear[i].min_bitrate_bps, streams[i].min_bitrate_bps,
                20000);
  }
}

TEST(SimulcastTest, MaxLayersWithRoundUpDisabled) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-SimulcastLayerLimitRoundUp/max_ratio:0.0/");
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const int kMaxLayers = 3;

  std::vector<VideoStream> streams;
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 960, 540,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(3u, streams.size());
  // <960x540: 2 layers
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 960, 539,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 270,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());
  // <480x270: 1 layer
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 269,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(1u, streams.size());
}

TEST(SimulcastTest, MaxLayersWithDefaultRoundUpRatio) {
  // Default: "WebRTC-SimulcastLayerLimitRoundUp/max_ratio:0.1/"
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const int kMaxLayers = 3;

  std::vector<VideoStream> streams;
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 960, 540,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(3u, streams.size());
  // Lowest cropped height where max layers from higher resolution is used.
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 960, 512,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(3u, streams.size());
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 960, 508,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 270,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());
  // Lowest cropped height where max layers from higher resolution is used.
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 256,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 254,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(1u, streams.size());
}

TEST(SimulcastTest, MaxLayersWithRoundUpRatio) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-SimulcastLayerLimitRoundUp/max_ratio:0.13/");
  FieldTrialBasedConfig trials;
  const size_t kMinLayers = 1;
  const int kMaxLayers = 3;

  std::vector<VideoStream> streams;
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 270,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());
  // Lowest cropped height where max layers from higher resolution is used.
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 252,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(2u, streams.size());
  streams = cricket::GetSimulcastConfig(kMinLayers, kMaxLayers, 480, 250,
                                        kBitratePriority, kQpMax, !kScreenshare,
                                        true, trials);
  EXPECT_EQ(1u, streams.size());
}

TEST(SimulcastTest, BitratesInterpolatedForResBelow180p) {
  // TODO(webrtc:12415): Remove when feature launches.
  test::ScopedFieldTrials field_trials(
      "WebRTC-LowresSimulcastBitrateInterpolation/Enabled/");

  const size_t kMaxLayers = 3;
  FieldTrialBasedConfig trials;

  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      /* min_layers = */ 1, kMaxLayers, /* width = */ 960, /* height = */ 540,
      kBitratePriority, kQpMax, !kScreenshare, true, trials);

  ASSERT_EQ(streams.size(), kMaxLayers);
  EXPECT_EQ(240u, streams[0].width);
  EXPECT_EQ(135u, streams[0].height);
  EXPECT_EQ(streams[0].max_bitrate_bps, 112500);
  EXPECT_EQ(streams[0].target_bitrate_bps, 84375);
  EXPECT_EQ(streams[0].min_bitrate_bps, 30000);
}

TEST(SimulcastTest, BitratesConsistentForVerySmallRes) {
  // TODO(webrtc:12415): Remove when feature launches.
  test::ScopedFieldTrials field_trials(
      "WebRTC-LowresSimulcastBitrateInterpolation/Enabled/");

  FieldTrialBasedConfig trials;

  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      /* min_layers = */ 1, /* max_layers = */ 3, /* width = */ 1,
      /* height = */ 1, kBitratePriority, kQpMax, !kScreenshare, true, trials);

  ASSERT_TRUE(!streams.empty());
  EXPECT_EQ(1u, streams[0].width);
  EXPECT_EQ(1u, streams[0].height);
  EXPECT_EQ(streams[0].max_bitrate_bps, 30000);
  EXPECT_EQ(streams[0].target_bitrate_bps, 30000);
  EXPECT_EQ(streams[0].min_bitrate_bps, 30000);
}

TEST(SimulcastTest,
     BitratesNotInterpolatedForResBelow180pWhenDisabledTrialSet) {
  test::ScopedFieldTrials field_trials(
      "WebRTC-LowresSimulcastBitrateInterpolation/Disabled/");

  const size_t kMaxLayers = 3;
  FieldTrialBasedConfig trials;

  std::vector<VideoStream> streams = cricket::GetSimulcastConfig(
      /* min_layers = */ 1, kMaxLayers, /* width = */ 960, /* height = */ 540,
      kBitratePriority, kQpMax, !kScreenshare, true, trials);

  ASSERT_EQ(streams.size(), kMaxLayers);
  EXPECT_EQ(240u, streams[0].width);
  EXPECT_EQ(135u, streams[0].height);
  EXPECT_EQ(streams[0].max_bitrate_bps, 200000);
  EXPECT_EQ(streams[0].target_bitrate_bps, 150000);
  EXPECT_EQ(streams[0].min_bitrate_bps, 30000);
}

}  // namespace webrtc