1.設置rtsp超時
AVDictionary* opts = NULL;
av_dict_set(&opts, "rtsp_transport", m_bTcp ? "tcp" : "udp", 0); //設置tcp or udp,默認一般優先tcp再嘗試udp
av_dict_set(&opts, "stimeout", "3000000", 0);//設置超時3秒
int ret = avformat_open_input(&ctx, url, NULL, &opts);
2.設置udp,http超時
AVDictionary* opts = NULL;
av_dict_set(&opts, "timeout", "3000000", 0);//設置超時3秒
int ret = avformat_open_input(&ctx, url, NULL, &opts);
3.設置av_read_frame 超時
auto ctx = avformat_alloc_context();
ctx->interrupt_callback.callback = CheckInterrupt;//超時回調
ctx->interrupt_callback.opaque = this;
//
m_tStart = time(NULL);
av_read_frame(ctx, &pkt);
//超時回調函數
static int CheckInterrupt(void* ctx)
{
auto p = (xxx*)ctx;
return time(NULL) - p->m_tStart >= 3 ? 1 : 0;//3秒超時
}