| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef INC_SRT_CONGCTL_H |
| 12 |
#define INC_SRT_CONGCTL_H |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <map> |
| 16 |
#include <string> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
namespace srt { |
| 20 |
|
| 21 |
class CUDT; |
| 22 |
class SrtCongestionControlBase; |
| 23 |
typedef SrtCongestionControlBase* srtcc_create_t(srt::CUDT* parent); |
| 24 |
|
| 25 |
class SrtCongestion |
| 26 |
{ |
| 27 |
// Temporarily changed to linear searching, until this is exposed |
| 28 |
// for a user-defined controller. |
| 29 |
// Note that this is a pointer to function :) |
| 30 |
|
| 31 |
static const size_t N_CONTROLLERS = 2; |
| 32 |
// The first/second is to mimic the map. |
| 33 |
typedef struct { const char* first; srtcc_create_t* second; } NamePtr; |
| 34 |
static NamePtr congctls[N_CONTROLLERS]; |
| 35 |
|
| 36 |
// This is a congctl container. |
| 37 |
SrtCongestionControlBase* congctl; |
| 38 |
size_t selector; |
| 39 |
|
| 40 |
void Check(); |
| 41 |
|
| 42 |
public: |
| 43 |
|
| 44 |
// If you predict to allow something to be done on controller also |
| 45 |
// before it is configured, call this first. If you need it configured, |
| 46 |
// you can rely on Check(). |
| 47 |
bool ready() { return congctl; } |
| 48 |
SrtCongestionControlBase* operator->() { Check(); return congctl; } |
| 49 |
|
| 50 |
// In the beginning it's uninitialized |
| 51 |
SrtCongestion(): congctl(), selector(N_CONTROLLERS) {} |
| 52 |
|
| 53 |
struct IsName |
| 54 |
{ |
| 55 |
const std::string n; |
| 56 |
IsName(const std::string& nn): n(nn) {} |
| 57 |
bool operator()(NamePtr np) { return n == np.first; } |
| 58 |
}; |
| 59 |
|
| 60 |
static NamePtr* find(const std::string& name) |
| 61 |
{ |
| 62 |
NamePtr* end = congctls+N_CONTROLLERS; |
| 63 |
NamePtr* try_selector = std::find_if(congctls, end, IsName(name)); |
| 64 |
return try_selector != end ? try_selector : NULL; |
| 65 |
} |
| 66 |
|
| 67 |
static bool exists(const std::string& name) |
| 68 |
{ |
| 69 |
return find(name); |
| 70 |
} |
| 71 |
|
| 72 |
// You can call select() multiple times, until finally |
| 73 |
// the 'configure' method is called. |
| 74 |
bool select(const std::string& name) |
| 75 |
{ |
| 76 |
NamePtr* try_selector = find(name); |
| 77 |
if (!try_selector) |
| 78 |
return false; |
| 79 |
selector = try_selector - congctls; |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
std::string selected_name() |
| 84 |
{ |
| 85 |
if (selector == N_CONTROLLERS) |
| 86 |
return ""; |
| 87 |
return congctls[selector].first; |
| 88 |
} |
| 89 |
|
| 90 |
// Copy constructor - important when listener-spawning |
| 91 |
// Things being done: |
| 92 |
// 1. The congctl is individual, so don't copy it. Set NULL. |
| 93 |
// 2. The selected name is copied so that it's configured correctly. |
| 94 |
SrtCongestion(const SrtCongestion& source): congctl(), selector(source.selector) {} |
| 95 |
void operator=(const SrtCongestion& source) { congctl = 0; selector = source.selector; } |
| 96 |
|
| 97 |
// This function will be called by the parent CUDT |
| 98 |
// in appropriate time. It should select appropriate |
| 99 |
// congctl basing on the value in selector, then |
| 100 |
// pin oneself in into CUDT for receiving event signals. |
| 101 |
bool configure(srt::CUDT* parent); |
| 102 |
|
| 103 |
// This function will intentionally delete the contained object. |
| 104 |
// This makes future calls to ready() return false. Calling |
| 105 |
// configure on it again will create it again. |
| 106 |
void dispose(); |
| 107 |
|
| 108 |
// Will delete the pinned in congctl object. |
| 109 |
// This must be defined in *.cpp file due to virtual |
| 110 |
// destruction. |
| 111 |
~SrtCongestion(); |
| 112 |
|
| 113 |
enum RexmitMethod |
| 114 |
{ |
| 115 |
SRM_LATEREXMIT, |
| 116 |
SRM_FASTREXMIT |
| 117 |
}; |
| 118 |
|
| 119 |
enum TransAPI |
| 120 |
{ |
| 121 |
STA_MESSAGE = 0x1, // sendmsg/recvmsg functions |
| 122 |
STA_BUFFER = 0x2, // send/recv functions |
| 123 |
STA_FILE = 0x3, // sendfile/recvfile functions |
| 124 |
}; |
| 125 |
|
| 126 |
enum TransDir |
| 127 |
{ |
| 128 |
STAD_RECV = 0, |
| 129 |
STAD_SEND = 1 |
| 130 |
}; |
| 131 |
}; |
| 132 |
|
| 133 |
class CPacket; |
| 134 |
|
| 135 |
class SrtCongestionControlBase |
| 136 |
{ |
| 137 |
protected: |
| 138 |
// Here can be some common fields |
| 139 |
srt::CUDT* m_parent; |
| 140 |
|
| 141 |
double m_dPktSndPeriod; |
| 142 |
double m_dCWndSize; |
| 143 |
|
| 144 |
//int m_iBandwidth; // NOT REQUIRED. Use m_parent->bandwidth() instead. |
| 145 |
double m_dMaxCWndSize; |
| 146 |
|
| 147 |
//int m_iMSS; // NOT REQUIRED. Use m_parent->MSS() instead. |
| 148 |
//int32_t m_iSndCurrSeqNo; // NOT REQUIRED. Use m_parent->sndSeqNo(). |
| 149 |
//int m_iRcvRate; // NOT REQUIRED. Use m_parent->deliveryRate() instead. |
| 150 |
//int m_RTT; // NOT REQUIRED. Use m_parent->SRTT() instead. |
| 151 |
//char* m_pcParam; // Used to access m_llMaxBw. Use m_parent->maxBandwidth() instead. |
| 152 |
|
| 153 |
// Constructor in protected section so that this class is semi-abstract. |
| 154 |
SrtCongestionControlBase(srt::CUDT* parent); |
| 155 |
public: |
| 156 |
|
| 157 |
// This could be also made abstract, but this causes a linkage |
| 158 |
// problem in C++: this would constitute the first virtual method, |
| 159 |
// and C++ compiler uses the location of the first virtual method as the |
| 160 |
// file to which it also emits the virtual call table. When this is |
| 161 |
// abstract, there would have to be simultaneously either defined |
| 162 |
// an empty method in congctl.cpp file (obviously never called), |
| 163 |
// or simply left empty body here. |
| 164 |
virtual ~SrtCongestionControlBase() { } |
| 165 |
|
| 166 |
// All these functions that return values interesting for processing |
| 167 |
// by CUDT can be overridden. Normally they should refer to the fields |
| 168 |
// and these fields should keep the values as a state. |
| 169 |
virtual double pktSndPeriod_us() { return m_dPktSndPeriod; } |
| 170 |
virtual double cgWindowSize() { return m_dCWndSize; } |
| 171 |
virtual double cgWindowMaxSize() { return m_dMaxCWndSize; } |
| 172 |
|
| 173 |
virtual int64_t sndBandwidth() { return 0; } |
| 174 |
|
| 175 |
// If user-defined, will return nonzero value. |
| 176 |
// If not, it will be internally calculated. |
| 177 |
virtual int RTO() { return 0; } |
| 178 |
|
| 179 |
// Maximum number of packets to trigger ACK sending. |
| 180 |
// Specifies the number of packets to receive before sending the ACK. |
| 181 |
// Used by CUDT together with ACKTimeout_us() to trigger ACK packet sending. |
| 182 |
virtual int ACKMaxPackets() const { return 0; } |
| 183 |
|
| 184 |
// Periodical interval to send an ACK, in microseconds. |
| 185 |
// If user-defined, this value will be used to calculate |
| 186 |
// the next ACK time every time ACK is considered to be sent (see CUDT::checkTimers). |
| 187 |
// Otherwise this will be calculated internally in CUDT, normally taken |
| 188 |
// from CUDT::COMM_SYN_INTERVAL_US. |
| 189 |
virtual int ACKTimeout_us() const { return 0; } |
| 190 |
|
| 191 |
// Called when the settings concerning m_llMaxBW were changed. |
| 192 |
// Arg 1: value of CUDT's m_config.m_llMaxBW |
| 193 |
// Arg 2: value calculated out of CUDT's m_config.llInputBW and m_config.iOverheadBW. |
| 194 |
virtual void updateBandwidth(int64_t, int64_t) {} |
| 195 |
|
| 196 |
virtual bool needsQuickACK(const srt::CPacket&) |
| 197 |
{ |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
// Particular controller is allowed to agree or disagree on the use of particular API. |
| 202 |
virtual bool checkTransArgs(SrtCongestion::TransAPI , SrtCongestion::TransDir , const char* /*buffer*/, size_t /*size*/, int /*ttl*/, bool /*inorder*/) |
| 203 |
{ |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
virtual SrtCongestion::RexmitMethod rexmitMethod() = 0; // Implementation enforced. |
| 208 |
|
| 209 |
virtual int64_t updateNAKInterval(int64_t nakint_us, int rcv_speed, size_t loss_length) |
| 210 |
{ |
| 211 |
if (rcv_speed > 0) |
| 212 |
nakint_us += (loss_length * int64_t(1000000) / rcv_speed); |
| 213 |
|
| 214 |
return nakint_us; |
| 215 |
} |
| 216 |
|
| 217 |
virtual int64_t minNAKInterval() |
| 218 |
{ |
| 219 |
return 0; // Leave default |
| 220 |
} |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
} // namespace srt |
| 225 |
|
| 226 |
#endif |