最近在做一個從rtmp服務器中拉流音頻實時數據會延遲播放的問題,從rtmp播放端<拉流音頻數據端>發現,是探測時間太長了,超過了5s,播放數據就延遲播放了5second,
卡在了這個函數:avformat_find_stream_info(),我通過ffplay的以下命令可以解決播放延時的問題:
參考網址:http://blog.csdn.net/cai6811376/article/details/52637158
使用ffplay播放視頻源時,rtsp/rtmp等,會有一定的延時,這裏我們可以通過設置ffplay播放參數將延時控制到最小。
ffplay.exe -i rtmp://xxxxxxx -fflags nobuffer
減少緩衝
也可以減少分析碼流的時間
ffplay.exe -i rtmp://xxxxxxx -analyzeduration 1000000
碼流分析時間設置,單位爲微秒
RTSP低延時播放:
ffplay.exe -i rtsp://xxx -fflags nobuffer -analyzeduration 1000000 -rtsp_transport tcp
ffmpeg中avformat_stream_info函數阻塞時間太長
在使用ffmpeg播放網絡流中,在執行到avformat_stream_info函數會阻塞5秒左右,這樣造成播放等待時間過長,影響
用戶體驗,經試驗,修改函數裏面AVFormatContext參數,probesize和max_analyze_duration值大小
通過AVDictionary來改變AVFormatContext結構體裏參數。
AVDictionary * avdic = NULL;
av_dict_set(&avdic,"probesize","2048",0);
av_dict_set(&avdic,"max_analyze_duration","1000",0);
avforamt_open_input(&pFormatCtx,url, NULL, &avdic);
avformat_find_stream_info(pFormatCtx, NULL);
我的實際代碼如下:
AVDictionary* pOptions = NULL;
pFormatCtx->probesize = 1 *1024;
pFormatCtx->max_analyze_duration = 1 * AV_TIME_BASE;
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx,&pOptions)<0)
{
printf("Couldn't find stream information.\n");
return -1;
}
以下代碼也是可以的:
AVDictionary * avdic = NULL;
av_dict_set(&avdic,"probesize","2048",0);
av_dict_set(&avdic,"max_analyze_duration","10",0);
avformat_open_input(&pFormatCtx,url, NULL, &avdic);
avformat_find_stream_info(pFormatCtx, NULL);
這樣就不會有太大的延時問題,我設置了服務器的緩衝大小等方式沒有用,通過上述方法就可以了。