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

media / capture / content / capture_resolution_chooser_unittest.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/capture/content/capture_resolution_chooser.h"

#include <stddef.h>

#include <array>
#include <numeric>

#include "base/location.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"

using base::Location;

namespace media {

namespace {

// 16:9 maximum and minimum frame sizes.
constexpr int kMaxFrameWidth = 3840;
constexpr int kMaxFrameHeight = 2160;
constexpr int kMinFrameWidth = 320;
constexpr int kMinFrameHeight = 180;

// Smallest non-empty size for I420 video.
constexpr int kSmallestNonEmptyWidth = 2;
constexpr int kSmallestNonEmptyHeight = 2;

// Checks whether |size| is strictly between (inclusive) |min_size| and
// |max_size| and has the same aspect ratio as |max_size|.
void ExpectIsWithinBoundsAndSameAspectRatio(const Location& location,
                                            const gfx::Size& min_size,
                                            const gfx::Size& max_size,
                                            const gfx::Size& size) {
  SCOPED_TRACE(::testing::Message() << "From here: " << location.ToString());
  EXPECT_LE(min_size.width(), size.width());
  EXPECT_LE(min_size.height(), size.height());
  EXPECT_GE(max_size.width(), size.width());
  EXPECT_GE(max_size.height(), size.height());
  EXPECT_NEAR(static_cast<double>(max_size.width()) / max_size.height(),
              static_cast<double>(size.width()) / size.height(), 0.01);
}

// Test that the correct snapped frame sizes are computed for a |chooser|
// configured with either of the variable-resolution change policies, and are
// correctly found when searched.
void TestSnappedFrameSizes(CaptureResolutionChooser* chooser,
                           const gfx::Size& smallest_size) {
  const std::array<std::array<const int, 2>, 17> kSizes = {{
      {kMaxFrameWidth, kMaxFrameHeight},
      {3520, 1980},
      {3200, 1800},
      {2880, 1620},
      {2560, 1440},
      {2240, 1260},
      {1920, 1080},
      {1760, 990},
      {1600, 900},
      {1440, 810},
      {1280, 720},
      {1120, 630},
      {960, 540},
      {800, 450},
      {640, 360},
      {480, 270},
      {320, 180},
  }};

  const gfx::Size largest_size(kMaxFrameWidth, kMaxFrameHeight);
  chooser->SetSourceSize(largest_size);

  // There should be no size larger than the largest size.
  for (int i = 1; i < 4; ++i) {
    EXPECT_EQ(largest_size,
              chooser->FindLargerFrameSize(largest_size.GetArea(), i));
    EXPECT_EQ(largest_size,
              chooser->FindLargerFrameSize(largest_size.GetArea() * 2, +i));
  }

  // There should be no size smaller than the smallest size.
  for (int i = 1; i < 4; ++i) {
    EXPECT_EQ(smallest_size,
              chooser->FindSmallerFrameSize(smallest_size.GetArea(), i));
    EXPECT_EQ(smallest_size,
              chooser->FindSmallerFrameSize(smallest_size.GetArea() / 2, i));
  }

  // Test the "find Nth lower size" logic.
  for (size_t skips = 1; skips < 4; ++skips) {
    for (size_t i = skips; i < std::size(kSizes); ++i) {
      EXPECT_EQ(
          gfx::Size(kSizes[i][0], kSizes[i][1]),
          chooser->FindSmallerFrameSize(
              gfx::Size(kSizes[i - skips][0], kSizes[i - skips][1]).GetArea(),
              skips));
    }
  }

  // Test the "find Nth higher size" logic.
  for (size_t skips = 1; skips < 4; ++skips) {
    for (size_t i = skips; i < std::size(kSizes); ++i) {
      EXPECT_EQ(gfx::Size(kSizes[i - skips][0], kSizes[i - skips][1]),
                chooser->FindLargerFrameSize(
                    gfx::Size(kSizes[i][0], kSizes[i][1]).GetArea(), skips));
    }
  }

  // Test the "find nearest size" logic.
  for (size_t i = 1; i < std::size(kSizes) - 1; ++i) {
    const gfx::Size size(kSizes[i][0], kSizes[i][1]);
    const int a_somewhat_smaller_area =
        gfx::Size((kSizes[i - 1][0] + 3 * kSizes[i][0]) / 4,
                  (kSizes[i - 1][1] + 3 * kSizes[i][1]) / 4).GetArea();
    EXPECT_EQ(size, chooser->FindNearestFrameSize(a_somewhat_smaller_area));

    const int a_smidge_smaller_area = size.GetArea() - 1;
    EXPECT_EQ(size, chooser->FindNearestFrameSize(a_smidge_smaller_area));

    const int a_smidge_larger_area = size.GetArea() + 1;
    EXPECT_EQ(size, chooser->FindNearestFrameSize(a_smidge_larger_area));

    const int a_somewhat_larger_area =
        gfx::Size((kSizes[i + 1][0] + 3 * kSizes[i][0]) / 4,
                  (kSizes[i + 1][1] + 3 * kSizes[i][1]) / 4).GetArea();
    EXPECT_EQ(size, chooser->FindNearestFrameSize(a_somewhat_larger_area));
  }
}

// Test that setting the target frame area results in the correct capture sizes
// being computed for a |chooser| configured with either of the
// variable-resolution change policies.
void TestTargetedFrameAreas(CaptureResolutionChooser* chooser,
                            const gfx::Size& smallest_size) {
  chooser->SetSourceSize(gfx::Size(1280, 720));

  // The computed capture size cannot be larger than the source size, even
  // though the |chooser| is configured with a larger max frame size.
  chooser->SetTargetFrameArea(kMaxFrameWidth * kMaxFrameHeight);
  EXPECT_EQ(gfx::Size(1280, 720), chooser->capture_size());

  chooser->SetTargetFrameArea(1280 * 720 + 1);
  EXPECT_EQ(gfx::Size(1280, 720), chooser->capture_size());
  chooser->SetTargetFrameArea(1280 * 720 - 1);
  EXPECT_EQ(gfx::Size(1280, 720), chooser->capture_size());

  chooser->SetTargetFrameArea(1120 * 630 + 1);
  EXPECT_EQ(gfx::Size(1120, 630), chooser->capture_size());
  chooser->SetTargetFrameArea(1120 * 630 - 1);
  EXPECT_EQ(gfx::Size(1120, 630), chooser->capture_size());

  chooser->SetTargetFrameArea(800 * 450 + 1);
  EXPECT_EQ(gfx::Size(800, 450), chooser->capture_size());
  chooser->SetTargetFrameArea(800 * 450 - 1);
  EXPECT_EQ(gfx::Size(800, 450), chooser->capture_size());

  chooser->SetTargetFrameArea(640 * 360 + 1);
  EXPECT_EQ(gfx::Size(640, 360), chooser->capture_size());
  chooser->SetTargetFrameArea(640 * 360 - 1);
  EXPECT_EQ(gfx::Size(640, 360), chooser->capture_size());

  chooser->SetTargetFrameArea(smallest_size.GetArea() + 1);
  EXPECT_EQ(smallest_size, chooser->capture_size());
  chooser->SetTargetFrameArea(smallest_size.GetArea() - 1);
  EXPECT_EQ(smallest_size, chooser->capture_size());

  chooser->SetTargetFrameArea(smallest_size.GetArea() / 2);
  EXPECT_EQ(smallest_size, chooser->capture_size());

  chooser->SetTargetFrameArea(0);
  EXPECT_EQ(smallest_size, chooser->capture_size());

  // If the source size has increased, the |chooser| is now permitted to compute
  // higher capture sizes.
  chooser->SetSourceSize(gfx::Size(kMaxFrameWidth, kMaxFrameHeight));
  chooser->SetTargetFrameArea(kMaxFrameWidth * kMaxFrameHeight);
  EXPECT_EQ(gfx::Size(kMaxFrameWidth, kMaxFrameHeight),
            chooser->capture_size());

  chooser->SetTargetFrameArea(3200 * 1800 + 1);
  EXPECT_EQ(gfx::Size(3200, 1800), chooser->capture_size());
  chooser->SetTargetFrameArea(3200 * 1800 - 1);
  EXPECT_EQ(gfx::Size(3200, 1800), chooser->capture_size());

  chooser->SetTargetFrameArea(640 * 360 + 1);
  EXPECT_EQ(gfx::Size(640, 360), chooser->capture_size());
  chooser->SetTargetFrameArea(640 * 360 - 1);
  EXPECT_EQ(gfx::Size(640, 360), chooser->capture_size());

  chooser->SetTargetFrameArea(0);
  EXPECT_EQ(smallest_size, chooser->capture_size());
}

// Determines that there is only one snapped frame size by implication: This
// function searches for the nearest/larger/smaller frame sizes around
// |the_one_size| and checks that there is only ever one result.
void ExpectOnlyOneSnappedFrameSize(const CaptureResolutionChooser& chooser,
                                   const gfx::Size& the_one_size) {
  for (int i = 1; i < 4; ++i) {
    EXPECT_EQ(the_one_size,
              chooser.FindNearestFrameSize(the_one_size.GetArea() * i));
    EXPECT_EQ(the_one_size,
              chooser.FindSmallerFrameSize(the_one_size.GetArea(), i));
    EXPECT_EQ(the_one_size,
              chooser.FindLargerFrameSize(the_one_size.GetArea(), i));
  }
}

}  // namespace

// While clients should always strive to set their own constraints before
// querying the CaptureResolutionChooser, do test that the reasonable "default"
// behavior is exhibited beforehand.
TEST(CaptureResolutionChooserTest, DefaultCaptureSizeIfNeverSetConstraints) {
  CaptureResolutionChooser chooser;
  EXPECT_EQ(CaptureResolutionChooser::kDefaultCaptureSize,
            chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMinFrameWidth, kMinFrameHeight));
  EXPECT_EQ(CaptureResolutionChooser::kDefaultCaptureSize,
            chooser.capture_size());

  chooser.SetSourceSize(gfx::Size());
  EXPECT_EQ(CaptureResolutionChooser::kDefaultCaptureSize,
            chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth, kMaxFrameHeight));
  EXPECT_EQ(CaptureResolutionChooser::kDefaultCaptureSize,
            chooser.capture_size());

  ExpectOnlyOneSnappedFrameSize(chooser,
                                CaptureResolutionChooser::kDefaultCaptureSize);
}

// While clients should always strive to set the source size before querying the
// CaptureResolutionChooser, do test that the reasonable "default" behavior is
// exhibited beforehand.
TEST(CaptureResolutionChooserTest, ReasonableCaptureSizeWhenMissingSourceSize) {
  CaptureResolutionChooser chooser;
  // CaptureResolutionChooser starts out with capture size set to
  // kDefaultCaptureSize.
  EXPECT_EQ(CaptureResolutionChooser::kDefaultCaptureSize,
            chooser.capture_size());

  // Setting constraints around kDefaultCaptureSize means the capture size
  // should remain as kDefaultCaptureSize.
  chooser.SetConstraints(gfx::Size(kMinFrameWidth, kMinFrameHeight),
                         gfx::Size(kMaxFrameWidth, kMaxFrameHeight), false);
  EXPECT_EQ(CaptureResolutionChooser::kDefaultCaptureSize,
            chooser.capture_size());

  // Setting constraints to a fixed size less than kDefaultCaptureSize will
  // change the capture size, to meet the required constraints range.
  chooser.SetConstraints(gfx::Size(kMinFrameWidth, kMinFrameHeight),
                         gfx::Size(kMinFrameWidth, kMinFrameHeight), false);
  EXPECT_EQ(gfx::Size(kMinFrameWidth, kMinFrameHeight), chooser.capture_size());

  // Setting the constraints back to the wide range should not change the
  // capture size, since the new range includes the former capture size.
  chooser.SetConstraints(gfx::Size(kMinFrameWidth, kMinFrameHeight),
                         gfx::Size(kMaxFrameWidth, kMaxFrameHeight), false);
  EXPECT_EQ(gfx::Size(kMinFrameWidth, kMinFrameHeight), chooser.capture_size());

  // Finally, updating the source size to be exactly in the middle of the
  // constraints range should result in the capture size being updated to that
  // same size.
  static constexpr gfx::Size middle_size(std::midpoint(kMinFrameWidth, kMaxFrameWidth),
                              std::midpoint(kMinFrameHeight, kMaxFrameHeight));
  chooser.SetSourceSize(middle_size);
  EXPECT_EQ(middle_size, chooser.capture_size());
}

TEST(CaptureResolutionChooserTest,
     FixedResolutionPolicy_CaptureSizeAlwaysFixed) {
  const gfx::Size the_one_frame_size(kMaxFrameWidth, kMaxFrameHeight);
  CaptureResolutionChooser chooser;
  chooser.SetConstraints(the_one_frame_size, the_one_frame_size, false);
  EXPECT_EQ(the_one_frame_size, chooser.capture_size());

  chooser.SetSourceSize(the_one_frame_size);
  EXPECT_EQ(the_one_frame_size, chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth + 424, kMaxFrameHeight - 101));
  EXPECT_EQ(the_one_frame_size, chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth - 202, kMaxFrameHeight + 56));
  EXPECT_EQ(the_one_frame_size, chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMinFrameWidth, kMinFrameHeight));
  EXPECT_EQ(the_one_frame_size, chooser.capture_size());

  ExpectOnlyOneSnappedFrameSize(chooser, the_one_frame_size);

  // Ensure that changing the target frame area does not change the computed
  // frame size.
  chooser.SetTargetFrameArea(0);
  EXPECT_EQ(the_one_frame_size, chooser.capture_size());
  chooser.SetTargetFrameArea(the_one_frame_size.GetArea() / 2);
  EXPECT_EQ(the_one_frame_size, chooser.capture_size());
}

TEST(CaptureResolutionChooserTest,
     FixedAspectRatioPolicy_CaptureSizeHasSameAspectRatio) {
  const gfx::Size min_size(kMinFrameWidth, kMinFrameHeight);
  const gfx::Size max_size(kMaxFrameWidth, kMaxFrameHeight);
  CaptureResolutionChooser chooser;
  chooser.SetConstraints(min_size, max_size, true);

  // Starting condition.
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  // Max size in --> max size out.
  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth, kMaxFrameHeight));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  // Various source sizes within bounds.
  chooser.SetSourceSize(gfx::Size(640, 480));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(480, 640));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(640, 640));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  // Bad source size results in no update.
  const gfx::Size unchanged_size = chooser.capture_size();
  chooser.SetSourceSize(gfx::Size(0, 0));
  EXPECT_EQ(unchanged_size, chooser.capture_size());

  // Downscaling size (preserving aspect ratio) when source size exceeds the
  // upper bounds.
  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth * 2, kMaxFrameHeight * 2));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth * 2, kMaxFrameHeight));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth, kMaxFrameHeight * 2));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  // Upscaling size (preserving aspect ratio) when source size is under the
  // lower bounds.
  chooser.SetSourceSize(gfx::Size(kMinFrameWidth / 2, kMinFrameHeight / 2));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMinFrameWidth / 2, kMaxFrameHeight));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMinFrameWidth, kMinFrameHeight / 2));
  ExpectIsWithinBoundsAndSameAspectRatio(FROM_HERE, min_size, max_size,
                                         chooser.capture_size());

  // For a chooser configured with the "fixed aspect ratio" policy, the smallest
  // possible computed size is the one with 180 lines of resolution and the same
  // aspect ratio.
  const gfx::Size smallest_size(180 * kMaxFrameWidth / kMaxFrameHeight, 180);

  TestSnappedFrameSizes(&chooser, smallest_size);
  TestTargetedFrameAreas(&chooser, smallest_size);
}

TEST(CaptureResolutionChooserTest,
     AnyWithinLimitPolicy_CaptureSizeIsAnythingWithinLimits) {
  const gfx::Size min_size(kSmallestNonEmptyWidth, kSmallestNonEmptyHeight);
  const gfx::Size max_size(kMaxFrameWidth, kMaxFrameHeight);
  CaptureResolutionChooser chooser;
  chooser.SetConstraints(min_size, max_size, false);

  // Starting condition.
  EXPECT_EQ(CaptureResolutionChooser::kDefaultCaptureSize,
            chooser.capture_size());

  // Max size in --> max size out.
  chooser.SetSourceSize(max_size);
  EXPECT_EQ(max_size, chooser.capture_size());

  // Various source sizes within bounds.
  chooser.SetSourceSize(gfx::Size(640, 480));
  EXPECT_EQ(gfx::Size(640, 480), chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(480, 640));
  EXPECT_EQ(gfx::Size(480, 640), chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(640, 640));
  EXPECT_EQ(gfx::Size(640, 640), chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(2, 2));
  EXPECT_EQ(gfx::Size(2, 2), chooser.capture_size());

  // Bad source size results in no update.
  const gfx::Size unchanged_size = chooser.capture_size();
  chooser.SetSourceSize(gfx::Size(0, 0));
  EXPECT_EQ(unchanged_size, chooser.capture_size());

  // Downscaling size (preserving aspect ratio) when source size exceeds the
  // upper bounds.
  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth * 2, kMaxFrameHeight * 2));
  EXPECT_EQ(max_size, chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth * 2, kMaxFrameHeight));
  EXPECT_EQ(gfx::Size(kMaxFrameWidth, kMaxFrameHeight / 2),
            chooser.capture_size());

  chooser.SetSourceSize(gfx::Size(kMaxFrameWidth, kMaxFrameHeight * 2));
  EXPECT_EQ(gfx::Size(kMaxFrameWidth / 2, kMaxFrameHeight),
            chooser.capture_size());

  // For a chooser configured with the "any within limit" policy, the smallest
  // possible computed size is smallest non-empty snapped size (which is 90
  // lines of resolution) with the same aspect ratio as the maximum size.
  const gfx::Size smallest_size(90 * kMaxFrameWidth / kMaxFrameHeight, 90);

  TestSnappedFrameSizes(&chooser, smallest_size);
  TestTargetedFrameAreas(&chooser, smallest_size);
}

}  // namespace media