8948
- 收藏
- 点赞
- 分享
- 举报
海思 RTP封包 发送
[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]
#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("
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("
}
else
{
pRtpStream->stats.sent_byte += pRtpStream->buffLen;
pRtpStream->stats.sent_packet++;
}
if(pRtpStream->buffLen <= 16)
{
exit(-1);
}
}
}
return SUCCESS;
}
[/code]
我来回答
回答13个
时间排序
认可量排序
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
认可0
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
相关问答
-
2023-12-01 14:34:20
-
2016-12-06 11:01:44
-
2015-09-12 16:08:11
-
2014-09-30 09:26:45
-
2018-12-21 01:06:21
-
2014-11-12 16:35:33
-
2016-12-08 13:04:57
-
2014-12-29 20:16:24
-
2020-10-12 17:25:41
-
2015-09-11 18:05:20
-
2016-07-04 08:47:34
-
2020-07-21 14:39:06
-
2017-02-22 15:13:29
-
2014-09-04 12:43:59
-
2015-12-09 10:52:34
-
2016-03-08 14:04:13
-
2024-03-20 16:36:29
-
2020-02-29 18:27:48
-
2021-06-14 21:32:16
无更多相似问答 去提问
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
-
10求HI3519DV500_SDK_V2.0.1.1
-
5有偿求HI3516DV500 + OV5647驱动
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认