有时设备有多网口,而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