| 6 |
| 7 | #include "CongestionControl.h" |
| 8 | #include "VoIPController.h" |
| 9 | #include "logging.h" |
| 10 | #include "VoIPServerConfig.h" |
| 11 | #include "PrivateDefines.h" |
| 12 | #include <math.h> |
| 13 | #include <assert.h> |
| 14 |
| 15 | using namespace tgvoip; |
| 16 |
| 17 | CongestionControl::CongestionControl(){ |
| 18 | memset(inflightPackets, 0, sizeof(inflightPackets)); |
| 19 | tmpRtt=0; |
| 20 | tmpRttCount=0; |
| 21 | lastSentSeq=0; |
| 22 | lastActionTime=0; |
| 23 | lastActionRtt=0; |
| 24 | stateTransitionTime=0; |
| 25 | inflightDataSize=0; |
| 26 | lossCount=0; |
| 27 | cwnd=(size_t) ServerConfig::GetSharedInstance()->GetInt("audio_congestion_window", 1024); |
| 28 | } |
| 29 |
| 30 | CongestionControl::~CongestionControl(){ |
| 31 | } |
| 32 |
| 33 | size_t CongestionControl::GetAcknowledgedDataSize(){ |
| 34 | return 0; |
| 35 | } |
| 36 |
| 37 | double CongestionControl::GetAverageRTT(){ |
| 38 | return rttHistory.NonZeroAverage(); |
| 39 | } |
| 40 |
| 41 | size_t CongestionControl::GetInflightDataSize(){ |
| 42 | return inflightHistory.Average(); |
| 43 | } |
| 44 |
| 45 |
| 46 | size_t CongestionControl::GetCongestionWindow(){ |
| 47 | return cwnd; |
| 48 | } |
| 49 |
| 50 | double CongestionControl::GetMinimumRTT(){ |
| 51 | return rttHistory.Min(); |
| 52 | } |
| 53 |
| 54 | void CongestionControl::PacketAcknowledged(uint32_t seq){ |
| 55 | MutexGuard sync(mutex); |
| 56 | int i; |
| 57 | for(i=0;i<100;i++){ |
| 58 | if(inflightPackets[i].seq==seq && inflightPackets[i].sendTime>0){ |
| 59 | tmpRtt+=(VoIPController::GetCurrentTime()-inflightPackets[i].sendTime); |
| 60 | tmpRttCount++; |
| 61 | inflightPackets[i].sendTime=0; |
| 62 | inflightDataSize-=inflightPackets[i].size; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 |
| 68 | void CongestionControl::PacketSent(uint32_t seq, size_t size){ |
| 69 | if(!seqgt(seq, lastSentSeq) || seq==lastSentSeq){ |
| 70 | LOGW("Duplicate outgoing seq %u", seq); |
| 71 | return; |
| 72 | } |
| 73 | lastSentSeq=seq; |
| 74 | MutexGuard sync(mutex); |
| 75 | double smallestSendTime=INFINITY; |
| 76 | tgvoip_congestionctl_packet_t* slot=NULL; |
| 77 | int i; |
| 78 | for(i=0;i<100;i++){ |
| 79 | if(inflightPackets[i].sendTime==0){ |
| 80 | slot=&inflightPackets[i]; |
| 81 | break; |
| 82 | } |
| 83 | if(smallestSendTime>inflightPackets[i].sendTime){ |
| 84 | slot=&inflightPackets[i]; |
| 85 | smallestSendTime=slot->sendTime; |
| 86 | } |
| 87 | } |
| 88 | assert(slot!=NULL); |
| 89 | if(slot->sendTime>0){ |
| 90 | inflightDataSize-=slot->size; |
| 91 | lossCount++; |
| 92 | LOGD("Packet with seq %u was not acknowledged", slot->seq); |
| 93 | } |
| 94 | slot->seq=seq; |
| 95 | slot->size=size; |
| 96 | slot->sendTime=VoIPController::GetCurrentTime(); |
| 97 | inflightDataSize+=size; |
| 98 | } |
| 99 |
| 100 |
| 101 | void CongestionControl::Tick(){ |
| 102 | tickCount++; |
| 103 | MutexGuard sync(mutex); |
| 104 | if(tmpRttCount>0){ |
| 105 | rttHistory.Add(tmpRtt/tmpRttCount); |
| 106 | tmpRtt=0; |
| 107 | tmpRttCount=0; |
| 108 | } |
| 109 | int i; |
| 110 | for(i=0;i<100;i++){ |
| 111 | if(inflightPackets[i].sendTime!=0 && VoIPController::GetCurrentTime()-inflightPackets[i].sendTime>2){ |
| 112 | inflightPackets[i].sendTime=0; |
| 113 | inflightDataSize-=inflightPackets[i].size; |
| 114 | lossCount++; |
| 115 | LOGD("Packet with seq %u was not acknowledged", inflightPackets[i].seq); |
| 116 | } |
| 117 | } |
| 118 | inflightHistory.Add(inflightDataSize); |
| 119 | } |
| 120 |
| 121 |
| 122 | int CongestionControl::GetBandwidthControlAction(){ |
| 123 | if(VoIPController::GetCurrentTime()-lastActionTime<1) |
| 124 | return TGVOIP_CONCTL_ACT_NONE; |
| 125 | size_t inflightAvg=GetInflightDataSize(); |
| 126 | size_t max=cwnd+cwnd/10; |
| 127 | size_t min=cwnd-cwnd/10; |
| 128 | if(inflightAvg<min){ |
| 129 | lastActionTime=VoIPController::GetCurrentTime(); |
| 130 | return TGVOIP_CONCTL_ACT_INCREASE; |
| 131 | } |
| 132 | if(inflightAvg>max){ |
| 133 | lastActionTime=VoIPController::GetCurrentTime(); |
| 134 | return TGVOIP_CONCTL_ACT_DECREASE; |
| 135 | } |
| 136 | return TGVOIP_CONCTL_ACT_NONE; |
| 137 | } |
| 138 |
| 139 |
| 140 | uint32_t CongestionControl::GetSendLossCount(){ |
| 141 | return lossCount; |
| 142 | } |
| 143 |