ffmpeg-设置推流,拉流使用的协议类型(TCP/UDP)

原文地址:

https://blog.csdn.net/shizheng163/article/details/80475927

 

如有错误,请指正,谢谢。

拉流(设置TCP/UDP)

//设置参数
AVDictionary *format_opts = NULL;
av_dict_set(&format_opts, "stimeout", std::to_string( 2* 1000000).c_str(), 0); //设置链接超时时间(us)
av_dict_set(&format_opts, "rtsp_transport",  "tcp", 0); //设置推流的方式,默认udp。
//初始化输入上下文
AVFormatContext * m_InputContext = avformat_alloc_context();
//打开输入流。
avformat_open_input(&m_InputContext, "rtsp://127.0.0.1:554/", NULL, &format_opts);
// ......123456789

推流(设置TCP/UDP)

//初始化输出流上下文。
AVFormatContext * output_format_context_ = NULL;
avformat_alloc_output_context2(&output_format_context_, NULL, "rtsp", "rtsp://127.0.0.1:554/");
/*
    添加流信息
    //TODO
*/
//设置参数,设置为TCP推流, 默认UDP
AVDictionary *format_opts = NULL;
av_dict_set(&format_opts, "stimeout", std::to_string(2 * 1000000).c_str(), 0);
av_dict_set(&format_opts, "rtsp_transport", "tcp", 0);
//写入输出头(建立rtsp连接)
avformat_write_header(output_format_context_, &format_opts);

while(1)
{
    AVPakcet media_pkt;
    /*
        初始化PKT,
        读取数据,
        填充数据,
    */
    //发送数据, 多个流时需要使用 av_interleaved_write_frame, 另附ret部分错误码,见其他文章。
    int ret = av_interleaved_write_frame(output_format_context_, &media_pkt);
    //释放数据
    av_packet_unref(&media_pkt);
    av_free_packet(&media_pkt);
}12345678910111213141516171819202122232425262728

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值