当前位置:网站首页>RTP 发送PS流零拷贝方案
RTP 发送PS流零拷贝方案
2022-06-30 15:44:00 【qianbo_insist】
代码依然在gitee上,逐步修改
https://gitee.com/guanzhi0319/rtp
数据达到发送RTP
数据收集到后,编码h264,或者h265,发送ps流或者h264裸流到指定服务器。
void LiveRTPUdpEncoder::OnCaptureVideoBuffer(uint8_t *data, int len, unsigned int timestamp, bool isKeyframe)
{
#define D(x) *(data+x)
if (SimpleThread::IsStop()) return;
#if 0
char buffer[32];
sprintf_s(buffer, "QB:%02x %02x %02x %02x %02x %02x",
D(0), D(1), D(2), D(3), D(4), D(5));
OutputDebugStringA(buffer);
return;
#endif
uint8_t* odata = NULL;
int olen = 0;
uint8_t n = D(4) & 0x1f;
int isIFrame = n == 0x07;
switch (v_stream_type)
{
case en_rtp:
odata = data;
olen = len;
break;
case en_ps_over_rtp:
uint8_t* odata;
int olen;
v_ps.packagingPs(isIFrame, timestamp, data, len, &odata, &olen);
break;
}
//timestamp = GetTimestamp();
{
Lock lock(this);
if(v_stream_type == en_ps_over_rtp)
{
uint8_t* ps = odata + RTP_UDP_HEAD_LEN;
olen = olen - RTP_UDP_HEAD_LEN;
v_rtp.send_video_ps(ps, olen, convertToRTPTimestamp(), isKeyframe);
delete[]odata;
}
else if (v_stream_type == en_rtp)
{
v_rtp.send_video(odata, olen, convertToRTPTimestamp(), isKeyframe);
}
if (!gVideoBegin)
gVideoBegin = true;
}
}
发送RTP ps
在ps流的前面留空12字节,每次1400字节发送的时候,修改前面12字节RTP包,注意也就是修改了数据包,不复用,并且发送一定是同步的才能正确,好处是避免了数据拷贝。
避免数据拷贝的方式有两种,
1、一种是同步缓冲区,但是异步发送
2、同步发送
3、使用协程,同步发送。
这里是客户端,发送时只有一路,使用了UDP,直接同步发送就行。申请ps内存的时候为了不拷贝数据,前面留空了12字节作为RTP UDP的头部。那么在发送每隔包的时候,在发送的数据往前倒推12字节,避免拷贝,但是这样修改了缓冲区里的数据,如果想复用,每次在覆盖的时候先把12字节赋值,发送完毕,再把字节恢复。
int c_rtp::send_video_ps(uint8_t* data, int len, uint32_t timestamp, bool iskeyframe)
{
//data 前面有12 字节为空,构造 ps 流时留空
uint8_t* x = data;
int num = (len - 1) / 1400 + 1;
for (int i = 0; i < num; i++)
{
uint8_t* pos = x - 12;
memset(pos, 0, 12);
RTP_FIXED_HEADER* rtp_hdr; //RTP头部
rtp_hdr = (RTP_FIXED_HEADER*)data;
//设置RTP HEADER,
rtp_hdr->payload = H264; //负载类型号,
rtp_hdr->version = 2; //版本号,此版本固定为2
rtp_hdr->marker = 0; //标志位,由具体协议规定其值。
rtp_hdr->ssrc = little2big(1111);
rtp_hdr->timestamp = little2big(timestamp);
if (i < num-1)
v_sock.SendTo(pos, 12 + 1400, v_sockaddr);
else
v_sock.SendTo(pos, 12 + len -1400*i,v_sockaddr);
x += 1400;
}
}
这里的v_sock 就是简单的同步发送socket。读者有兴趣,可以自己改为异步方式。
边栏推荐
- What role does "low code" play in enterprise digital transformation?
- 云化XR,如何助力产业升级
- [leetcode] linked list sorting (gradually increasing the space-time complexity)
- Model system: Sword (1)
- Log4j2 advanced use
- 婴儿认知学习所带来的启发,也许是下一代无监督机器学习的关键
- halcon变量窗口的图像变量不显示,重启软件和电脑都没用
- topic: Privacy, Deception and Device Abuse
- 互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
- Generating verification code with sring
猜你喜欢
![Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.](/img/c1/99ad29789a669c4498fb93ce1fb009.png)
Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.

Go-Micro安装

Modifying MySQL password under Linux: error 1396 (HY000): Operation alter user failed for 'root' @ 'localhost‘

【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例

几百行代码实现一个 JSON 解析器
![[附下载]渗透测试神器Nessus安装及使用](/img/ef/b6a37497010a8320cf5a49a01c2dff.png)
[附下载]渗透测试神器Nessus安装及使用

Build cloud native observability capability suitable for organizations

Alibaba cloud OSS object storage cross domain settings

Map reduce case super detailed explanation

halcon变量窗口的图像变量不显示,重启软件和电脑都没用
随机推荐
MySQL master-slave configuration
How to get the preferential activities for stock account opening? Is online account opening safe?
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)
Types of waveguides
Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
Data governance Market: Yixin Huachen faces left, Huaao data faces right
Policy Center-User Data
Build cloud native observability capability suitable for organizations
Phone number shielding function
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
Cloud XR, how to help industrial upgrading
Finally understand science! 200 pictures to appreciate the peak of human wisdom
mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法
招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
BYD is more and more like Huawei?
Asp. NETCORE uses cache and AOP to prevent repeated commit
[sub matrix quantity statistics] cf1181c flag sub matrix quantity statistics
Table responsive layout tips for super nice
KDD 2022 | 我们离通用预训练推荐模型还有多远?推荐系统的通用序列表示学习模型 UniSRec