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
|
/*
* Copyright (c) 2024 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 "test/network/schedulable_network_behavior.h"
#include <algorithm>
#include <cstdint>
#include <utility>
#include "absl/functional/any_invocable.h"
#include "api/sequence_checker.h"
#include "api/task_queue/task_queue_base.h"
#include "api/test/network_emulation/network_config_schedule.pb.h"
#include "api/test/simulated_network.h"
#include "api/units/data_rate.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "rtc_base/checks.h"
#include "rtc_base/task_utils/repeating_task.h"
#include "system_wrappers/include/clock.h"
#include "test/network/simulated_network.h"
namespace webrtc {
namespace {
void UpdateConfigFromSchedule(
const network_behaviour::NetworkConfigScheduleItem& schedule_item,
BuiltInNetworkBehaviorConfig& config) {
if (schedule_item.has_queue_length_packets()) {
config.queue_length_packets = schedule_item.queue_length_packets();
}
if (schedule_item.has_queue_delay_ms()) {
config.queue_delay_ms = schedule_item.queue_delay_ms();
}
if (schedule_item.has_link_capacity_kbps()) {
config.link_capacity =
DataRate::KilobitsPerSec(schedule_item.link_capacity_kbps());
}
if (schedule_item.has_loss_percent()) {
config.loss_percent = schedule_item.loss_percent();
}
if (schedule_item.has_delay_standard_deviation_ms()) {
config.delay_standard_deviation_ms =
schedule_item.delay_standard_deviation_ms();
}
if (schedule_item.has_allow_reordering()) {
config.allow_reordering = schedule_item.allow_reordering();
}
if (schedule_item.has_avg_burst_loss_length()) {
config.avg_burst_loss_length = schedule_item.avg_burst_loss_length();
}
if (schedule_item.has_packet_overhead()) {
config.packet_overhead = schedule_item.packet_overhead();
}
}
BuiltInNetworkBehaviorConfig GetInitialConfig(
const network_behaviour::NetworkConfigSchedule& schedule) {
BuiltInNetworkBehaviorConfig config;
if (!schedule.item().empty()) {
UpdateConfigFromSchedule(schedule.item(0), config);
}
return config;
}
} // namespace
SchedulableNetworkBehavior::SchedulableNetworkBehavior(
network_behaviour::NetworkConfigSchedule schedule,
uint64_t random_seed,
Clock& clock,
absl::AnyInvocable<bool(Timestamp)> start_callback)
: SimulatedNetwork(GetInitialConfig(schedule), random_seed),
schedule_(std::move(schedule)),
start_condition_(std::move(start_callback)),
clock_(clock),
config_(GetInitialConfig(schedule_)) {
if (schedule_.item().size() > 1) {
next_schedule_index_ = 1;
}
sequence_checker_.Detach();
}
bool SchedulableNetworkBehavior::EnqueuePacket(PacketInFlightInfo packet_info) {
RTC_DCHECK_RUN_ON(&sequence_checker_);
if (first_send_time_.IsInfinite() &&
start_condition_(Timestamp::Micros(packet_info.send_time_us))) {
first_send_time_ = Timestamp::Micros(packet_info.send_time_us);
if (schedule_.item().size() > 1) {
RTC_CHECK_LT(next_schedule_index_, schedule_.item().size());
TimeDelta delay =
TimeDelta::Millis(schedule_.item()[next_schedule_index_]
.time_since_first_sent_packet_ms());
schedule_task_ = RepeatingTaskHandle::DelayedStart(
TaskQueueBase::Current(), delay,
[this] { return UpdateConfigAndReschedule(); });
}
}
return SimulatedNetwork::EnqueuePacket(packet_info);
}
TimeDelta SchedulableNetworkBehavior::UpdateConfigAndReschedule() {
RTC_DCHECK_RUN_ON(&sequence_checker_);
Timestamp reschedule_time = clock_.CurrentTime();
RTC_CHECK_LT(next_schedule_index_, schedule_.item().size());
auto next_config = schedule_.item()[next_schedule_index_];
UpdateConfigFromSchedule(next_config, config_);
SimulatedNetwork::SetConfig(config_, reschedule_time);
next_schedule_index_ = ++next_schedule_index_ % schedule_.item().size();
TimeDelta delay = TimeDelta::Zero();
TimeDelta time_since_first_sent_packet = reschedule_time - first_send_time_;
if (next_schedule_index_ != 0) {
delay = std::max(TimeDelta::Millis(schedule_.item()[next_schedule_index_]
.time_since_first_sent_packet_ms()) -
(time_since_first_sent_packet - wrap_time_delta_),
TimeDelta::Zero());
} else if (!schedule_.has_repeat_schedule_after_last_ms()) {
// No more schedule items.
schedule_task_.Stop();
return TimeDelta::Zero(); // This is ignored.
} else {
// Wrap around to the first schedule item.
wrap_time_delta_ +=
TimeDelta::Millis(schedule_.repeat_schedule_after_last_ms()) +
TimeDelta::Millis(schedule_.item()[schedule_.item().size() - 1]
.time_since_first_sent_packet_ms());
delay = TimeDelta::Millis(schedule_.repeat_schedule_after_last_ms());
RTC_DCHECK_GE(delay, TimeDelta::Zero());
}
return delay;
}
} // namespace webrtc
|