| 1 | // |
| 2 | // libtgvoip is free and unencumbered public domain software. |
| 3 | // For more information, see http://unlicense.org or the UNLICENSE file |
| 4 | // you should have received with this source code distribution. |
| 5 | // |
| 6 | |
| 7 | #include "logging.h" |
| 8 | #include "CongestionControl.h" |
| 9 | #include "PrivateDefines.h" |
| 10 | #include "VoIPController.h" |
| 11 | #include "VoIPServerConfig.h" |
| 12 | |
| 13 | #include <cassert> |
| 14 | #include <cstring> |
| 15 | #include <limits> |
| 16 | |
| 17 | using namespace tgvoip; |
| 18 | |
| 19 | CongestionControl::CongestionControl() |
| 20 | : m_cwnd(static_cast<std::size_t>(ServerConfig::GetSharedInstance()->GetInt("audio_congestion_window", 1024))) |
| 21 | { |
| 22 | std::memset(m_inflightPackets.data(), 0, STD_ARRAY_SIZEOF(m_inflightPackets)); |
| 23 | } |
| 24 | |
| 25 | CongestionControl::~CongestionControl() = default; |
| 26 | |
| 27 | std::size_t CongestionControl::GetAcknowledgedDataSize() const |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | double CongestionControl::GetAverageRTT() const |
| 33 | { |
| 34 | return m_rttHistory.NonZeroAverage(); |
| 35 | } |
| 36 | |
| 37 | std::size_t CongestionControl::GetInflightDataSize() const |
| 38 | { |
| 39 | return m_inflightHistory.Average(); |
| 40 | } |
| 41 | |
| 42 | std::size_t CongestionControl::GetCongestionWindow() const |
| 43 | { |
| 44 | return m_cwnd; |
| 45 | } |
| 46 | |
| 47 | double CongestionControl::GetMinimumRTT() const |
| 48 | { |
| 49 | return m_rttHistory.Min(); |
| 50 | } |
| 51 | |
| 52 | void CongestionControl::PacketAcknowledged(std::uint32_t seq) |
| 53 | { |
| 54 | for (tgvoip_congestionctl_packet_t& m_inflightPacket : m_inflightPackets) |
| 55 | { |
| 56 | if (m_inflightPacket.seq == seq && m_inflightPacket.sendTime > 0) |
| 57 | { |
| 58 | m_tmpRtt += (VoIPController::GetCurrentTime() - m_inflightPacket.sendTime); |
| 59 | ++m_tmpRttCount; |
| 60 | m_inflightPacket.sendTime = 0; |
| 61 | m_inflightDataSize -= m_inflightPacket.size; |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void CongestionControl::PacketSent(std::uint32_t seq, std::size_t size) |
| 68 | { |
| 69 | if (!seqgt(seq, m_lastSentSeq) || seq == m_lastSentSeq) |
| 70 | { |
| 71 | LOGW("Duplicate outgoing seq %u", seq); |
| 72 | return; |
| 73 | } |
| 74 | m_lastSentSeq = seq; |
| 75 | double smallestSendTime = std::numeric_limits<double>::infinity(); |
| 76 | tgvoip_congestionctl_packet_t* slot = nullptr; |
| 77 | for (tgvoip_congestionctl_packet_t& m_inflightPacket : m_inflightPackets) |
| 78 | { |
| 79 | if (m_inflightPacket.sendTime == 0) |
| 80 | { |
| 81 | slot = &m_inflightPacket; |
| 82 | break; |
| 83 | } |
| 84 | if (smallestSendTime > m_inflightPacket.sendTime) |
| 85 | { |
| 86 | slot = &m_inflightPacket; |
| 87 | smallestSendTime = slot->sendTime; |
| 88 | } |
| 89 | } |
| 90 | assert(slot != nullptr); |
| 91 | if (slot->sendTime > 0) |
| 92 | { |
| 93 | m_inflightDataSize -= slot->size; |
| 94 | ++m_lossCount; |
| 95 | LOGD("Packet with seq %u was not acknowledged", slot->seq); |
| 96 | } |
| 97 | slot->seq = seq; |
| 98 | slot->size = size; |
| 99 | slot->sendTime = VoIPController::GetCurrentTime(); |
| 100 | m_inflightDataSize += size; |
| 101 | } |
| 102 | |
| 103 | void CongestionControl::PacketLost(std::uint32_t seq) |
| 104 | { |
| 105 | for (tgvoip_congestionctl_packet_t& m_inflightPacket : m_inflightPackets) |
| 106 | { |
| 107 | if (m_inflightPacket.seq == seq && m_inflightPacket.sendTime > 0) |
| 108 | { |
| 109 | m_inflightPacket.sendTime = 0; |
| 110 | m_inflightDataSize -= m_inflightPacket.size; |
| 111 | ++m_lossCount; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void CongestionControl::Tick() |
| 118 | { |
| 119 | ++m_tickCount; |
| 120 | if (m_tmpRttCount > 0) |
| 121 | { |
| 122 | m_rttHistory.Add(m_tmpRtt / m_tmpRttCount); |
| 123 | m_tmpRtt = 0; |
| 124 | m_tmpRttCount = 0; |
| 125 | } |
| 126 | for (tgvoip_congestionctl_packet_t& m_inflightPacket : m_inflightPackets) |
| 127 | { |
| 128 | if (m_inflightPacket.sendTime != 0 && VoIPController::GetCurrentTime() - m_inflightPacket.sendTime > 2) |
| 129 | { |
| 130 | m_inflightPacket.sendTime = 0; |
| 131 | m_inflightDataSize -= m_inflightPacket.size; |
| 132 | ++m_lossCount; |
| 133 | LOGD("Packet with seq %u was not acknowledged", m_inflightPacket.seq); |
| 134 | } |
| 135 | } |
| 136 | m_inflightHistory.Add(m_inflightDataSize); |
| 137 | } |
| 138 | |
| 139 | ConctlAct CongestionControl::GetBandwidthControlAction() const |
| 140 | { |
| 141 | if (VoIPController::GetCurrentTime() - m_lastActionTime < 1) |
| 142 | return ConctlAct::NONE; |
| 143 | std::size_t inflightAvg = GetInflightDataSize(); |
| 144 | std::size_t max = m_cwnd + m_cwnd / 10; |
| 145 | std::size_t min = m_cwnd - m_cwnd / 10; |
| 146 | if (inflightAvg < min) |
| 147 | { |
| 148 | m_lastActionTime = VoIPController::GetCurrentTime(); |
| 149 | return ConctlAct::INCREASE; |
| 150 | } |
| 151 | if (inflightAvg > max) |
| 152 | { |
| 153 | m_lastActionTime = VoIPController::GetCurrentTime(); |
| 154 | return ConctlAct::DECREASE; |
| 155 | } |
| 156 | return ConctlAct::NONE; |
| 157 | } |
| 158 | |
| 159 | std::uint32_t CongestionControl::GetSendLossCount() const |
| 160 | { |
| 161 | return m_lossCount; |
| 162 | } |
| 163 | |