当我们需要发送一些自定义的协议 (并不是一些标准的协议)去解析解码的时候 这个时候就有需要自定义avio_alloc_context()
avio_alloc_context()
这个结构体里面的
read_packet和write_packet是函数指针,指向⽤户编写的回调函数。
可以自行重新设定
使用read_packet 做个demo 实验
AVIOContext *avio_ctx = avio_alloc_context(io_buffer, BUF_SIZE, 0, (void *)in_file, \
read_packet, NULL, NULL);
AVFormatContext *format_ctx = avformat_alloc_context();
format_ctx->pb = avio_ctx;
int ret = avformat_open_input(&format_ctx, NULL, NULL, NULL);
这样就可以把我们自定义的avio read数据方法 保存进上下文中 了
```cpp
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
FILE *in_file = (FILE *)opaque;
int read_size = fread(buf, 1, buf_size, in_file);
printf("read_packet read_size:%d, buf_size:%d\n", read_size, buf_size);
if(read_size <=0) {
return AVERROR_EOF; // 数据读取完毕
}
return read_size;
}