1 /*
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 */
10 #include "test/network/feedback_generator.h"
11
12 #include "absl/memory/memory.h"
13 #include "rtc_base/checks.h"
14
15 namespace webrtc {
16
FeedbackGeneratorImpl(FeedbackGeneratorImpl::Config config)17 FeedbackGeneratorImpl::FeedbackGeneratorImpl(
18 FeedbackGeneratorImpl::Config config)
19 : conf_(config),
20 net_(TimeMode::kSimulated),
21 send_link_{new SimulatedNetwork(conf_.send_link)},
22 ret_link_{new SimulatedNetwork(conf_.return_link)},
23 route_(this,
24 net_.CreateRoute(
25 {net_.CreateEmulatedNode(absl::WrapUnique(send_link_))}),
26 net_.CreateRoute(
27 {net_.CreateEmulatedNode(absl::WrapUnique(ret_link_))})) {}
28
Now()29 Timestamp FeedbackGeneratorImpl::Now() {
30 return net_.Now();
31 }
32
Sleep(TimeDelta duration)33 void FeedbackGeneratorImpl::Sleep(TimeDelta duration) {
34 net_.time_controller()->AdvanceTime(duration);
35 }
36
SendPacket(size_t size)37 void FeedbackGeneratorImpl::SendPacket(size_t size) {
38 SentPacket sent;
39 sent.send_time = Now();
40 sent.size = DataSize::Bytes(size);
41 sent.sequence_number = sequence_number_++;
42 route_.SendRequest(size, sent);
43 }
44
PopFeedback()45 std::vector<TransportPacketsFeedback> FeedbackGeneratorImpl::PopFeedback() {
46 std::vector<TransportPacketsFeedback> ret;
47 ret.swap(feedback_);
48 return ret;
49 }
50
SetSendConfig(BuiltInNetworkBehaviorConfig config)51 void FeedbackGeneratorImpl::SetSendConfig(BuiltInNetworkBehaviorConfig config) {
52 conf_.send_link = config;
53 send_link_->SetConfig(conf_.send_link);
54 }
55
SetReturnConfig(BuiltInNetworkBehaviorConfig config)56 void FeedbackGeneratorImpl::SetReturnConfig(
57 BuiltInNetworkBehaviorConfig config) {
58 conf_.return_link = config;
59 ret_link_->SetConfig(conf_.return_link);
60 }
61
SetSendLinkCapacity(DataRate capacity)62 void FeedbackGeneratorImpl::SetSendLinkCapacity(DataRate capacity) {
63 conf_.send_link.link_capacity_kbps = capacity.kbps<int>();
64 send_link_->SetConfig(conf_.send_link);
65 }
66
OnRequest(SentPacket packet,Timestamp arrival_time)67 void FeedbackGeneratorImpl::OnRequest(SentPacket packet,
68 Timestamp arrival_time) {
69 PacketResult result;
70 result.sent_packet = packet;
71 result.receive_time = arrival_time;
72 builder_.packet_feedbacks.push_back(result);
73 Timestamp first_recv = builder_.packet_feedbacks.front().receive_time;
74 if (Now() - first_recv > conf_.feedback_interval) {
75 route_.SendResponse(conf_.feedback_packet_size.bytes<size_t>(), builder_);
76 builder_ = {};
77 }
78 }
79
OnResponse(TransportPacketsFeedback packet,Timestamp arrival_time)80 void FeedbackGeneratorImpl::OnResponse(TransportPacketsFeedback packet,
81 Timestamp arrival_time) {
82 packet.feedback_time = arrival_time;
83 feedback_.push_back(packet);
84 }
85
86 } // namespace webrtc
87