DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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
/*
 *  Copyright (c) 2016 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 <memory>

#include "logging/rtc_event_log/events/rtc_event_audio_network_adaptation.h"
#include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
#include "modules/audio_coding/audio_network_adaptor/event_log_writer.h"
#include "test/gtest.h"

namespace webrtc {

namespace {

constexpr int kMinBitrateChangeBps = 5000;
constexpr float kMinPacketLossChangeFraction = 0.5;
constexpr float kMinBitrateChangeFraction = 0.25;

constexpr int kHighBitrateBps = 70000;
constexpr int kLowBitrateBps = 10000;
constexpr int kFrameLengthMs = 60;
constexpr bool kEnableFec = true;
constexpr bool kEnableDtx = true;
constexpr float kPacketLossFraction = 0.05f;
constexpr size_t kNumChannels = 1;

MATCHER_P(IsRtcEventAnaConfigEqualTo, config, "") {
  if (arg->GetType() != RtcEvent::Type::AudioNetworkAdaptation) {
    return false;
  }
  auto ana_event = static_cast<RtcEventAudioNetworkAdaptation*>(arg);
  return *ana_event->config_ == config;
}

struct EventLogWriterStates {
  std::unique_ptr<EventLogWriter> event_log_writer;
  std::unique_ptr<testing::StrictMock<MockRtcEventLog>> event_log;
  AudioEncoderRuntimeConfig runtime_config;
};

EventLogWriterStates CreateEventLogWriter() {
  EventLogWriterStates state;
  state.event_log.reset(new testing::StrictMock<MockRtcEventLog>());
  state.event_log_writer.reset(new EventLogWriter(
      state.event_log.get(), kMinBitrateChangeBps, kMinBitrateChangeFraction,
      kMinPacketLossChangeFraction));
  state.runtime_config.bitrate_bps = kHighBitrateBps;
  state.runtime_config.frame_length_ms = kFrameLengthMs;
  state.runtime_config.uplink_packet_loss_fraction = kPacketLossFraction;
  state.runtime_config.enable_fec = kEnableFec;
  state.runtime_config.enable_dtx = kEnableDtx;
  state.runtime_config.num_channels = kNumChannels;
  return state;
}
}  // namespace

TEST(EventLogWriterTest, FirstConfigIsLogged) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, SameConfigIsNotLogged) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogFecStateChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);

  state.runtime_config.enable_fec = !kEnableFec;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogDtxStateChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);

  state.runtime_config.enable_dtx = !kEnableDtx;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogChannelChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);

  state.runtime_config.num_channels = kNumChannels + 1;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogFrameLengthChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);

  state.runtime_config.frame_length_ms = 20;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, DoNotLogSmallBitrateChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  state.runtime_config.bitrate_bps = kHighBitrateBps + kMinBitrateChangeBps - 1;
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogLargeBitrateChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  // At high bitrate, the min fraction rule requires a larger change than the
  // min change rule. We make sure that the min change rule applies.
  RTC_DCHECK_GT(kHighBitrateBps * kMinBitrateChangeFraction,
                kMinBitrateChangeBps);
  state.runtime_config.bitrate_bps = kHighBitrateBps + kMinBitrateChangeBps;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogMinBitrateChangeFractionOnLowBitrateChange) {
  auto state = CreateEventLogWriter();
  state.runtime_config.bitrate_bps = kLowBitrateBps;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  // At high bitrate, the min change rule requires a larger change than the min
  // fraction rule. We make sure that the min fraction rule applies.
  state.runtime_config.bitrate_bps =
      kLowBitrateBps + kLowBitrateBps * kMinBitrateChangeFraction;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, DoNotLogSmallPacketLossFractionChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  state.runtime_config.uplink_packet_loss_fraction =
      kPacketLossFraction + kMinPacketLossChangeFraction * kPacketLossFraction -
      0.001f;
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogLargePacketLossFractionChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  state.runtime_config.uplink_packet_loss_fraction =
      kPacketLossFraction + kMinPacketLossChangeFraction * kPacketLossFraction;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogJustOnceOnMultipleChanges) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  state.runtime_config.uplink_packet_loss_fraction =
      kPacketLossFraction + kMinPacketLossChangeFraction * kPacketLossFraction;
  state.runtime_config.frame_length_ms = 20;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
}

TEST(EventLogWriterTest, LogAfterGradualChange) {
  auto state = CreateEventLogWriter();
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  state.runtime_config.bitrate_bps = kHighBitrateBps + kMinBitrateChangeBps;
  EXPECT_CALL(*state.event_log,
              LogProxy(IsRtcEventAnaConfigEqualTo(state.runtime_config)))
      .Times(1);
  for (int bitrate_bps = kHighBitrateBps;
       bitrate_bps <= kHighBitrateBps + kMinBitrateChangeBps; bitrate_bps++) {
    state.runtime_config.bitrate_bps = bitrate_bps;
    state.event_log_writer->MaybeLogEncoderConfig(state.runtime_config);
  }
}
}  // namespace webrtc