aboutsummaryrefslogtreecommitdiff
path: root/modules/congestion_controller/network_control/test/network_control_tester.cc
blob: 0fe6308384d2ca06459d9074fb78840f2686d10f (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
/*
 *  Copyright (c) 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 "modules/congestion_controller/network_control/test/network_control_tester.h"
#include "modules/congestion_controller/network_control/test/network_ostream_operators.h"

#include <algorithm>

#include "modules/congestion_controller/network_control/include/network_control.h"
#include "rtc_base/logging.h"
namespace webrtc {
namespace test {

NetworkControlState::NetworkControlState() = default;
NetworkControlState::~NetworkControlState() = default;
NetworkControlState::NetworkControlState(const NetworkControlState&) = default;

NetworkControlCacher::NetworkControlCacher() = default;
NetworkControlCacher::~NetworkControlCacher() = default;

void NetworkControlCacher::OnCongestionWindow(CongestionWindow msg) {
  current_state_.congestion_window = msg;
  RTC_LOG(LS_INFO) << "Received window=" << msg.data_window << "\n";
}
void NetworkControlCacher::OnPacerConfig(PacerConfig msg) {
  current_state_.pacer_config = msg;
  RTC_LOG(LS_INFO) << "Received pacing at:" << msg.at_time
                   << ": rate=" << msg.data_rate() << "\n";
}
void NetworkControlCacher::OnProbeClusterConfig(ProbeClusterConfig msg) {
  current_state_.probe_config = msg;
  RTC_LOG(LS_INFO) << "Received probe at:" << msg.at_time
                   << ": target=" << msg.target_data_rate << "\n";
}
void NetworkControlCacher::OnTargetTransferRate(TargetTransferRate msg) {
  current_state_.target_rate = msg;
  RTC_LOG(LS_INFO) << "Received target at:" << msg.at_time
                   << ": rate=" << msg.target_rate << "\n";
}

SentPacket SimpleTargetRateProducer::ProduceNext(
    const NetworkControlState& cache,
    Timestamp current_time,
    TimeDelta time_delta) {
  DataRate actual_send_rate =
      std::max(cache.target_rate->target_rate, cache.pacer_config->pad_rate());
  SentPacket packet;
  packet.send_time = current_time;
  packet.size = time_delta * actual_send_rate;
  return packet;
}

FeedbackBasedNetworkControllerTester::FeedbackBasedNetworkControllerTester(
    NetworkControllerFactoryInterface* factory,
    NetworkControllerConfig initial_config)
    : current_time_(Timestamp::seconds(100000)),
      accumulated_delay_(TimeDelta::ms(0)) {
  initial_config.constraints.at_time = current_time_;
  controller_ = factory->Create(&cacher_, initial_config);
  process_interval_ = factory->GetProcessInterval();
}

FeedbackBasedNetworkControllerTester::~FeedbackBasedNetworkControllerTester() =
    default;

PacketResult FeedbackBasedNetworkControllerTester::SimulateSend(
    SentPacket packet,
    TimeDelta time_delta,
    TimeDelta propagation_delay,
    DataRate actual_bandwidth) {
  TimeDelta bandwidth_delay = packet.size / actual_bandwidth;
  accumulated_delay_ =
      std::max(accumulated_delay_ - time_delta, TimeDelta::Zero());
  accumulated_delay_ += bandwidth_delay;
  TimeDelta total_delay = propagation_delay + accumulated_delay_;

  PacketResult result;
  result.sent_packet = packet;
  result.receive_time = packet.send_time + total_delay;
  return result;
}

void FeedbackBasedNetworkControllerTester::RunSimulation(
    TimeDelta duration,
    TimeDelta packet_interval,
    DataRate actual_bandwidth,
    TimeDelta propagation_delay,
    PacketProducer next_packet) {
  Timestamp start_time = current_time_;
  Timestamp last_process_time = current_time_;
  while (current_time_ - start_time < duration) {
    bool send_packet = true;
    NetworkControlState control_state = cacher_.GetState();

    if (control_state.congestion_window &&
        control_state.congestion_window->enabled) {
      DataSize data_in_flight = DataSize::Zero();
      for (PacketResult& packet : outstanding_packets_)
        data_in_flight += packet.sent_packet->size;
      if (data_in_flight > control_state.congestion_window->data_window)
        send_packet = false;
    }

    if (send_packet) {
      SentPacket sent_packet =
          next_packet(cacher_.GetState(), current_time_, packet_interval);
      controller_->OnSentPacket(sent_packet);
      outstanding_packets_.push_back(SimulateSend(
          sent_packet, packet_interval, propagation_delay, actual_bandwidth));
    }

    if (outstanding_packets_.size() >= 2 &&
        current_time_ >=
            outstanding_packets_[1].receive_time + propagation_delay) {
      TransportPacketsFeedback feedback;
      feedback.prior_in_flight = DataSize::Zero();
      for (PacketResult& packet : outstanding_packets_)
        feedback.prior_in_flight += packet.sent_packet->size;
      while (!outstanding_packets_.empty() &&
             current_time_ >= outstanding_packets_.front().receive_time +
                                  propagation_delay) {
        feedback.packet_feedbacks.push_back(outstanding_packets_.front());
        outstanding_packets_.pop_front();
      }
      feedback.feedback_time =
          feedback.packet_feedbacks.back().receive_time + propagation_delay;
      feedback.data_in_flight = DataSize::Zero();
      for (PacketResult& packet : outstanding_packets_)
        feedback.data_in_flight += packet.sent_packet->size;
      controller_->OnTransportPacketsFeedback(feedback);
    }
    current_time_ += packet_interval;
    if (current_time_ - last_process_time > process_interval_) {
      ProcessInterval interval_msg;
      interval_msg.at_time = current_time_;
      controller_->OnProcessInterval(interval_msg);
    }
  }
}

}  // namespace test
}  // namespace webrtc