summaryrefslogtreecommitdiffstats
path: root/src/rtcpscheduler.cpp
blob: fa8510f8e1139a3f29274844afed336da838e98e (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*

  This file is a part of JRTPLIB
  Copyright (c) 1999-2007 Jori Liesenborgs

  Contact: jori.liesenborgs@gmail.com

  This library was developed at the "Expertisecentrum Digitale Media"
  (http://www.edm.uhasselt.be), a research center of the Hasselt University
  (http://www.uhasselt.be). The library is based upon work done for 
  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

*/

#include "rtcpscheduler.h"
#include "rtpsources.h"
#include "rtpdefines.h"
#include "rtcppacket.h"
#include "rtppacket.h"
#include "rtcpcompoundpacket.h"
#include "rtpsourcedata.h"

#include "rtpdebug.h"

#define RTCPSCHED_MININTERVAL						1.0

RTCPSchedulerParams::RTCPSchedulerParams() : mininterval(RTCP_DEFAULTMININTERVAL)
{
	bandwidth = 1000; // TODO What is a good value here? 
	senderfraction = RTCP_DEFAULTSENDERFRACTION;
	usehalfatstartup = RTCP_DEFAULTHALFATSTARTUP;
	immediatebye = RTCP_DEFAULTIMMEDIATEBYE;
#if (defined(WIN32) || defined(_WIN32_WCE))
	timeinit.Dummy();
#endif // WIN32 || _WIN32_WCE
}

RTCPSchedulerParams::~RTCPSchedulerParams()
{
}

int RTCPSchedulerParams::SetRTCPBandwidth(double bw)
{
	if (bw < 0.0)
		return ERR_RTP_SCHEDPARAMS_INVALIDBANDWIDTH;
	bandwidth = bw;
	return 0;
}

int RTCPSchedulerParams::SetSenderBandwidthFraction(double fraction)
{
	if (fraction < 0.0 || fraction > 1.0)
		return ERR_RTP_SCHEDPARAMS_BADFRACTION;
	senderfraction = fraction;
	return 0;
}

int RTCPSchedulerParams::SetMinimumTransmissionInterval(const RTPTime &t)
{
	double t2 = t.GetDouble();

	if (t2 < RTCPSCHED_MININTERVAL)
		return ERR_RTP_SCHEDPARAMS_BADMINIMUMINTERVAL;

	mininterval = t;
	return 0;
}

RTCPScheduler::RTCPScheduler(RTPSources &s) : sources(s),nextrtcptime(0,0),prevrtcptime(0,0)
{
	Reset();
}

RTCPScheduler::~RTCPScheduler()
{
}

void RTCPScheduler::Reset()
{
	headeroverhead = 0; // user has to set this to an appropriate value
	hassentrtcp = false;
	firstcall = true;
	avgrtcppacksize = 1000; // TODO: what is a good value for this?
	byescheduled = false;
	sendbyenow = false;
}

void RTCPScheduler::AnalyseIncoming(RTCPCompoundPacket &rtcpcomppack)
{
	bool isbye = false;
	RTCPPacket *p;
	
	rtcpcomppack.GotoFirstPacket();
	while (!isbye && ((p = rtcpcomppack.GetNextPacket()) != 0))
	{
		if (p->GetPacketType() == RTCPPacket::BYE)
			isbye = true;
	}
	
	if (!isbye)
	{
		size_t packsize = headeroverhead+rtcpcomppack.GetCompoundPacketLength();
		avgrtcppacksize = (size_t)((1.0/16.0)*((double)packsize)+(15.0/16.0)*((double)avgrtcppacksize));
	}
	else
	{
		if (byescheduled)
		{
			size_t packsize = headeroverhead+rtcpcomppack.GetCompoundPacketLength();
			avgbyepacketsize = (size_t)((1.0/16.0)*((double)packsize)+(15.0/16.0)*((double)avgbyepacketsize));
			byemembers++;
		}
	}
}

void RTCPScheduler::AnalyseOutgoing(RTCPCompoundPacket &rtcpcomppack)
{
	bool isbye = false;
	RTCPPacket *p;
	
	rtcpcomppack.GotoFirstPacket();
	while (!isbye && ((p = rtcpcomppack.GetNextPacket()) != 0))
	{
		if (p->GetPacketType() == RTCPPacket::BYE)
			isbye = true;
	}
	
	if (!isbye)
	{
		size_t packsize = headeroverhead+rtcpcomppack.GetCompoundPacketLength();
		avgrtcppacksize = (size_t)((1.0/16.0)*((double)packsize)+(15.0/16.0)*((double)avgrtcppacksize));
	}

	hassentrtcp = true;
}

RTPTime RTCPScheduler::GetTransmissionDelay()
{
	if (firstcall)
	{
		firstcall = false;
		prevrtcptime = RTPTime::CurrentTime();
		pmembers = sources.GetActiveMemberCount();
		CalculateNextRTCPTime();
	}
	
	RTPTime curtime = RTPTime::CurrentTime();

	if (curtime > nextrtcptime) // packet should be sent
		return RTPTime(0,0);

	RTPTime diff = nextrtcptime;
	diff -= curtime;
	
	return diff;
}

bool RTCPScheduler::IsTime()
{
	if (firstcall)
	{
		firstcall = false;
		prevrtcptime = RTPTime::CurrentTime();
		pmembers = sources.GetActiveMemberCount();
		CalculateNextRTCPTime();
		return false;
	}

	RTPTime currenttime = RTPTime::CurrentTime();

//	// TODO: for debugging
//	double diff = nextrtcptime.GetDouble() - currenttime.GetDouble();
//
//	std::cout << "Delay till next RTCP interval: " << diff << std::endl;
	
	if (currenttime < nextrtcptime) // timer has not yet expired
		return false;

	RTPTime checktime(0,0);
	
	if (!byescheduled)
	{
		bool aresender = false;
		RTPSourceData *srcdat;
		
		if ((srcdat = sources.GetOwnSourceInfo()) != 0)
			aresender = srcdat->IsSender();
		
		checktime = CalculateTransmissionInterval(aresender);
	}
	else
		checktime = CalculateBYETransmissionInterval();
	
//	std::cout << "Calculated checktime: " << checktime.GetDouble() << std::endl;
	
	checktime += prevrtcptime;
	
	if (checktime <= currenttime) // Okay
	{
		byescheduled = false;
		prevrtcptime = currenttime;
		pmembers = sources.GetActiveMemberCount();
		CalculateNextRTCPTime();
		return true;
	}

//	std::cout << "New delay: " << nextrtcptime.GetDouble() - currenttime.GetDouble() << std::endl;
	
	nextrtcptime = checktime;
	pmembers = sources.GetActiveMemberCount();
	
	return false;
}

void RTCPScheduler::CalculateNextRTCPTime()
{
	bool aresender = false;
	RTPSourceData *srcdat;
	
	if ((srcdat = sources.GetOwnSourceInfo()) != 0)
		aresender = srcdat->IsSender();
	
	nextrtcptime = RTPTime::CurrentTime();	
	nextrtcptime += CalculateTransmissionInterval(aresender);
}

RTPTime RTCPScheduler::CalculateDeterministicInterval(bool sender /* = false */)
{
	int numsenders = sources.GetSenderCount();
	int numtotal = sources.GetActiveMemberCount();

//	std::cout << "CalculateDeterministicInterval" << std::endl;
//	std::cout << "  numsenders: " << numsenders << std::endl;
//	std::cout << "  numtotal: " << numtotal << std::endl;

	// Try to avoid division by zero:
	if (numtotal == 0)
		numtotal++;

	double sfraction = ((double)numsenders)/((double)numtotal);
	double C,n;

	if (sfraction <= schedparams.GetSenderBandwidthFraction())
	{
		if (sender)
		{
			C = ((double)avgrtcppacksize)/(schedparams.GetSenderBandwidthFraction()*schedparams.GetRTCPBandwidth());
			n = (double)numsenders;
		}
		else
		{
			C = ((double)avgrtcppacksize)/((1.0-schedparams.GetSenderBandwidthFraction())*schedparams.GetRTCPBandwidth());
			n = (double)(numtotal-numsenders);
		}
	}
	else
	{
		C = ((double)avgrtcppacksize)/schedparams.GetRTCPBandwidth();
		n = (double)numtotal;
	}
	
	RTPTime Tmin = schedparams.GetMinimumTransmissionInterval();
	double tmin = Tmin.GetDouble();
	
	if (!hassentrtcp && schedparams.GetUseHalfAtStartup())
		tmin /= 2.0;

	double ntimesC = n*C;
	double Td = (tmin>ntimesC)?tmin:ntimesC;

	// TODO: for debugging
//	std::cout << "  Td: " << Td << std::endl;

	return RTPTime(Td);
}

RTPTime RTCPScheduler::CalculateTransmissionInterval(bool sender)
{
	RTPTime Td = CalculateDeterministicInterval(sender);
	double td,mul,T;

//	std::cout << "CalculateTransmissionInterval" << std::endl;

	td = Td.GetDouble();
	mul = rtprand.GetRandomDouble()+0.5; // gives random value between 0.5 and 1.5
	T = (td*mul)/1.21828; // see RFC 3550 p 30

//	std::cout << "  Td: " << td << std::endl;
//	std::cout << "  mul: " << mul << std::endl;
//	std::cout << "  T: " << T << std::endl;

	return RTPTime(T);
}

void RTCPScheduler::PerformReverseReconsideration()
{
	if (firstcall)
		return;
	
	double diff1,diff2;
	int members = sources.GetActiveMemberCount();
	
	RTPTime tc = RTPTime::CurrentTime();
	RTPTime tn_min_tc = nextrtcptime;

	if (tn_min_tc > tc)
		tn_min_tc -= tc;
	else
		tn_min_tc = RTPTime(0,0);

//	std::cout << "+tn_min_tc0 " << nextrtcptime.GetDouble()-tc.GetDouble() << std::endl;
//	std::cout << "-tn_min_tc0 " << -nextrtcptime.GetDouble()+tc.GetDouble() << std::endl;
//	std::cout << "tn_min_tc " << tn_min_tc.GetDouble() << std::endl;
	
	RTPTime tc_min_tp = tc;

	if (tc_min_tp > prevrtcptime)
		tc_min_tp -= prevrtcptime;
	else
		tc_min_tp = 0;
	
	if (pmembers == 0) // avoid division by zero
		pmembers++;
	
	diff1 = (((double)members)/((double)pmembers))*tn_min_tc.GetDouble();
	diff2 = (((double)members)/((double)pmembers))*tc_min_tp.GetDouble();

	nextrtcptime = tc;
	prevrtcptime = tc;
	nextrtcptime += RTPTime(diff1);
	prevrtcptime -= RTPTime(diff2);
	
	pmembers = members;
}

void RTCPScheduler::ScheduleBYEPacket(size_t packetsize)
{
	if (byescheduled)
		return;
	
	if (firstcall)
	{
		firstcall = false;
		pmembers = sources.GetActiveMemberCount();
	}

	byescheduled = true;
	avgbyepacketsize = packetsize+headeroverhead;

	// For now, we will always use the BYE backoff algorithm as described in rfc 3550 p 33
	
	byemembers = 1;
	pbyemembers = 1;

	if (schedparams.GetRequestImmediateBYE() && sources.GetActiveMemberCount() < 50) // p 34 (top)
		sendbyenow = true;
	else
		sendbyenow = false;
	
	prevrtcptime = RTPTime::CurrentTime();
	nextrtcptime = prevrtcptime;
	nextrtcptime += CalculateBYETransmissionInterval();
}

void RTCPScheduler::ActiveMemberDecrease()
{
	if (sources.GetActiveMemberCount() < pmembers)
		PerformReverseReconsideration();
}

RTPTime RTCPScheduler::CalculateBYETransmissionInterval()
{
	if (!byescheduled)
		return RTPTime(0,0);
	
	if (sendbyenow)
		return RTPTime(0,0);
	
	double C,n;

	C = ((double)avgbyepacketsize)/((1.0-schedparams.GetSenderBandwidthFraction())*schedparams.GetRTCPBandwidth());
	n = (double)byemembers;
	
	RTPTime Tmin = schedparams.GetMinimumTransmissionInterval();
	double tmin = Tmin.GetDouble();
	
	if (schedparams.GetUseHalfAtStartup())
		tmin /= 2.0;

	double ntimesC = n*C;
	double Td = (tmin>ntimesC)?tmin:ntimesC;

	double mul = rtprand.GetRandomDouble()+0.5; // gives random value between 0.5 and 1.5
	double T = (Td*mul)/1.21828; // see RFC 3550 p 30
	
	return RTPTime(T);
}