1 /*
2  *  Copyright (c) 2013 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 
11 #ifndef WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_BWE_TEST_H_
12 #define WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_BWE_TEST_H_
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 #include "webrtc/base/constructormagic.h"
19 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
20 #include "webrtc/modules/remote_bitrate_estimator/test/bwe.h"
21 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.h"
22 #include "webrtc/test/gtest.h"
23 
24 namespace webrtc {
25 
26 namespace testing {
27 namespace bwe {
28 
29 class BweReceiver;
30 class PacketReceiver;
31 class PacketSender;
32 
33 class PacketProcessorRunner {
34  public:
35   explicit PacketProcessorRunner(PacketProcessor* processor);
36   ~PacketProcessorRunner();
37 
38   bool RunsProcessor(const PacketProcessor* processor) const;
39   void RunFor(int64_t time_ms, int64_t time_now_ms, Packets* in_out);
40 
41  private:
42   void FindPacketsToProcess(const FlowIds& flow_ids, Packets* in, Packets* out);
43   void QueuePackets(Packets* batch, int64_t end_of_batch_time_us);
44 
45   PacketProcessor* processor_;
46   Packets queue_;
47 };
48 
49 class Link : public PacketProcessorListener {
50  public:
~Link()51   virtual ~Link() {}
52 
53   virtual void AddPacketProcessor(PacketProcessor* processor,
54                                   ProcessorType type);
55   virtual void RemovePacketProcessor(PacketProcessor* processor);
56 
57   void Run(int64_t run_for_ms, int64_t now_ms, Packets* packets);
58 
senders()59   const std::vector<PacketSender*>& senders() { return senders_; }
processors()60   const std::vector<PacketProcessorRunner>& processors() { return processors_; }
61 
62  private:
63   std::vector<PacketSender*> senders_;
64   std::vector<PacketReceiver*> receivers_;
65   std::vector<PacketProcessorRunner> processors_;
66 };
67 
68 class BweTest {
69  public:
70   BweTest();
71   explicit BweTest(bool plot_capacity);
72   ~BweTest();
73 
74   void RunChoke(BandwidthEstimatorType bwe_type,
75                 std::vector<int> capacities_kbps);
76 
77   void RunVariableCapacity1SingleFlow(BandwidthEstimatorType bwe_type);
78   void RunVariableCapacity2MultipleFlows(BandwidthEstimatorType bwe_type,
79                                          size_t num_flows);
80   void RunBidirectionalFlow(BandwidthEstimatorType bwe_type);
81   void RunSelfFairness(BandwidthEstimatorType bwe_type);
82   void RunRoundTripTimeFairness(BandwidthEstimatorType bwe_type);
83   void RunLongTcpFairness(BandwidthEstimatorType bwe_type);
84   void RunMultipleShortTcpFairness(BandwidthEstimatorType bwe_type,
85                                    std::vector<int> tcp_file_sizes_bytes,
86                                    std::vector<int64_t> tcp_starting_times_ms);
87   void RunPauseResumeFlows(BandwidthEstimatorType bwe_type);
88 
89   void RunFairnessTest(BandwidthEstimatorType bwe_type,
90                        size_t num_media_flows,
91                        size_t num_tcp_flows,
92                        int64_t run_time_seconds,
93                        uint32_t capacity_kbps,
94                        int64_t max_delay_ms,
95                        int64_t rtt_ms,
96                        int64_t max_jitter_ms,
97                        const int64_t* offsets_ms);
98 
99   void RunFairnessTest(BandwidthEstimatorType bwe_type,
100                        size_t num_media_flows,
101                        size_t num_tcp_flows,
102                        int64_t run_time_seconds,
103                        uint32_t capacity_kbps,
104                        int64_t max_delay_ms,
105                        int64_t rtt_ms,
106                        int64_t max_jitter_ms,
107                        const int64_t* offsets_ms,
108                        const std::string& title,
109                        const std::string& flow_name);
110 
111   static std::vector<int> GetFileSizesBytes(int num_files);
112   static std::vector<int64_t> GetStartingTimesMs(int num_files);
113 
114  protected:
115   void SetUp();
116 
117   void VerboseLogging(bool enable);
118   void RunFor(int64_t time_ms);
119   std::string GetTestName() const;
120 
121   void PrintResults(double max_throughput_kbps,
122                     Stats<double> throughput_kbps,
123                     int flow_id,
124                     Stats<double> flow_delay_ms,
125                     Stats<double> flow_throughput_kbps);
126 
127   void PrintResults(double max_throughput_kbps,
128                     Stats<double> throughput_kbps,
129                     std::map<int, Stats<double>> flow_delay_ms,
130                     std::map<int, Stats<double>> flow_throughput_kbps);
131 
132   Link downlink_;
133   Link uplink_;
134 
135  private:
136   void FindPacketsToProcess(const FlowIds& flow_ids, Packets* in,
137                             Packets* out);
138   void GiveFeedbackToAffectedSenders(PacketReceiver* receiver);
139 
140   int64_t run_time_ms_;
141   int64_t time_now_ms_;
142   int64_t simulation_interval_ms_;
143   std::vector<Link*> links_;
144   Packets packets_;
145   bool plot_total_available_capacity_;
146 
147   RTC_DISALLOW_COPY_AND_ASSIGN(BweTest);
148 };
149 
150 // Default Evaluation parameters:
151 // Link capacity: 4000ms;
152 // Queueing delay capacity: 300ms.
153 // One-Way propagation delay: 50ms.
154 // Jitter model: Truncated gaussian.
155 // Maximum end-to-end jitter: 30ms = 2*standard_deviation.
156 // Bottleneck queue type: Drop tail.
157 // Path loss ratio: 0%.
158 
159 const int kOneWayDelayMs = 50;
160 const int kMaxQueueingDelayMs = 300;
161 const int kMaxCapacityKbps = 4000;
162 const int kMaxJitterMs = 15;
163 
164 struct DefaultEvaluationFilter {
DefaultEvaluationFilterDefaultEvaluationFilter165   DefaultEvaluationFilter(PacketProcessorListener* listener, int flow_id)
166       : choke(listener, flow_id),
167         delay(listener, flow_id),
168         jitter(listener, flow_id) {
169     SetDefaultParameters();
170   }
171 
DefaultEvaluationFilterDefaultEvaluationFilter172   DefaultEvaluationFilter(PacketProcessorListener* listener,
173                           const FlowIds& flow_ids)
174       : choke(listener, flow_ids),
175         delay(listener, flow_ids),
176         jitter(listener, flow_ids) {
177     SetDefaultParameters();
178   }
179 
SetDefaultParametersDefaultEvaluationFilter180   void SetDefaultParameters() {
181     delay.SetOneWayDelayMs(kOneWayDelayMs);
182     choke.set_max_delay_ms(kMaxQueueingDelayMs);
183     choke.set_capacity_kbps(kMaxCapacityKbps);
184     jitter.SetMaxJitter(kMaxJitterMs);
185   }
186 
187   ChokeFilter choke;
188   DelayFilter delay;
189   JitterFilter jitter;
190 };
191 
192 }  // namespace bwe
193 }  // namespace testing
194 }  // namespace webrtc
195 
196 #endif  // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_BWE_TEST_H_
197