ffmpeg udp推流指定网口

有时设备有多网口,而ffmpeg3.2没有指定网口的方法,因此有了以下几种解决方法:

1 自定义AVIO

一般,我们创建avio输出时,用的函数是

int avio_open(AVIOContext **s, const char *url, int flags)

将打开的AVFormatContext -->pb 指针做为该函数的第一个参数
udp输出的URL做为第二个参数
AVIO_FLAG_WRITE 做为第三个参数

这样做,ffmpeg会自己open一个输出,并相应的write后将流推出去。

然而我们可以通过申请avio的方式,自定义write函数,将申请的avio的结构体指针 赋给AVFormatContext -->pb
这样做的好处是,我们就可以通过设置socket的指定网口属性,然后再去sendto即可满足指定网口推流的功能。

自定义AVIO具体实现方式如下:

        U8 * pu8out_buffer = NULL;
        AVFormatContext * pFormatCtx = NULL;

        // 1)申请AVFormatContext结构体
        avformat_alloc_output_context2(&pFormatCtx, NULL, MPEGTS_FORMAT_NAME, NULL);
        if(!pFormatCtx)
        {
            LOG_PRINTF(LOG_LEVEL_DEBUG, LOG_MODULE_DECODER, "Could not create output context");
            return ;
        }

		// 2) 申请avio缓存空间
        pu8out_buffer = (unsigned char*)av_malloc(WV_TSPKT_LEN * WV_IPOUT_PAYNUM);

        // 3)申请AVIO结构体
        avio_out = avio_alloc_context(pu8out_buffer,     WV_TSPKT_LEN * WV_IPOUT_PAYNUM,1,
NULL,NULL,write_buffer,NULL);

        // 4) 将avio赋值到pb指针
        pFormatCtx->pb = avio_out;
        pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;

重点函数是avio_alloc_context


/**
 * Allocate and initialize an AVIOContext for buffered I/O. It must be later
 * freed with av_free().
 *
 * @param buffer Memory block for input/output operations via AVIOContext.
 *        The buffer must be allocated with av_malloc() and friends.
 *        It may be freed and replaced with a new buffer by libavformat.
 *        AVIOContext.buffer hol
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值