纯C语言解析RTP包的头部

RTP数据包构成
RTP数据包一般由:Header+有效载荷数据:构成,Header一般为12字节,有效载荷数据则可以是音频数据,h264码流,PS码流等等

RTP数据包构成
RTP数据包一般由:Header+有效载荷数据:构成,Header一般为12字节,有效载荷数据则可以是音频数据,h264码流,PS码流等等

RTP头部组成 头部一般至少包含12个固定字节,也包括若扩展干字节。

1字节 V(2bit)+P(1bit)+X(1bit)+CC(4bit) 2字节M(1bit)+PT(7bit) 3 4字节SN(序列号) 5 6 7 8字节timestamp(4字节) 9 10 11 12字节SSRC[13 14 15 16字节CSRC(4字节,可选 可以有多个,数量为前面CC的值大小)....] [扩展头部...当前面X==1时才有]

如下图片,其中当CC的值大于0时,CSRC为扩展字节。

1、版本号(V):占2位,用来标志使用的RTP版本。 2、填充位(P):占1位,如果P=1,则该RTP包的尾部包含附加的填充字节。3、作用是为了字节对齐,填充字节的数量保存在最后一个字节中 4、扩展位(X):占1位,如果X=1,则RTP固定头部后面就跟有一个扩展头部,即上面的CSRC信息 5、CSRC计数器(CC):占4位,指示 CSRC标识符的数量。 标记位(M):占1位,当M=1时,对于视频流, 它表示一帧的结束,而对于音频,则表示一次谈话的开始。 6、载荷类型(PT):占7位,标识了RTP载荷的类型,比如是视频还是音频类型 7、序列号(SN):占16位,发送方在每发送完一个RTP包后就将该域的值增加1,接收方可以由该域检测包的丢失及恢复包序列。序列号的初始值是随机的。 8、时间戳(timestamp):占32位,记录了该包中数据的第一个字节的采样时刻。在一次会话开始时,时间戳初始化成一个初始值(随机生成)。即使在没有信号发送时,时间戳的数值也要随时间而不断地增加。时间戳是去除抖动和实现同步不可缺少的。 9、同步源标识符(SSRC):占32位,用来表示RTP包的类型,每次开启新的流会话该值都会变化 10、特约信源(CSRC):每个CSRC标识符占32位,可以有0~15个,每个CSRC 标识了包含在该RTP报文有效载荷中的所有特约信源。当CC>0时有此信息扩展信息XXXX:定义的扩展信息,当X为1时有此信息。

编程解析:

#include<stdio.h>
#include<stdlib.h>

int main(){
    int res, len;
    struct sockaddr_in sin;
    int headerlen = 12;
    unsigned int firstFiled;
    char rawdata[5120];
    unsigned int *rtp_header_ptr = NULL;
    int version, payloadtype, padding, mark, ext, seqno, ssrc;
    unsigned int timestamp;

    len = sizeof(sin);
    res = recvfrom(sockfd, rawdata, sizeof(rawdata), MSG_DONTWAIT, (struct sockaddr *)&sin, , &len);

    if(res < 0)
        return -1;

    rtp_header_ptr = (unsigned int *)rawdata;

    /*Get the first 4 bytes*/
    firstFiled = ntohl(rtp_header_ptr[0]);//网络字节序(大端)转为主机字节序

    //Get RTP Version
    version = (firstFiled & 0xc0000000) >> 30;

    //Get RTP Payloadtype
    payloadtype = (firstFiled & 0x7f0000) >> 16;

    //Get RTP Padding
    padding = (1<<29) & firstFiled;

    //Get RTP mark
    mark = (1<<23) & firstFiled;

    //Get RTP ext
    ext = (1<<28) & firstFiled;

    //Get RTP seqno
    seqno = (firstFiled & 0xffff);

    //Get RTP timestamp
    timestamp = ntohl(rtp_header_ptr[1]);

    //Get RTP ssrc
    ssrc = ntohl(rtp_header_ptr[2]);


    return 0;
}

添加rtp头:

typedef struct _antFrame {
    int frametype;
    int subclass;
    int datalen;
    int samples;
    int mallocd;
    int offset;
    char *src;
    void *data;
int rtp_mark;
int timestamp;
unsigned short seqno;
int ptime;
struct _antFrame *prev;
struct _antFrame *next;
} tAntFrame;

static int RtpRawWrite(tAntRtp * rtp, tAntFrame * f, int codec)
{
    unsigned int *rtp_header_ptr;
    int hdrlen = 12;
    int res;
    int mark = 0;

    rtp_header_ptr = (unsigned int *) (f->data - hdrlen);
    rtp_header_ptr[0] = htonl((2 << 30) | (codec << 16) | (rtp->seqno++) | (mark << 23));
    rtp_header_ptr[1] = htonl(rtp->lastts);//timestamp
    rtp_header_ptr[2] = htonl(rtp->ssrc);
    if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
        res = sendto(rtp->sockFd, (void *) rtp_header_ptr, f->datalen + hdrlen, 0, (struct sockaddr *) &rtp->them, sizeof (rtp->them));
        if (res < 0)
            LogTrace(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
    }
    return 0;
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值