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

📄 rtp.c

📁 一个关于 RTP的例子
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******
 *
 * FILE INFO:
 * project:	RTP_lib
 * file:	RTP.c
 * started on:	03/26/03
 * started by:	Cedric Lacroix <lacroix_cedric@yahoo.com>
 *
 *
 * TODO:
 *
 * BUGS:
 *
 * UPDATE INFO:
 * updated on:	05/15/03 16:21:40
 * updated by:	Julien Dupasquier <jdupasquier@wanadoo.fr>
 *
 *******/


#include		<sys/types.h>
#include		<sys/socket.h>

#include		<netinet/in.h>
#include		<arpa/inet.h>

#include		<stdio.h>
#include		<stdlib.h>
#include		<string.h>
#include		<unistd.h>
#include		<err.h>
#include 		<math.h>

#include 		"Config.h"
#include 		"RTP.h"
#include 		"Macros.h"
#include 		"Proto.h"


/****************************************/
/****                                ****/
/****       GLOBALES VARIABLES       ****/
/****                                ****/
/****************************************/

u_int32			payload_type[] =
{
	8000, 8000, 8000, 8000, 8000, 8000, 16000, 8000, 8000, 8000,	/* 0-9 */
	44100, 44100, 0, 0, 90000, 8000, 11025, 22050, 0, 0,		/* 10-19 */
	0, 0, 0, 0, 0, 90000, 90000, 0, 90000, 0,			/* 20-29 */
	0, 90000, 90000, 90000, 90000, 0, 0, 0, 0, 0,			/* 30-39 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 40-49 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 50-59 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 60-69 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 70-79 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 80-89 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 90-99 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 100-109 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,					/* 110-119 */
	0, 0, 0, 0, 0, 0, 0, 0						/* 120-127 */
};

//sess_context_t		**context_list	= NULL;
u_int32			nb_context	= 0;
u_int32			above_context	= 0;
u_int32			*tab_ssrc	= NULL;




/****************************************/
/****                                ****/
/****           FUNCTIONS            ****/
/****                                ****/
/****************************************/


/**
 **  Alloc the context structure and initialize it
 **/
int			RTP_Create(context *the_context)
{
	conx_context_t	*conx_cc;

	RTP_Context_Create(the_context);
	conx_cc = (conx_context_t *) malloc(sizeof(conx_context_t));
	conx_cc->connection_opened = FALSE;
	conx_cc->send_addr_list = NULL;
	RTP_Session_Save_Cxinfo(*the_context, conx_cc);

	return (0);
}


/**
 **  Destroy the RTP session
 **/
int			RTP_Destroy(context cid)
{
	conx_context_t	 *conx_cc = NULL;
	remote_address_t *buff;
	remote_address_t *buff2;

	conx_cc = (conx_context_t *) malloc(sizeof(conx_context_t));
	buff = (remote_address_t *) malloc(sizeof(remote_address_t));
	buff2 = (remote_address_t *) malloc(sizeof(remote_address_t));

	RTP_Session_Restore_Cxinfo(cid, (void **)&conx_cc);
	buff = conx_cc->send_addr_list;
	while (buff)
	{
		//closesocket(buff->socket);
		buff2 = buff->next;
		free(buff);
		buff = buff2;
	}
	free(conx_cc);
	RTP_Context_destroy(cid);
	return (0);
}


/**
 **  Send the packet and update the context
 **/
int			RTP_Send(context cid, u_int32 tsinc, u_int8 marker, u_int16 pti, u_int8 *payload, int len)
{
	u_int32		ts;
	u_int8		padd_len;

	padd_len = MAX_PAYLOAD_LEN - len;
	ts = context_list[cid]->init_RTP_timestamp + context_list[cid]->time_elapsed;

	RTP_Sd_Pkt(cid, ts, marker, pti, payload, len, padd_len);

	if(context_list[cid]->seq_no == 65535)
		context_list[cid]->seq_no = 0;
	else
		context_list[cid]->seq_no++;
	context_list[cid]->time_elapsed += tsinc;
	context_list[cid]->sending_pkt_count++;
	context_list[cid]->sending_octet_count += len;
	return (0);
}


/**
 **  Receive the packet
 **/
/*
int			RTP_Receive(context cid, int fd, char *msg, int *len, struct sockaddr	*sin)
{
	return (0);
}
*/


/**
 **  Add a send address and port
 **/
int			RTP_Add_Send_Addr(context cid, char *addr, u_int16 port, u_int8 ttl)
{
	conx_context_t	 *conx_cc;
	remote_address_t *remote_dev_data;
	SOCKADDR_IN 	 sin;
	int		 ismulticast;

	RTP_Session_Restore_Cxinfo(cid, (void **)&conx_cc);
	remote_dev_data = (remote_address_t *) malloc(sizeof(remote_address_t));
	strcpy(remote_dev_data->address, addr);
	remote_dev_data->port = port;
	ismulticast = test_multicast(remote_dev_data->address);
	if (ismulticast)
		remote_dev_data->ttl = ttl;
	else
		remote_dev_data->ttl = 0;
	sin.sin_addr.s_addr	= inet_addr(remote_dev_data->address);
	sin.sin_family		= AF_INET;
	sin.sin_port		= htons(port);
	if ((remote_dev_data->socket = Create_Socket(sin)) == -1)
		return (-1);
	if (ismulticast)
	{
		#ifdef UNIX
			Set_TTL_Unix(remote_dev_data->socket, remote_dev_data->ttl);
		#endif
		#ifdef WINDOWS
			Set_TTL_Win32(remote_dev_data->socket, remote_dev_data->ttl);
		#endif
	}
	remote_dev_data->next = conx_cc->send_addr_list;
	conx_cc->send_addr_list = remote_dev_data;
	RTP_Session_Save_Cxinfo(cid, conx_cc);
	return (0);
}


/**
 **  Remove a send address and port
 **/
int			RTP_Rem_Send_Addr(context cid, char *addr, u_int16 port, u_int8 ttl)
{
	conx_context_t	 *conx_cc;
	remote_address_t *buffer1;
	remote_address_t *buffer2;

	RTP_Session_Restore_Cxinfo(cid, (void **)&conx_cc);
	buffer1 = (remote_address_t *) malloc(sizeof(remote_address_t));
	buffer2 = (remote_address_t *) malloc(sizeof(remote_address_t));
	buffer1 = conx_cc->send_addr_list;
	if (!test_multicast(addr))
		ttl = 0;
	if (buffer1)
	{
		if (!strcmp(buffer1->address, addr) && (buffer1->port == port) && (buffer1->ttl == ttl))
			conx_cc->send_addr_list = buffer1->next;
		else
		{
			while (buffer1)
			{
				buffer2 = buffer1;
				buffer1 = buffer1->next;
				if (buffer1)
				{
					if (!strcmp(buffer1->address, addr) && (buffer1->port == port) && (buffer1->ttl == ttl))
					{
						buffer2->next = buffer1->next;
						break;
					}
				}
			}
		}
	}
	RTP_Session_Save_Cxinfo(cid, conx_cc);
	return (0);
}


/**
 **  Set the profile in the extension header
 **/
int			Set_Extension_Profile(context cid, u_int16 profile)
{
	#ifndef MALLOC_EXT
	#define MALLOC_EXT
	MEM_ALLOC(context_list[cid]->hdr_extension);
	#endif /* MALLOC_EXT */
	context_list[cid]->hdr_extension->ext_type = htons(profile);
	return (0);
}


/**
 **  Add an extension
 **/
int			Add_Extension(context cid, u_int32 extension)
{
	u_int16		nb_ext;

	if (!(context_list[cid]->hdr_extension->ext_len))
		context_list[cid]->hdr_extension->ext_len = 0;
	if (!(context_list[cid]->hdr_extension->hd_ext))
	{
		#ifndef MALLOC_EXT
		#define MALLOC_EXT
		MEM_ALLOC(context_list[cid]->hdr_extension);
		#endif /* MALLOC_EXT */
		MEM_ALLOC(context_list[cid]->hdr_extension->hd_ext);
		context_list[cid]->hdr_extension->hd_ext[0] = htonl(extension);
		context_list[cid]->hdr_extension->ext_len = htons(1);
	}
	else
	{
		nb_ext = ntohs(context_list[cid]->hdr_extension->ext_len);
		nb_ext++;
		context_list[cid]->hdr_extension->ext_len = htons(nb_ext);
		context_list[cid]->hdr_extension->hd_ext = realloc(context_list[cid]->hdr_extension->hd_ext, sizeof(u_int32) * nb_ext);
		context_list[cid]->hdr_extension->hd_ext[nb_ext - 1] = htonl(extension);
	}
	return (0);
}


/**
 **  Remove extension header
 **/
int			Rem_Ext_Hdr(context cid)
{
	free(context_list[cid]->hdr_extension);
	return (0);
}


/**
 ** Add a CSRC
 **/
int			Add_CRSC(context cid, u_int32 csrc)
{
	if (!(context_list[cid]->CSRCList))
	{
		MEM_ALLOC(context_list[cid]->CSRCList);
		context_list[cid]->CSRCList[0]= htonl(csrc);
		context_list[cid]->CSRClen = 1;
	}
	else
	{
		context_list[cid]->CSRClen++;
		context_list[cid]->CSRCList = realloc(context_list[cid]->CSRCList, sizeof(u_int32) * context_list[cid]->CSRClen);
		context_list[cid]->CSRCList[context_list[cid]->CSRClen - 1] = htonl(csrc);
	}
	return (0);
}


/**
 **  Return the period
 **/
u_int32			Get_Period_us(u_int8 pt)
{
	if (payload_type[pt])
		return (1000000 / payload_type[pt]);
	else
		return (0);
}


/******************************************************************/
/******************************************************************/

/**
 **  Save data about connexion in the context structure
 **/
int			RTP_Session_Save_Cxinfo(context cid, void *info)
{
	context_list[cid]->conx_data = info;
	return (0);
}


/**
 **  Restore data about connexion in the context structure
 **/
int			RTP_Session_Restore_Cxinfo(context cid, void **info)
{
	*info = context_list[cid]->conx_data;
	return (0);
}


/**
 **  Initialize the context
 **/
int			Init_Context(context context_num, u_int32 nb_context)
{
	Random_init();
	context_list[context_num]->context_num = context_num;
	context_list[context_num]->CSRClen = 0;
	context_list[context_num]->CSRCList = 0;
	context_list[context_num]->init_RTP_timestamp = Random32(context_num);
	context_list[context_num]->RTP_timestamp = 0;
	context_list[context_num]->init_seq_no = (Random32(context_num) % 65536);
	context_list[context_num]->my_previous_ssrc = 0;
	context_list[context_num]->my_ssrc = Random32(context_num);
	context_list[context_num]->sending_pkt_count = 0;
	context_list[context_num]->sending_octet_count = 0;
	context_list[context_num]->seq_no = context_list[context_num]->init_seq_no;

⌨️ 快捷键说明

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