www.pudn.com > RTP通用开发库(for Linux).rar > rtcpheader.c


/*-------------------------------------------------------------------------
 * rtcpheader.c - rtcpheader
 *-------------------------------------------------------------------------
 */

#include 
#include 

/*------------------------------------------------------------------------
 * rtcpheader - fill in an RTCP header in machine byte order and pad
 * for 32-bit alignment if necessary. Rtcpheader assumes that the caller 
 * has allocated the correct amount of memory to round up so that the
 * message is 32-bit aligned.
 *------------------------------------------------------------------------
 */
int
rtcpheader(struct rtcp *prtcp, int count, unsigned char type, int bytelength)
{
  
	int		pad, i;
  
	if (count > (1 << 5) - 1)
		return ERROR;

	if (bytelength > 1 << 18)
		return ERROR;

	prtcp->rtcp_ver = RTP_CURRVERS;
	prtcp->rtcp_count = count;
	prtcp->rtcp_type = type;
  
	/*
	 * RTCP acket length represented in 4-octet units minus one.
	 */ 
	prtcp->rtcp_length = (bytelength / 4) - 1;

	/*
	 * Determine if the packet must be padded so that it is
	 * 32-bit aligned.
	 */
	pad = (bytelength % 4 > 0 ? 4 - (bytelength % 4) : 0);
	if (pad > 0) {
		prtcp->rtcp_length++;
		prtcp->rtcp_pad = TRUE;
		for (i = 0; i < pad - 1; i++) {
			*((char *) prtcp + bytelength + i) = 0;      
		}
		
		/* 
		 * The last byte of a padded RTCP packet
		 * indicates the number of padding bytes
		 * present.
		 */
		*((char *) prtcp + bytelength + pad - 1) = pad;
	}
	else
		prtcp->rtcp_pad = FALSE;
    
	return OK;
}