⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtprtcpmodule.cpp

📁 此代码为jrtplib-2.9库文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*  This file is a part of JRTPLIB  Copyright (c) 1999-2004 Jori Liesenborgs  Contact: jori@lumumba.luc.ac.be  This library (JRTPLIB) was partially developed 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 "rtprtcpmodule.h"#include "rtptimeutil.h"#include "rtpdefines.h"#include "rtpconnection.h"#include "rtpsources.h"#include "rtpcontributingsources.h"#include "rtplocalinfo.h"#include "rtprandom.h"#include "rtpsourcedescription.h"#include "rtpsourcedata.h"#include "rtphandlers.h"#include <string.h>/* Class implementation */RTPRTCPModule::RTPRTCPModule(){	initialized = false;	rtpconn = NULL;	sources = NULL;	contribsrcs = NULL;	localinf = NULL;	handlers = NULL;}RTPRTCPModule::~RTPRTCPModule(){}void RTPRTCPModule::Initialize(){	sessbandwidth = RTP_DEFAULT_SESSIONBANDWIDTH;	rtcpfrag = RTP_DEFAULT_RTCPFRAGMENT;	rtcpbandwidth = sessbandwidth*rtcpfrag;	prevrtcptime = time(NULL);	CalcNextRTCPTime();	sentdatasincelastSR = false;		// assume that source is a sender, that there's at least 1 receiver and	// that the average CNAME length is 30	avgrtcpsize = (double)(IP_UDP_HEADERSIZE+sizeof(RTCPHeader)+sizeof(SSRCPrefix)						   +sizeof(RTCPSenderInfo)+sizeof(RTCPReportBlock)						   +sizeof(RTCPHeader)+sizeof(SSRCPrefix)+sizeof(SDESPrefix)						   +30);	numrtcpsent = 0;	rtcpcount = 0;}void RTPRTCPModule::CalcNextRTCPTime(){	double localrtcpbandwidth;	double delay,factor;	unsigned long delay2;	RTPRandom r;		if (sources == NULL)		localrtcpbandwidth = rtcpbandwidth;	else		localrtcpbandwidth = rtcpbandwidth/((double)sources->GetNumberOfParticipants()+1.0); // +1 for this session	delay = (avgrtcpsize*8)/localrtcpbandwidth;	if (delay < RTP_MINIMUM_RTCP_DELAY) // minimum delay of 5 seconds		delay = RTP_MINIMUM_RTCP_DELAY;	factor = (((double)r.RandomByte())/256.0)+0.5; // factor from interval [0.5,1.5[	delay *= factor;	delay2 = (unsigned long)(delay+0.5);	nextrtcptime = prevrtcptime+delay2;}int RTPRTCPModule::RTCPRoutine(unsigned long curtime){	int bytessent;	bool allsdesinfo;	if (!initialized)		return ERR_RTP_RTCPMODULENOTINITIALIZED;	/* Recalc delay requirements */	prevrtcptime = curtime;	CalcNextRTCPTime();	/* Check if we have to send just the CNAME or all required SDES fields */		if (rtcpcount >= RTP_SDESINFO_INTERVAL)	{		allsdesinfo = true;		rtcpcount = 0;	}	else	{		rtcpcount++;		allsdesinfo = false;	}	/* Build and send the packets */	bytessent = BuildAndSendPackets(false,allsdesinfo);	sources->UpdateAllSources();	if (bytessent < 0)		return bytessent;	/* Recalculate average rtcpsize */	if (numrtcpsent < 1000000) // this should be enough loops to make a good average	{		numrtcpsent++;		avgrtcpsize = (((double)(numrtcpsent-1))*avgrtcpsize+((double)bytessent))/((double)numrtcpsent);	}	sentdatasincelastSR = false;	return 0;}int RTPRTCPModule::SendBYE(){	int bytessent;	if (!initialized)		return ERR_RTP_RTCPMODULENOTINITIALIZED;	bytessent = BuildAndSendPackets(true,false);	sources->UpdateAllSources();	if (bytessent < 0)		return bytessent;	return 0;	}int RTPRTCPModule::BuildAndSendPackets(bool bye,bool allsdesinfo){	int status;		maxpacksize = localinf->maxpacksize;	sendcount = 0;	packetoffset = 0;		if ((status = ProcessReports()) < 0)		return status;	if ((status = ProcessSDESInfo(allsdesinfo)) < 0)		return status;	if ((status = ProcessAPPData()) < 0)		return status;	if (bye)	{		if ((status = ProcessBYEMessage()) < 0)			return status;	}	if (packetoffset > 0) // some info still has to be sent	{		if ((status = SendPacketData()) < 0)			return status;	}	return sendcount;}int RTPRTCPModule::ProcessReports(){	int status;	bool firsttime;	bool done,neednewheader,mustsetfields;	struct timeval tv;	int RRsourcesprocessed;	int RRnumsources;	int blockcount;	int length;	RTCPHeader *hdr;	SSRCPrefix *ssrcpref;	RTCPSenderInfo *sendinf;	RTPSourceData *srcdat;		hdr = NULL;	RRsourcesprocessed = 0;	RRnumsources = sources->GetNumberOfSenders();	sources->GotoFirstSender();	firsttime = true;	done = false;	neednewheader = true;	length = 0;	blockcount = 0;	mustsetfields = false;	while (!done)	{		if (firsttime && sentdatasincelastSR) // SR info		{			// here, we can be sure that we've got enough space to			// put the header in, since we're at the start of the packet			firsttime = false;						hdr = (RTCPHeader *)packetbuffer;			hdr->version = RTP_VERSION;			hdr->padding = 0;			hdr->packettype = TYPE_RTCP_SR;			blockcount = 0;			length = ((sizeof(RTCPHeader)+sizeof(SSRCPrefix)+sizeof(RTCPSenderInfo))/sizeof(RTPuint32))-1;						ssrcpref = (SSRCPrefix *)(packetbuffer+sizeof(RTCPHeader));			ssrcpref->ssrc = contribsrcs->localinfo.src; // already in network byte order						sendinf = (RTCPSenderInfo *)(packetbuffer+sizeof(RTCPHeader)+sizeof(SSRCPrefix));			gettimeofday(&tv,NULL);			getntptime(&tv,&(sendinf->NTPlsw),&(sendinf->NTPmsw));			getrtptimestamp(&tv,&(localinf->tsoffsettime),localinf->timestampoffset,localinf->tsunit,&(sendinf->rtptimestamp));			sendinf->senderoctetcount = htonl(localinf->octetcount);			sendinf->senderpacketcount = htonl(localinf->packetcount);						packetoffset = sizeof(RTCPHeader)+sizeof(SSRCPrefix)+sizeof(RTCPSenderInfo);			neednewheader = false;			mustsetfields = true;		}		else if (RRsourcesprocessed >= RRnumsources)			done = true;		else // RR info		{			if (neednewheader)			{				// check if the header and ssrc identifier will fit				if ((int)(packetoffset + sizeof(RTCPHeader) + sizeof(SSRCPrefix)				     + sizeof(RTCPReportBlock)) > maxpacksize)				{					hdr->length = htons(length);					hdr->blockcount = blockcount;					if ((status = SendPacketData()) < 0)						return status;					mustsetfields = false;				}				else				{					hdr = (RTCPHeader *)(packetbuffer+packetoffset);					hdr->version = RTP_VERSION;					hdr->padding = 0;					hdr->packettype = TYPE_RTCP_RR;					blockcount = 0;					length = ((sizeof(RTCPHeader)+sizeof(SSRCPrefix))/sizeof(RTPuint32))-1;										ssrcpref = (SSRCPrefix *)(packetbuffer+packetoffset+sizeof(RTCPHeader));					ssrcpref->ssrc = contribsrcs->localinfo.src; // already in network byte order										neednewheader = false;					packetoffset += sizeof(RTCPHeader)+sizeof(SSRCPrefix);					mustsetfields = true;				}			}			else // add a reception report block, if possible			{				if ((int)(packetoffset + sizeof(RTCPReportBlock)) > maxpacksize)				{					hdr->length = htons(length);					hdr->blockcount = blockcount;					if ((status = SendPacketData()) < 0)						return status;					neednewheader = true;					mustsetfields = false;				}				else				{					srcdat = sources->GetSourceInfo();					GetRRParams(srcdat,(RTCPReportBlock *)(packetbuffer+packetoffset));					blockcount++;					length += sizeof(RTCPReportBlock)/sizeof(RTPuint32);					packetoffset += sizeof(RTCPReportBlock);					RRsourcesprocessed++;					sources->GotoNextSender();					if (blockcount == 31) // max number of reports in one RTCP block					{						hdr->blockcount = 31;						hdr->length = htons(length);						neednewheader = true;						mustsetfields = false;					}					else						mustsetfields = true;				}			}		}	}	if (mustsetfields)	{		hdr->length = htons(length);		hdr->blockcount = blockcount;	}		return 0;}int RTPRTCPModule::ProcessSDESInfo(bool allsdesinfo){	bool needsrchdr,needrtcphdr;	bool increment,haveheader;	int status,sdespos;	int sdeslen,add,mod;	int blockcount,length;	RTCPHeader *hdr;	SSRCPrefix *ssrcpref;	RTPSourceDescription *cursrc;	SDESPrefix *sdespref;	hdr = NULL;	cursrc = &(contribsrcs->localinfo);		sdespos = 0;	needsrchdr = true;	needrtcphdr = true;	length = 0;	blockcount = 0;	haveheader = false;	while (cursrc != NULL)	{		increment = true;		if ((sdespos == (TYPE_SDES_CNAME-1)) || (allsdesinfo && contribsrcs->enabledinfo[sdespos]))		{			add = 0;			if (packetoffset == 0)				add = sizeof(RTCPHeader)+sizeof(SSRCPrefix);			if (needrtcphdr)				add += sizeof(RTCPHeader);			if (needsrchdr)				add += sizeof(SSRCPrefix);			add += sizeof(SDESPrefix);			sdeslen = cursrc->sdesinfolen[sdespos];			if ((packetoffset + add + sdeslen) > (maxpacksize-4))			{				increment = false;				if (haveheader)				{					packetbuffer[packetoffset++] = 0; // end of chunk					length++;					// fill till 32 bit boundary					if ((mod = (length%sizeof(RTPuint32))) != 0)					{						while (mod != sizeof(RTPuint32))						{							packetbuffer[packetoffset++] = 0;							length++;							mod++;						}					}					hdr->length = htons(length/sizeof(RTPuint32)-1);					hdr->blockcount = blockcount;				}				if ((status = SendPacketData()) < 0)					return status;				needrtcphdr = true;				needsrchdr = true;				haveheader = false;			}			else // info fits			{				if (packetoffset == 0)				{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -