
孤独QQ


0 访问量
0 原创专栏
0 资料
0 粉丝
个性签名:此E友还没有留下个性签名哦~
加入易百纳时间:2013-11-15
-
在此先谢谢好心的网友。2013-11-153 0 3604
-
[code]#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "hi_common_api.h" #include "avcommon.h" #include "av_type.h" #include "dvs_pub.h" #include "av_var.h" CHAR args[10][255]; /********************************************************** Function : HI_Socket Description : 创建socket Input : int af, int type, int protocol Output : 无 Return Value : int Date : 2007/10/10 Author : cuixianwang **********************************************************/ int HI_Socket(int af, int type, int protocol) { return socket(af, type, protocol); } /********************************************************** Function : HI_SetSockOpt Description : 设置socket Input : int s, int level, int optname, const VOID *optval, HI_SockLen_T optlen Output : 无 Return Value : int Date : 2007/10/10 Author : cuixianwang **********************************************************/ int HI_SetSockOpt(int s, int level, int optname, const VOID *optval, HI_SockLen_T optlen) { return setsockopt(s, level, optname, optval, optlen); } /********************************************************** Function : HI_CloseSocket Description : 关闭socket Input : int ulSocket Output : 无 Return Value : int Date : 2007/9/25 Author : cuixianwang **********************************************************/ int HI_CloseSocket(int ulSocket) { return close(ulSocket); } /********************************************************** Function : HI_Bind Description : 与特定socket的绑定地址 Input : int s, const struct sockaddr *paddr, int addrlen Output : 无 Return Value : int Date : 2007/9/25 Author : cuixianwang **********************************************************/ int HI_Bind(int s, const struct sockaddr *paddr, int addrlen) { return bind(s, (struct sockaddr *)paddr, addrlen); } /********************************************************** Function : HI_SOCKET_Udp_GetPort Description : 获取UDP socket端口 Input : HI_SOCKET fd Output : 无 Return Value : USHORT Date : 2007/10/10 Author : cuixianwang **********************************************************/ USHORT HI_SOCKET_Udp_GetPort(HI_SOCKET fd) { socklen_t namelen = sizeof(struct sockaddr_in); struct sockaddr_in *s; if(getsockname(fd, (struct sockaddr *)s, &namelen)) { return 0; } return ntohs(s->sin_port); } /********************************************************** Function : HI_SOCKET_Udp_Open Description : 打开UDP socket Input : USHORT port Output : 无 Return Value : HI_SOCKET Date : 2007/10/10 Author : cuixianwang **********************************************************/ HI_SOCKET HI_SOCKET_Udp_Open(USHORT port) { HI_SOCKET fd; struct sockaddr_in addr; fd = socket(AF_INET, SOCK_DGRAM, 0); if(fd < 0) { printf("UDP Socket Open error: create socket error.\n"); return -1; } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); memset(&(addr.sin_zero), '\0', 8); // zero the rest of the struct if(bind(fd, (struct sockaddr *)&addr, sizeof(addr))) { HI_CloseSocket(fd); printf("UDP Socket Open error: bind socket error.\n"); return -1; } return fd; } /********************************************************** Function : RTP_Sender_Find Description : 查找是否已经存在目的主机 Input : *pszRemoteHost, ip, port Output : 无 Return Value : 存在则返回REMOTEHOST_S类型指针; 不存在则返回空指针 Date : 2007/10/17 Author : cuixianwang **********************************************************/ REMOTEHOST_S* RTP_Sender_Find(REMOTEHOST_S *pszRemoteHost, IN CHAR *ip, IN USHORT port) { ULONG ulCounter = 0; /*输入参数合法性判断*/ if(NULL == ip) { return NULL; } /*查找目标主机是否已经存在*/ for(ulCounter = 0; ulCounter < RTP_MAX_SENDER; ulCounter++) { if((strcmp(ip, pszRemoteHost[ulCounter].RemoteIp) == 0) && (port == pszRemoteHost[ulCounter].ReceivePort) && (pszRemoteHost[ulCounter].Active == TRUE)) { return(&(pszRemoteHost[ulCounter])); } } /*不存在该目标主机*/ return NULL; } /********************************************************** Function : RTP_Sender_FindAvail Description : 在全局数组中查找插入位置 Input : *pszRemoteHost Output : 无 Return Value : REMOTEHOST_S* Date : 2007/10/17 Author : cuixianwang **********************************************************/ REMOTEHOST_S* RTP_Sender_FindAvail(REMOTEHOST_S *pszRemoteHost) { ULONG ulCounter = 0; /*查找目标主机插入的位置*/ for(ulCounter = 0; ulCounter < RTP_MAX_SENDER; ulCounter++) { if(pszRemoteHost[ulCounter].Active == FALSE) { return(&(pszRemoteHost[ulCounter])); } } /*发送目标数量已经最大*/ return NULL; } /********************************************************** Function : RTP_Sender_Add Description : 添加音视频发送的目的主机 Input : *pszRemoteHost, *ip, port Output : 无 Return Value : int Date : 2007/10/17 Author : cuixianwang **********************************************************/ int RTP_Sender_Add(IN REMOTEHOST_S *pszRemoteHost, OUT REMOTEHOST_S *pstHost, IN CHAR *ip, IN USHORT port) { HI_SOCKET Sock; int inRetValue; int yes = 1; USHORT UdpPort = 0; REMOTEHOST_S *pstRemoteHost = NULL; /*输入参数和法性判断*/ if((NULL == ip) || (NULL == pszRemoteHost)) { return FAILURE; } /*查找目标机是否已经存在*/ pstRemoteHost = RTP_Sender_Find(pszRemoteHost, ip, port); /*不存在该目的主机*/ if(NULL == pstRemoteHost) { /*查找该目的主机的插入位置*/ pstRemoteHost = RTP_Sender_FindAvail(pszRemoteHost); /*达到了支持最大用户数目*/ if(NULL == pstRemoteHost) { return FAILURE; } memset(pstRemoteHost, 0, sizeof(REMOTEHOST_S)); strcpy(pstRemoteHost->RemoteIp, ip); pstRemoteHost->ReceivePort = port; pstRemoteHost->RemoteAddr.sin_family = AF_INET; // host byte order pstRemoteHost->RemoteAddr.sin_port = htons(pstRemoteHost->ReceivePort); // SHORT, network byte order pstRemoteHost->RemoteAddr.sin_addr.s_addr = inet_addr(pstRemoteHost->RemoteIp); memset(&(pstRemoteHost->RemoteAddr.sin_zero), '\0', 8); // zero the rest of the struct /*创建UDP socket*/ Sock = HI_SOCKET_Udp_Open(0); if(Sock < 0) { printf("create socket error.\n"); return FAILURE; } UdpPort = HI_SOCKET_Udp_GetPort(Sock); pstRemoteHost->UdpPort = UdpPort; pstRemoteHost->Sock = Sock; inRetValue = setsockopt(Sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if(FAILURE == inRetValue) { return FAILURE; } pstRemoteHost->Active = TRUE; } pstRemoteHost->HostState = RTP_TARGETHOST_STATE_REQ_IFrame; memcpy(pstHost, pstRemoteHost, sizeof(REMOTEHOST_S)); return SUCCESS; } /********************************************************** Function : HI_RTP_Packet Description : 构造RTP报文 Input : pRtpStream, ts_inc, marker, pPayload, len Output : 无 Return Value : int Date : 2007/10/10 Author : cuixianwang **********************************************************/ int HI_RTP_Packet(IN RTP_SENDER_S *pRtpStream, int ts_inc, UINT marker, CHAR *pPayload, int len) { RTP_HDR_S *pRtpHdr = NULL; /*输入参数合法性检查*/ if((NULL == pRtpStream) || (NULL == pPayload)) { return FAILURE; } memset(pRtpStream->buff, 0, RPT_MAX_PACKET_BUFF); pRtpStream->buffLen = 0; pRtpHdr = (RTP_HDR_S*)pRtpStream->buff; RTP_HDR_SET_VERSION(pRtpHdr, RTP_VERSION); RTP_HDR_SET_P(pRtpHdr, 0); RTP_HDR_SET_X(pRtpHdr, 1); RTP_HDR_SET_CC(pRtpHdr, 0); RTP_HDR_SET_M(pRtpHdr, marker); RTP_HDR_SET_PT(pRtpHdr, pRtpStream->pt); RTP_HDR_SET_SEQNO(pRtpHdr, htons(pRtpStream->last_sn)); RTP_HDR_SET_TS(pRtpHdr, htonl(pRtpStream->last_ts)); RTP_HDR_SET_SSRC(pRtpHdr, htonl(pRtpStream->ssrc)); pRtpHdr->usProfile = htons(1); pRtpHdr->usExtLen = htons(2); pRtpHdr->uiVendorID = 0x7000 & 0xff00; pRtpHdr->uiPadding = 0x0000; pRtpStream->last_sn++; pRtpStream->last_ts += ts_inc; memcpy((pRtpStream->buff + RTP_HDR_LEN), pPayload, len); pRtpStream->buffLen = RTP_HDR_LEN + len; return SUCCESS; } /********************************************************** Function : RTP_Send Description : 向各个目标机发送RTP报文 Input : *pRtpStream, *pstRemoteHost Output : 无 Return Value : FAILURE 失败; SUCCESS 成功 Date : 2007/10/17 Author : cuixianwang **********************************************************/ int RTP_Send(IN RTP_SENDER_S *pRtpStream, IN REMOTEHOST_S *pstRemoteHost) { int iRetValue = 0; ULONG ulCounter = 0; /*输入参数合法性检查*/ if((NULL == pRtpStream) || (NULL == pstRemoteHost)) { return FAILURE; } /*向各个目标机发送RTP报文*/ for(ulCounter = 0; ulCounter < RTP_MAX_SENDER; ulCounter++) { if((pstRemoteHost[ulCounter].Active == TRUE) && (pRtpStream->buffLen > 0)) { iRetValue = sendto(pstRemoteHost[ulCounter].Sock, pRtpStream->buff, pRtpStream->buffLen, 0, (struct sockaddr*)(&(pstRemoteHost[ulCounter].RemoteAddr)), sizeof(struct sockaddr)); if(iRetValue != pRtpStream->buffLen) { perror("send rtp error."); pRtpStream->stats.sent_error++; printf("send packet error. %s", strerror(errno)); } else { pRtpStream->stats.sent_byte += pRtpStream->buffLen; pRtpStream->stats.sent_packet++; } if(pRtpStream->buffLen <= 16) { exit(-1); } } } return SUCCESS; } [/code]2013-11-1513 20 9015

共2条