-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathNetQueue.cpp
More file actions
149 lines (139 loc) · 3.89 KB
/
NetQueue.cpp
File metadata and controls
149 lines (139 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "NetQueue.h"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
/*
* Implements a simple RTP packet queue
*/
NetQueueItem::NetQueueItem() {
packet = 0;
used = false;
size = 0;
seqNr = 0;
tRelease = 0.0;
tQueue = 0.0;
}
NetQueue::NetQueue(float delay_, float rate_, float jitter_, bool isL4s_) {
for (int n=0; n < NetQueueSize; n++) {
items[n] = new NetQueueItem();
}
head = -1;
tail = 0;
nItems = 0;
rate = rate_;
delay = delay_;
jitter = jitter_;
lastQueueLow = 0.0;
nextTx = 0;
isL4s = isL4s_;
bytesTx = 0;
lastRateUpdateT = 0;
pDrop = 0.0f;
prevRateFrac = 0.0f;
l4sTh = 0.002;//std::max(0.005, 5.0 * 1200 * 8 / rate);
tQueueAvg = 0.0;
}
void NetQueue::insert(float time,
void *rtpPacket,
unsigned int ssrc,
int size,
unsigned short seqNr,
bool isCe) {
int prevHead = head;
head++; if (head == NetQueueSize) head = 0;
items[head]->used = true;
items[head]->packet = rtpPacket;
items[head]->ssrc = ssrc;
items[head]->size = size;
items[head]->seqNr = seqNr;
items[head]->tRelease = time+delay+jitter*(rand()/float(RAND_MAX));
items[head]->tQueue = time;
items[head]->isCe = isCe;
if (rate > 0) {
float delay = size*8.0f / rate;
if (prevHead != -1 && items[prevHead]->used) {
items[head]->tRelease = items[prevHead]->tRelease + delay;
}
else {
items[head]->tRelease = time + delay;
}
}
items[head]->tRelease = std::max(items[head]->tRelease, nextTx);
nextTx = items[head]->tRelease;
}
bool NetQueue::extract(float time,
void *rtpPacket,
unsigned int &ssrc,
int& size,
unsigned short& seqNr,
bool& isCe) {
if (items[tail]->used == false) {
lastQueueLow = time;
return false;
} else {
if (time >= items[tail]->tRelease) {
rtpPacket = items[tail]->packet;
seqNr = items[tail]->seqNr;
ssrc = items[tail]->ssrc;
size = items[tail]->size;
isCe = items[tail]->isCe;
items[tail]->used = false;
/*
* Implement a rudimentary CoDel-ish ECN marker (without the 1/sqrt(N) part)
*/
float qDel = time - items[tail]->tQueue;
if (!isL4s && time - items[tail]->tQueue <= 0.005 && rate > 0.0) {
lastQueueLow = time;
}
if (!isL4s && time - lastQueueLow > 0.1 && rate > 0.0) {
isCe = true;
lastQueueLow = time;
}
tQueueAvg = (time-items[tail]->tQueue);
//if (isL4s && time - items[tail]->tQueue > l4sTh && rate > 0.0) {
if (false && rate > 0)
cerr << tQueueAvg << endl;
if (isL4s && tQueueAvg > l4sTh && rate > 0.0) {
isCe = true;
}
if (false && isL4s && pDrop > 0.0f) {
float rnd = float(rand()) / RAND_MAX;
if (rnd < pDrop) {
isCe = true;
}
}
bytesTx += size;
tail++; if (tail == NetQueueSize) tail = 0;
return true;
}
return false;
}
}
int NetQueue::sizeOfQueue() {
int size = 0;
for (int n=0; n < NetQueueSize; n++) {
if (items[n]->used)
size += items[n]->size;
}
return size;
}
const float rateUpdateT = 0.05;
void NetQueue::updateRate(float time) {
if (time - lastRateUpdateT >= rateUpdateT && rate > 0) {
float dT = time - lastRateUpdateT;
float rateT = bytesTx * 8 / dT;
bytesTx = 0;
lastRateUpdateT = time;
float rateFrac = rateT / rate - 0.9;
rateFrac /= 0.2;
rateFrac = std::max(0.0f, std::min(1.0f, rateFrac));
pDrop = 0.9*pDrop + 0.1*rateFrac;
pDrop = std::min(1.0f, std::max(0.0f, pDrop));
prevRateFrac = rateFrac;
if (false && pDrop > 0)
cerr << pDrop << endl;;
}
}