blob: 96aed21c6b41e1b655875dfcdd05e80aa9c70938 [file] [log] [blame]
stefan@webrtc.org14b02792015-02-16 12:02:20 +00001/*
2 * Copyright (c) 2015 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_BWE_H_
12#define MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_BWE_H_
stefan@webrtc.org14b02792015-02-16 12:02:20 +000013
terelius8f09f172015-12-15 00:51:54 -080014#include <list>
15#include <map>
stefan@webrtc.org14b02792015-02-16 12:02:20 +000016#include <sstream>
terelius8f09f172015-12-15 00:51:54 -080017#include <string>
stefan@webrtc.org14b02792015-02-16 12:02:20 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/bitrate_controller/include/bitrate_controller.h"
20#include "modules/remote_bitrate_estimator/test/bwe_test_framework.h"
21#include "modules/remote_bitrate_estimator/test/packet.h"
22#include "rtc_base/constructormagic.h"
23#include "rtc_base/gtest_prod_util.h"
stefan@webrtc.org14b02792015-02-16 12:02:20 +000024
25namespace webrtc {
26namespace testing {
27namespace bwe {
28
Cesar Magalhaes9c261f22015-07-15 16:31:18 +020029// Overload map comparator.
30class SequenceNumberOlderThan {
31 public:
32 bool operator()(uint16_t seq_num_1, uint16_t seq_num_2) const {
33 return IsNewerSequenceNumber(seq_num_2, seq_num_1);
34 }
35};
36
37// Holds information for computing global packet loss.
38struct LossAccount {
39 LossAccount() : num_total(0), num_lost(0) {}
40 LossAccount(size_t num_total, size_t num_lost)
41 : num_total(num_total), num_lost(num_lost) {}
42 void Add(LossAccount rhs);
43 void Subtract(LossAccount rhs);
44 float LossRatio();
45 size_t num_total;
46 size_t num_lost;
47};
48
Cesar Magalhaes77cabab2015-06-08 11:29:08 +020049// Holds only essential information about packets to be saved for
50// further use, e.g. for calculating packet loss and receiving rate.
51struct PacketIdentifierNode {
52 PacketIdentifierNode(uint16_t sequence_number,
53 int64_t send_time_ms,
54 int64_t arrival_time_ms,
55 size_t payload_size)
56 : sequence_number(sequence_number),
57 send_time_ms(send_time_ms),
58 arrival_time_ms(arrival_time_ms),
59 payload_size(payload_size) {}
60
61 uint16_t sequence_number;
62 int64_t send_time_ms;
63 int64_t arrival_time_ms;
64 size_t payload_size;
65};
66
67typedef std::list<PacketIdentifierNode*>::iterator PacketNodeIt;
68
69// FIFO implementation for a limited capacity set.
70// Used for keeping the latest arrived packets while avoiding duplicates.
71// Allows efficient insertion, deletion and search.
72class LinkedSet {
73 public:
74 explicit LinkedSet(int capacity) : capacity_(capacity) {}
stefan870eee42015-07-14 03:53:57 -070075 ~LinkedSet();
Cesar Magalhaes77cabab2015-06-08 11:29:08 +020076
77 // If the arriving packet (identified by its sequence number) is already
78 // in the LinkedSet, move its Node to the head of the list. Else, create
79 // a PacketIdentifierNode n_ and then UpdateHead(n_), calling RemoveTail()
80 // if the LinkedSet reached its maximum capacity.
81 void Insert(uint16_t sequence_number,
82 int64_t send_time_ms,
83 int64_t arrival_time_ms,
84 size_t payload_size);
85
Cesar Magalhaes9c261f22015-07-15 16:31:18 +020086 void Insert(PacketIdentifierNode packet_identifier);
87
Cesar Magalhaes77cabab2015-06-08 11:29:08 +020088 PacketNodeIt begin() { return list_.begin(); }
89 PacketNodeIt end() { return list_.end(); }
Cesar Magalhaes9c261f22015-07-15 16:31:18 +020090
91 bool empty() const { return list_.empty(); }
92 size_t size() const { return list_.size(); }
93 size_t capacity() const { return capacity_; }
94
95 uint16_t OldestSeqNumber() const { return empty() ? 0 : map_.begin()->first; }
96 uint16_t NewestSeqNumber() const {
97 return empty() ? 0 : map_.rbegin()->first;
98 }
99
100 void Erase(PacketNodeIt node_it);
Cesar Magalhaes77cabab2015-06-08 11:29:08 +0200101
102 private:
103 // Pop oldest element from the back of the list and remove it from the map.
104 void RemoveTail();
105 // Add new element to the front of the list and insert it in the map.
106 void UpdateHead(PacketIdentifierNode* new_head);
107 size_t capacity_;
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200108 std::map<uint16_t, PacketNodeIt, SequenceNumberOlderThan> map_;
Cesar Magalhaes77cabab2015-06-08 11:29:08 +0200109 std::list<PacketIdentifierNode*> list_;
110};
111
stefandb754382016-08-04 06:42:07 -0700112const int kMinBitrateKbps = 10;
terelius182e4a42016-12-01 07:29:09 -0800113const int kMaxBitrateKbps = 25000;
sprang@webrtc.orgdb8e6052015-02-24 13:24:19 +0000114
stefan@webrtc.org76636842015-02-17 16:03:45 +0000115class BweSender : public Module {
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000116 public:
stefan@webrtc.org76636842015-02-17 16:03:45 +0000117 BweSender() {}
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200118 explicit BweSender(int bitrate_kbps) : bitrate_kbps_(bitrate_kbps) {}
stefan@webrtc.org76636842015-02-17 16:03:45 +0000119 virtual ~BweSender() {}
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000120
121 virtual int GetFeedbackIntervalMs() const = 0;
122 virtual void GiveFeedback(const FeedbackPacket& feedback) = 0;
stefan@webrtc.org4346d922015-03-18 13:40:54 +0000123 virtual void OnPacketsSent(const Packets& packets) = 0;
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000124
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200125 protected:
126 int bitrate_kbps_;
127
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000128 private:
henrikg3c089d72015-09-16 05:37:44 -0700129 RTC_DISALLOW_COPY_AND_ASSIGN(BweSender);
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000130};
131
132class BweReceiver {
133 public:
Cesar Magalhaes77cabab2015-06-08 11:29:08 +0200134 explicit BweReceiver(int flow_id);
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200135 BweReceiver(int flow_id, int64_t window_size_ms);
136
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000137 virtual ~BweReceiver() {}
138
139 virtual void ReceivePacket(int64_t arrival_time_ms,
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200140 const MediaPacket& media_packet);
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000141 virtual FeedbackPacket* GetFeedback(int64_t now_ms) { return NULL; }
142
Cesar Magalhaes77cabab2015-06-08 11:29:08 +0200143 size_t GetSetCapacity() { return received_packets_.capacity(); }
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200144 double BitrateWindowS() const { return rate_counter_.BitrateWindowS(); }
145 uint32_t RecentKbps() const; // Receiving Rate.
146
147 // Computes packet loss during an entire simulation, up to 4 billion packets.
148 float GlobalReceiverPacketLossRatio(); // Plot histogram.
149 float RecentPacketLossRatio(); // Plot dynamics.
Cesar Magalhaes77cabab2015-06-08 11:29:08 +0200150
151 static const int64_t kPacketLossTimeWindowMs = 500;
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200152 static const int64_t kReceivingRateTimeWindowMs = 1000;
Cesar Magalhaes77cabab2015-06-08 11:29:08 +0200153
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000154 protected:
155 int flow_id_;
Cesar Magalhaes77cabab2015-06-08 11:29:08 +0200156 // Deals with packets sent more than once.
157 LinkedSet received_packets_;
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200158 // Used for calculating recent receiving rate.
159 RateCounter rate_counter_;
160
161 private:
162 FRIEND_TEST_ALL_PREFIXES(BweReceiverTest, RecentKbps);
163 FRIEND_TEST_ALL_PREFIXES(BweReceiverTest, Loss);
164
165 void UpdateLoss();
166 void RelieveSetAndUpdateLoss();
167 // Packet loss for packets stored in the LinkedSet, up to 1000 packets.
168 // Used to update global loss account whenever the set is filled and cleared.
169 LossAccount LinkedSetPacketLossRatio();
170
171 // Used for calculating global packet loss ratio.
172 LossAccount loss_account_;
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000173};
174
175enum BandwidthEstimatorType {
176 kNullEstimator,
stefan@webrtc.org76636842015-02-17 16:03:45 +0000177 kNadaEstimator,
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000178 kRembEstimator,
terelius2d81eb32016-10-25 07:04:37 -0700179 kSendSideEstimator,
gnish6dcdf102017-06-05 06:01:26 -0700180 kTcpEstimator,
181 kBbrEstimator
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000182};
183
gnish6dcdf102017-06-05 06:01:26 -0700184const char* const bwe_names[] = {"Null", "NADA", "REMB", "GCC", "TCP", "BBR"};
Cesar Magalhaes9c261f22015-07-15 16:31:18 +0200185
sprang@webrtc.orgdb8e6052015-02-24 13:24:19 +0000186int64_t GetAbsSendTimeInMs(uint32_t abs_send_time);
187
stefan@webrtc.org76636842015-02-17 16:03:45 +0000188BweSender* CreateBweSender(BandwidthEstimatorType estimator,
189 int kbps,
190 BitrateObserver* observer,
191 Clock* clock);
stefan@webrtc.org14b02792015-02-16 12:02:20 +0000192
193BweReceiver* CreateBweReceiver(BandwidthEstimatorType type,
194 int flow_id,
195 bool plot);
196} // namespace bwe
197} // namespace testing
198} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200199#endif // MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_BWE_H_