孤独QQ

孤独QQ

0个粉丝

2

问答

0

专栏

0

资料

孤独QQ  发布于  2013-11-15 12:45:46
采纳率 0%
2个问答
8808

海思 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]
我来回答
回答13个
时间排序
认可量排序

liyin2005

0个粉丝

18

问答

0

专栏

7

资料

liyin2005 2015-04-21 09:43:47
认可0
非常感谢,收藏了

syj0424@163.com

0个粉丝

1

问答

0

专栏

0

资料

syj0424@163.com 2015-06-22 17:32:33
认可0
正在学习中,多谢了 !!

youxia0420

0个粉丝

0

问答

0

专栏

0

资料

youxia0420 2015-06-25 09:59:00
认可0
不错,学习收藏了,谢谢!

lee1111

0个粉丝

5

问答

0

专栏

0

资料

lee1111 2015-08-29 16:38:07
认可0

不错,学习收藏了,谢谢!

qq328848298

0个粉丝

1

问答

0

专栏

0

资料

qq328848298 2017-06-05 09:34:13
认可0

不错,学习收藏了,谢谢!

桃花岛主

0个粉丝

0

问答

0

专栏

0

资料

桃花岛主 2017-08-18 10:32:28
认可0
学习了。谢谢楼主

hero

0个粉丝

1

问答

0

专栏

0

资料

hero 2017-08-18 12:09:35
认可0
学习了。谢谢楼主

helphel

0个粉丝

4

问答

0

专栏

0

资料

helphel 2017-08-18 13:25:12
认可0
值得参考学习。。

clare

0个粉丝

0

问答

0

专栏

0

资料

clare 2017-11-22 16:08:01
认可0
mark 学习   

qinjinghua2012

0个粉丝

1

问答

0

专栏

0

资料

qinjinghua2012 2017-11-28 16:00:04
认可0
先收藏。谢谢楼主

qn1543916138

0个粉丝

1

问答

0

专栏

0

资料

qn1543916138 2018-12-05 14:11:46
认可0
收藏了,不错,拿到办公室好好读读。

yangzi8000

0个粉丝

5

问答

0

专栏

0

资料

yangzi8000 2017-03-31 09:21:32
认可0
收藏了。。。。。。。。。。。。。。。。

gcp

0个粉丝

0

问答

12

专栏

0

资料

gcp 2018-12-06 09:38:37
认可0
very good
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区