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

media / cast / sender / video_bitrate_suggester_unittest.cc [blame]

// Copyright 2023 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/cast/sender/video_bitrate_suggester.h"

#include <memory>
#include <numeric>
#include <optional>
#include <utility>

#include "base/functional/bind.h"
#include "base/test/scoped_feature_list.h"
#include "media/base/media_switches.h"
#include "media/base/video_codecs.h"
#include "media/cast/cast_config.h"
#include "media/cast/cast_environment.h"
#include "media/cast/common/openscreen_conversion_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media::cast {
namespace {

constexpr uint32_t kFirstSsrc = 35535;
constexpr int kRtpTimebase = 9000;
constexpr char kAesSecretKey[] = "65386FD9BCC30BC7FB6A4DD1D3B0FA5E";
constexpr char kAesIvMask[] = "64A6AAC2821880145271BB15B0188821";

static const FrameSenderConfig kVideoConfig{
    kFirstSsrc + 2,
    kFirstSsrc + 3,
    base::Milliseconds(100),
    kDefaultTargetPlayoutDelay,
    /* use_hardware_encoder= */ false,
    kRtpTimebase,
    /* channels = */ 1,
    kDefaultMaxVideoBitrate,
    kDefaultMinVideoBitrate,
    std::midpoint<int>(kDefaultMinVideoBitrate, kDefaultMaxVideoBitrate),
    kDefaultMaxFrameRate,
    kAesSecretKey,
    kAesIvMask,
    VideoCodecParams(VideoCodec::kVP8),
    std::nullopt};
static const openscreen::cast::SessionConfig kOpenscreenVideoConfig =
    ToOpenscreenSessionConfig(kVideoConfig, /* is_pli_enabled= */ true);

}  // namespace

class VideoBitrateSuggesterTest : public ::testing::Test {
 public:
  int get_suggested_bitrate() { return suggested_bitrate_; }

 protected:
  VideoBitrateSuggesterTest() {
    video_bitrate_suggester_ = std::make_unique<VideoBitrateSuggester>(
        kVideoConfig,
        base::BindRepeating(&VideoBitrateSuggesterTest::get_suggested_bitrate,
                            // Safe because we destroy the audio sender before
                            // destroying `this`.
                            base::Unretained(this)));
  }

  void RecordShouldDropNextFrame(bool should_drop) {
    video_bitrate_suggester_->RecordShouldDropNextFrame(should_drop);
  }

  void set_suggested_bitrate(int bitrate) { suggested_bitrate_ = bitrate; }

  VideoBitrateSuggester& video_bitrate_suggester() {
    return *video_bitrate_suggester_;
  }

  void UseExponentialAlgorithm() {
    feature_list_.InitAndEnableFeature(
        media::kCastStreamingExponentialVideoBitrateAlgorithm);
  }

  void UseLinearAlgorithm() {
    feature_list_.InitAndDisableFeature(
        media::kCastStreamingExponentialVideoBitrateAlgorithm);
  }

 private:
  std::unique_ptr<VideoBitrateSuggester> video_bitrate_suggester_;
  base::test::ScopedFeatureList feature_list_;
  int suggested_bitrate_ = 0;
};

TEST_F(VideoBitrateSuggesterTest,
       SuggestsBitratesCorrectlyWithExponentialAlgorithm) {
  UseExponentialAlgorithm();

  // We should start with the maximum video bitrate.
  set_suggested_bitrate(5000001);
  EXPECT_EQ(5000000, video_bitrate_suggester().GetSuggestedBitrate());

  // After a period with multiple frame drops, this should go down.
  RecordShouldDropNextFrame(true);
  RecordShouldDropNextFrame(true);
  for (int i = 0; i < 29; ++i) {
    RecordShouldDropNextFrame(false);
  }

  // It should now go down.
  EXPECT_EQ(4000000, video_bitrate_suggester().GetSuggestedBitrate());

  // It should continue to go down to the minimum as long as frames are being
  // dropped.
  int last_suggestion = 4685120;
  for (int i = 0; i < 12; ++i) {
    RecordShouldDropNextFrame(true);
    for (int j = 0; j < 29; ++j) {
      RecordShouldDropNextFrame(false);
    }

    // It should drop every time.
    const int suggestion = video_bitrate_suggester().GetSuggestedBitrate();
    EXPECT_LT(suggestion, last_suggestion);
    last_suggestion = suggestion;
  }

  // And then stabilize at the bottom.
  EXPECT_EQ(300000, video_bitrate_suggester().GetSuggestedBitrate());

  // It should increase once we stop dropping frames.
  last_suggestion = 300000;
  for (int i = 0; i < 30; ++i) {
    for (int j = 0; j < 30; ++j) {
      RecordShouldDropNextFrame(false);
    }
    const int suggestion = video_bitrate_suggester().GetSuggestedBitrate();
    EXPECT_GT(suggestion, last_suggestion);
    last_suggestion = suggestion;
  }

  // And stop at the maximum.
  EXPECT_EQ(5000000, video_bitrate_suggester().GetSuggestedBitrate());

  // Finally, it should cap at the bitrate suggested by Open Screen.
  set_suggested_bitrate(4998374);
  EXPECT_EQ(4998374, video_bitrate_suggester().GetSuggestedBitrate());
}

TEST_F(VideoBitrateSuggesterTest,
       SuggestsBitratesCorrectlyWithLinearAlgorithm) {
  UseLinearAlgorithm();

  // We should start with the maximum video bitrate.
  set_suggested_bitrate(5000001);
  EXPECT_EQ(5000000, video_bitrate_suggester().GetSuggestedBitrate());

  // After a period with multiple frame drops, this should go down.
  RecordShouldDropNextFrame(true);
  RecordShouldDropNextFrame(true);
  for (int i = 0; i < 99; ++i) {
    RecordShouldDropNextFrame(false);
  }

  // It should now go down.
  EXPECT_EQ(4412500, video_bitrate_suggester().GetSuggestedBitrate());

  // It should continue to go down to the minimum as long as frames are being
  // dropped.
  int last_suggestion = 4412500;
  for (int i = 0; i < 7; ++i) {
    RecordShouldDropNextFrame(true);
    for (int j = 0; j < 99; ++j) {
      RecordShouldDropNextFrame(false);
    }

    // It should drop every time.
    const int suggestion = video_bitrate_suggester().GetSuggestedBitrate();
    EXPECT_LT(suggestion, last_suggestion);
    last_suggestion = suggestion;
  }

  // And then stabilize at the bottom.
  EXPECT_EQ(300000, video_bitrate_suggester().GetSuggestedBitrate());

  // It should increase once we stop dropping frames.
  last_suggestion = 300000;
  for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 100; ++j) {
      RecordShouldDropNextFrame(false);
    }
    const int suggestion = video_bitrate_suggester().GetSuggestedBitrate();
    EXPECT_GT(suggestion, last_suggestion);
    last_suggestion = suggestion;
  }

  // And stop at the maximum.
  EXPECT_EQ(5000000, video_bitrate_suggester().GetSuggestedBitrate());

  // Finally, it should cap at the bitrate suggested by Open Screen.
  set_suggested_bitrate(4998374);
  EXPECT_EQ(4998374, video_bitrate_suggester().GetSuggestedBitrate());
}
}  // namespace media::cast