問題,拉流hls報以下錯誤
[hls @ 000001bc59476000] Opening 'http://39.135.138.59:18890/PLTV/88888910/224/3221225644/1647845325-1-1644624937.hls.ts?ssr_hostlv1=39.134.116.2:18890&ssr_host=117.169.122.137:8080' for reading
[hls @ 000001bc59476000] Failed to open segment 1644624937 of playlist 0
在不設置回調,只指定 timeout
參數后並無效果,設置 rw_timeout
后會導致一直失敗重讀,設置回調后才可以真正判斷超時,設置方式如下
AVDictionary* opts = NULL;
if (startWith(srcPath, "rtsp://"))
{
// 設置rtsp拉流超時時間
bool is_tcp = true; // 設置tcp or udp,默認一般優先tcp再嘗試udp
av_dict_set(&opts, "rtsp_transport", is_tcp ? "tcp" : "udp", 0);
av_dict_set(&opts, "stimeout", "10000000", 0); // 設置超時10秒
}
else
{
// 設置udp,http超時
av_dict_set(&opts, "timeout", "10000000", 0); // 設置超時10秒
}
// rw_timeout 網絡讀寫操作最大等待時間,微妙
av_dict_set(&opts, "rw_timeout", "10000000", 0);
// 設置av_read_frame 超時回調
in_fmt = avformat_alloc_context();
in_fmt->interrupt_callback.callback = read_interrupt_callback;
in_fmt->interrupt_callback.opaque = this;
read_start_time = time(NULL); // 每次讀流之前記錄一下起始時間
if (avformat_open_input(&in_fmt, srcPath.c_str(), NULL, &opts) < 0) {
logger.error("avformat_open_input fail");
return;
}
回調函數
int read_interrupt_callback(void *ctx)
{
using namespace xl::picture;
if (!ctx) return 0;
Demo *d = (Demo*)ctx;
uint64_t end = time(NULL);
if (end - d->read_start_time >= 10)
{
return 1; // 退出阻塞
}
return 0; // 繼續阻塞
}