blob: 68fbcc02a5fef1a9a50bbe5864e8874867d5ed72 [file] [log] [blame]
Sebastian Jansson3525f862019-05-17 17:44:04 +02001/*
2 * Copyright 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Artem Titov386802e2019-07-05 10:48:17 +020010#include "test/network/feedback_generator.h"
Sebastian Jansson3525f862019-05-17 17:44:04 +020011
12#include "absl/memory/memory.h"
Per Kjellander1ca57b92022-01-19 14:14:02 +010013#include "api/transport/network_types.h"
Sebastian Jansson3525f862019-05-17 17:44:04 +020014#include "rtc_base/checks.h"
15
16namespace webrtc {
17
18FeedbackGeneratorImpl::FeedbackGeneratorImpl(
19 FeedbackGeneratorImpl::Config config)
20 : conf_(config),
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010021 net_(TimeMode::kSimulated),
Sebastian Jansson3525f862019-05-17 17:44:04 +020022 send_link_{new SimulatedNetwork(conf_.send_link)},
23 ret_link_{new SimulatedNetwork(conf_.return_link)},
Sebastian Janssonb13ccc52019-06-07 13:06:00 +020024 route_(this,
25 net_.CreateRoute(
26 {net_.CreateEmulatedNode(absl::WrapUnique(send_link_))}),
27 net_.CreateRoute(
28 {net_.CreateEmulatedNode(absl::WrapUnique(ret_link_))})) {}
Sebastian Jansson3525f862019-05-17 17:44:04 +020029
30Timestamp FeedbackGeneratorImpl::Now() {
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010031 return net_.Now();
Sebastian Jansson3525f862019-05-17 17:44:04 +020032}
33
34void FeedbackGeneratorImpl::Sleep(TimeDelta duration) {
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010035 net_.time_controller()->AdvanceTime(duration);
Sebastian Jansson3525f862019-05-17 17:44:04 +020036}
37
38void FeedbackGeneratorImpl::SendPacket(size_t size) {
39 SentPacket sent;
40 sent.send_time = Now();
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +010041 sent.size = DataSize::Bytes(size);
Sebastian Janssonad9113f2019-08-13 18:18:29 +020042 sent.sequence_number = sequence_number_++;
Per Kjellander1ca57b92022-01-19 14:14:02 +010043 sent_packets_.push(sent);
Sebastian Janssonb13ccc52019-06-07 13:06:00 +020044 route_.SendRequest(size, sent);
Sebastian Jansson3525f862019-05-17 17:44:04 +020045}
46
47std::vector<TransportPacketsFeedback> FeedbackGeneratorImpl::PopFeedback() {
48 std::vector<TransportPacketsFeedback> ret;
49 ret.swap(feedback_);
50 return ret;
51}
52
53void FeedbackGeneratorImpl::SetSendConfig(BuiltInNetworkBehaviorConfig config) {
54 conf_.send_link = config;
55 send_link_->SetConfig(conf_.send_link);
56}
57
58void FeedbackGeneratorImpl::SetReturnConfig(
59 BuiltInNetworkBehaviorConfig config) {
60 conf_.return_link = config;
61 ret_link_->SetConfig(conf_.return_link);
62}
63
64void FeedbackGeneratorImpl::SetSendLinkCapacity(DataRate capacity) {
65 conf_.send_link.link_capacity_kbps = capacity.kbps<int>();
66 send_link_->SetConfig(conf_.send_link);
67}
68
Sebastian Janssonb13ccc52019-06-07 13:06:00 +020069void FeedbackGeneratorImpl::OnRequest(SentPacket packet,
70 Timestamp arrival_time) {
Sebastian Jansson3525f862019-05-17 17:44:04 +020071 PacketResult result;
72 result.sent_packet = packet;
73 result.receive_time = arrival_time;
Per Kjellander1ca57b92022-01-19 14:14:02 +010074 received_packets_.push_back(result);
75 Timestamp first_recv = received_packets_.front().receive_time;
Sebastian Jansson3525f862019-05-17 17:44:04 +020076 if (Now() - first_recv > conf_.feedback_interval) {
Per Kjellander1ca57b92022-01-19 14:14:02 +010077 route_.SendResponse(conf_.feedback_packet_size.bytes<size_t>(),
78 std::move(received_packets_));
79 received_packets_ = {};
Sebastian Jansson3525f862019-05-17 17:44:04 +020080 }
81}
82
Per Kjellander1ca57b92022-01-19 14:14:02 +010083void FeedbackGeneratorImpl::OnResponse(std::vector<PacketResult> packet_results,
Sebastian Janssonb13ccc52019-06-07 13:06:00 +020084 Timestamp arrival_time) {
Per Kjellander1ca57b92022-01-19 14:14:02 +010085 TransportPacketsFeedback feedback;
86 feedback.feedback_time = arrival_time;
87 std::vector<PacketResult>::const_iterator received_packet_iterator =
88 packet_results.begin();
89 while (received_packet_iterator != packet_results.end()) {
90 RTC_DCHECK(!sent_packets_.empty() &&
91 sent_packets_.front().sequence_number <=
92 received_packet_iterator->sent_packet.sequence_number)
93 << "reordering not implemented";
94 if (sent_packets_.front().sequence_number <
95 received_packet_iterator->sent_packet.sequence_number) {
96 // Packet lost.
97 PacketResult lost;
98 lost.sent_packet = sent_packets_.front();
99 feedback.packet_feedbacks.push_back(lost);
100 }
101 if (sent_packets_.front().sequence_number ==
102 received_packet_iterator->sent_packet.sequence_number) {
103 feedback.packet_feedbacks.push_back(*received_packet_iterator);
104 ++received_packet_iterator;
105 }
106 sent_packets_.pop();
107 }
108 feedback_.push_back(feedback);
Sebastian Janssonb13ccc52019-06-07 13:06:00 +0200109}
110
Sebastian Jansson3525f862019-05-17 17:44:04 +0200111} // namespace webrtc