文章目录
一、RTP封装
1.1 RTP数据结构
1.2 RTP包的结构以及发包函数
1.3 源码
二、H.264的RTP打包
2.1 H.264格式以及H.264的RTP打包方式
2.2 H.264 RTP包的时间戳计算
2.3 源码
三、H.264 RTP打包的sdp描述
四、测试
在写RTP传输H264之前,需要了解一些知识。1. RTP流的相关知识。2. H264、H265裸流NALU的格式。以上两点在网络流媒体(三)—RTP流有所介绍。
RTP头部的格式见网络流媒体(三)—RTP流,我们用数据结构把它描述出来。
struct RtpHeader
{
/* byte 0 */
uint8_t csrcLen:4;
uint8_t extension:1;
uint8_t padding:1;
uint8_t version:2;
/* byte 1 */
uint8_t payloadType:7;
uint8_t marker:1;
/* bytes 2,3 */
uint16_t seq;
/* bytes 4-7 */
uint32_t timestamp;
/* bytes 8-11 */
uint32_t ssrc;
};
struct RtpPacket
{
struct RtpHeader rtpHeader;
uint8_t payload[0];
};
包含一个RTP头部和RTP载荷,uint8_t payload[0]
并不占用空间,它表示rtp头部接下来紧跟着的地址。
/*
* 函数功能:发送RTP包
* 参数 socket:表示本机的udp套接字
* 参数 ip:表示目的ip地址
* 参数 port:表示目的的端口号
* 参数 rtpPacket:表示rtp包
* 参数 dataSize:表示rtp包中载荷的大小
* 放回值:发送字节数
*/
int rtpSendPacket(int socket, char* ip, int16_t port, struct RtpPacket* rtpPacket, uint32_t dataSize)
{
struct sockaddr_in addr;
int ret;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
rtpPacket->rtpHeader.seq = htons(rtpPacket->rtpHeader.seq);
rtpPacket->rtpHeader.timestamp = htonl(rtpPacket->rtpHeader.timestamp);
rtpPacket->rtpHeader.ssrc = htonl(rtpPacket->rtpHeader.ssrc);
ret = sendto(socket, (void*)rtpPacket, dataSize+RTP_HEADER_SIZE, 0,
(struct sockaddr*)&addr, sizeof(addr));
rtpPacket->rtpHeader.seq = ntohs(rtpPacket->rtpHeader.seq);
rtpPacket->rtpHeader.timestamp = ntohl(rtpPacket->rtpHeader.timestamp);
rtpPacket->rtpHeader.ssrc = ntohl(rtpPacket->rtpHeader.ssrc);
return ret;
}
我们设置好一个包之后,就会调用这个函数发送指定目标。这个函数中多处使用htons等函数,是因为RTP是采用网络字节序(大端模式),所以要将主机字节字节序转换为网络字节序。
rtp.h
#ifndef _RTP_H_
#define _RTP_H_
#include
#define RTP_VESION 2
#define RTP_PAYLOAD_TYPE_H264 96
#define RTP_PAYLOAD_TYPE_AAC 97
#define RTP_HEADER_SIZE 12
#define RTP_MAX_PKT_SIZE 1400
/*
*
* 0 1 2 3
* 7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |V=2|P|X| CC |M| PT | sequence number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | timestamp |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | synchronization source (SSRC) identifier |
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* | contributing source (CSRC) identifiers |
* : .... :
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*/
struct RtpHeader
{
/* byte 0 */
uint8_t csrcLen:4;
uint8_t extension:1;
uint8_t padding:1;
uint8_t version:2;
/* byte 1 */
uint8_t payloadType:7;
uint8_t marker:1;
/* bytes 2,3 */
uint16_t seq;
/* bytes 4-7 */
uint32_t timestamp;
/* bytes 8-11 */
uint32_t ssrc;
};
struct RtpPacket
{
struct RtpHeader rtpHeader;
uint8_t payload[0];
};
void rtpHeaderInit(struct RtpPacket* rtpPacket, uint8_t csrcLen, uint8_t extension,
uint8_t padding, uint8_t version, uint8_t payloadType, uint8_t marker,
uint16_t seq, uint32_t timestamp, uint32_t ssrc);
int rtpSendPacket(int socket, char* ip, int16_t port, struct RtpPacket* rtpPacket, uint32_t dataSize);
#endif //_RTP_H_
rtp.c
/*
* 作者:_JT_
* 博客:https://blog.csdn.net/weixin_42462202
*/
#include
#include
#include
#include
#include
#include "rtp.h"
void rtpHeaderInit(struct RtpPacket* rtpPacket, uint8_t csrcLen, uint8_t extension,
uint8_t padding, uint8_t version, uint8_t payloadType, uint8_t marker,
uint16_t seq, uint32_t timestamp, uint32_t ssrc)
{
rtpPacket->rtpHeader.csrcLen = csrcLen;
rtpPacket->rtpHeader.extension = extension;
rtpPacket->rtpHeader.padding = padding;
rtpPacket->rtpHeader.version = version;
rtpPacket->rtpHeader.payloadType = payloadType;
rtpPacket->rtpHeader.marker = marker;
rtpPacket->rtpHeader.seq = seq;
rtpPacket->rtpHeader.timestamp = timestamp;
rtpPacket->rtpHeader.ssrc = ssrc;
}
int rtpSendPacket(int socket, char* ip, int16_t port, struct RtpPacket* rtpPacket, uint32_t dataSize)
{
struct sockaddr_in addr;
int ret;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
rtpPacket->rtpHeader.seq = htons(rtpPacket->rtpHeader.seq);
rtpPacket->rtpHeader.timestamp = htonl(rtpPacket->rtpHeader.timestamp);
rtpPacket->rtpHeader.ssrc = htonl(rtpPacket->rtpHeader.ssrc);
ret = sendto(socket, (void*)rtpPacket, dataSize+RTP_HEADER_SIZE, 0,
(struct sockaddr*)&addr, sizeof(addr));
rtpPacket->rtpHeader.seq = ntohs(rtpPacket->rtpHeader.seq);
rtpPacket->rtpHeader.timestamp = ntohl(rtpPacket->rtpHeader.timestamp);
rtpPacket->rtpHeader.ssrc = ntohl(rtpPacket->rtpHeader.ssrc);
return ret;
}
详见网络流媒体(三)—RTP流。
RTP包的时间戳起始值是随机的。
RTP包的时间戳增量怎么计算?
假设时钟频率为90000,帧率为25。
频率为90000表示一秒用90000点来表示。
帧率为25,那么一帧就是1/25秒。
所以一帧有90000*(1/25)=3600个点来表示。
因此每一帧数据的时间增量为3600。
#include
#include
#include
#include
#include
#include
#include
#include
#include "rtp.h"
#define H264_FILE_NAME "test.h264"
#define CLIENT_IP "10.14.33.103"
#define CLIENT_PORT 9832
#define FPS 25
static inline int startCode4(char* buf)
{
if(buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 1)
return 1;
else
return 0;
}
static char* findNextStartCode(char* buf, int len)
{
int i;
if(len < 4)
return NULL;
for(i = 0; i < len-4; i++)
{
if(startCode4(buf))
return buf;
buf++;
}
return NULL;
}
static int getFrameFromH264File(int fd, char* frame, int size)
{
int rSize, frameSize;
char* nextStartCode;
if(fd < 0)
return fd;
rSize = read(fd, frame, size);
if(!startCode4(frame))
return -1;
nextStartCode = findNextStartCode(frame+4, rSize-4);
if(!nextStartCode)
{
lseek(fd, 0, SEEK_SET);
frameSize = rSize;
}
else
{
frameSize = (nextStartCode-frame);
lseek(fd, frameSize-rSize, SEEK_CUR);
}
return frameSize;
}
static int createUdpSocket()
{
int fd;
int on = 1;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
return -1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
return fd;
}
static int rtpSendH264Frame(int socket, char* ip, int16_t port,
struct RtpPacket* rtpPacket, uint8_t* frame, uint32_t frameSize)
{
uint8_t naluType; // nalu第一个字节
int sendBytes = 0;
int ret;
naluType = frame[0];
if (frameSize <= RTP_MAX_PKT_SIZE) // nalu长度小于最大包场:单一NALU单元模式
{
/*
* 0 1 2 3 4 5 6 7 8 9
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |F|NRI| Type | a single NAL unit ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
memcpy(rtpPacket->payload, frame, frameSize);
ret = rtpSendPacket(socket, ip, port, rtpPacket, frameSize);
if(ret < 0)
return -1;
rtpPacket->rtpHeader.seq++;
sendBytes += ret;
if ((naluType & 0x1F) == 7 || (naluType & 0x1F) == 8) // 如果是SPS、PPS就不需要加时间戳
goto out;
}
else // nalu长度小于最大包场:分片模式
{
/*
* 0 1 2
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | FU indicator | FU header | FU payload ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/*
* FU Indicator
* 0 1 2 3 4 5 6 7
* +-+-+-+-+-+-+-+-+
* |F|NRI| Type |
* +---------------+
*/
/*
* FU Header
* 0 1 2 3 4 5 6 7
* +-+-+-+-+-+-+-+-+
* |S|E|R| Type |
* +---------------+
*/
int pktNum = frameSize / RTP_MAX_PKT_SIZE; // 有几个完整的包
int remainPktSize = frameSize % RTP_MAX_PKT_SIZE; // 剩余不完整包的大小
int i, pos = 1;
/* 发送完整的包 */
for (i = 0; i < pktNum; i++)
{
rtpPacket->payload[0] = (naluType & 0x60) | 28;
rtpPacket->payload[1] = naluType & 0x1F;
if (i == 0) //第一包数据
rtpPacket->payload[1] |= 0x80; // start
else if (remainPktSize == 0 && i == pktNum - 1) //最后一包数据
rtpPacket->payload[1] |= 0x40; // end
memcpy(rtpPacket->payload+2, frame+pos, RTP_MAX_PKT_SIZE);
ret = rtpSendPacket(socket, ip, port, rtpPacket, RTP_MAX_PKT_SIZE+2);
if(ret < 0)
return -1;
rtpPacket->rtpHeader.seq++;
sendBytes += ret;
pos += RTP_MAX_PKT_SIZE;
}
/* 发送剩余的数据 */
if (remainPktSize > 0)
{
rtpPacket->payload[0] = (naluType & 0x60) | 28;
rtpPacket->payload[1] = naluType & 0x1F;
rtpPacket->payload[1] |= 0x40; //end
memcpy(rtpPacket->payload+2, frame+pos, remainPktSize+2);
ret = rtpSendPacket(socket, ip, port, rtpPacket, remainPktSize+2);
if(ret < 0)
return -1;
rtpPacket->rtpHeader.seq++;
sendBytes += ret;
}
}
out:
return sendBytes;
}
int main(int argc, char* argv[])
{
int socket;
int fd;
int fps = 25;
int startCode;
struct RtpPacket* rtpPacket;
uint8_t* frame;
uint32_t frameSize;
fd = open(H264_FILE_NAME, O_RDONLY);
if(fd < 0)
{
printf("failed to open %s\n", H264_FILE_NAME);
return -1;
}
socket = createUdpSocket();
if(socket < 0)
{
printf("failed to create socket\n");
return -1;
}
rtpPacket = (struct RtpPacket*)malloc(500000);
frame = (uint8_t*)malloc(500000);
rtpHeaderInit(rtpPacket, 0, 0, 0, RTP_VESION, RTP_PAYLOAD_TYPE_H264, 0,
0, 0, 0x88923423);
while(1)
{
frameSize = getFrameFromH264File(fd, frame, 500000);
if(frameSize < 0)
{
printf("read err\n");
continue;
}
if(startCode4(frame))
startCode = 4;
frameSize -= startCode;
rtpSendH264Frame(socket, CLIENT_IP, CLIENT_PORT,
rtpPacket, frame+startCode, frameSize);
rtpPacket->rtpHeader.timestamp += 90000/FPS;
usleep(1000*1000/fps);
}
free(rtpPacket);
free(frame);
return 0;
}
sdp文件有什么用?
sdp描述着媒体信息,当使用vlc打开这个sdp文件后,会根据这些信息做相应的操作(创建套接字…),然后等待接收RTP包。
这里给出RTP打包H.264的sdp文件,并描述每一行是什么意思。
m=video 9832 RTP/AVP 96
a=rtpmap:96 H264/90000
a=framerate:25
c=IN IP4 10.14.33.103
这个一个媒体级的sdp描述,关于sdp文件描述详情可看PTSP协议介绍。
> m=video 9832 RTP/AVP 96
格式为 m=<媒体类型> <端口号> <传输协议> <媒体格式 >
媒体类型:video,表示这是一个视频流
端口号:9832,表示UDP发送的目的端口为9832
传输协议:RTP/AVP,表示RTP OVER UDP,通过UDP发送RTP包
媒体格式:表示负载类型(payload type),一般使用96表示H.264
> a=rtpmap:96 H264/90000
格式为a=rtpmap:<媒体格式><编码格式>/<时钟频率>
> a=framerate:25
表示帧率
> c=IN IP4 10.14.33.103
IN:表示internet
IP4:表示IPV4
10.14.33.103:表示UDP发送的目的地址为10.14.33.103
特别注意:这段sdp文件描述的udp发送的目的IP为10.14.33.103,目的端口为9832。
讲上面给出的源码rtp.c、rtp.h、rtp_h264.c保存下来,然后编译运行
注意:该程序默认打开的是test.h264,如果你没有视频源,可以从选择你自己的数据源,该一下名字就行。
# gcc rtp.c rtp_h264.c
# ./a.out
讲上面的sdp文件保存为rtp_h264.sdp
,使用vlc打开,即可观看到视频