1:
如果数据是rtp/rtsp传输的话,ffmpeg会每隔30s(哪里设置该值?)发送一个keepalive包,如果ipc支持GET_PARAMETER命令,就发该命令等ipc回复以确认ipc还活着。
某些ipc(IPCamera)不支持GET_PARAMETER的rtsp命令。则会通过OPTIONS *来是keepalive,不过这会导致连接断掉
原代码,及修改部分如下。先这样处理,有时间再研究原代码为什么不这么处理。

//ffmpeg/libavformat/rtspdec.c rtsp_read_packet()
if (!(rt->rtsp_flags & RTSP_FLAG_LISTEN)) {
/* send dummy request to keep TCP connection alive */
if ((av_gettime() - rt->last_cmd_time) / >= rt->timeout / ||
rt->auth_state.stale) {
if (rt->server_type == RTSP_SERVER_WMS ||
(rt->server_type != RTSP_SERVER_REAL &&
rt->get_parameter_supported)) {
ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
} else {
// ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL); //原代码
ff_rtsp_send_cmd_async(s, "OPTIONS", rt->control_uri, NULL); //修改后的代码,这样就算ipc不支持GET_PARAMETER的命令可以keepalive了
}
/* The stale flag should be reset when creating the auth response in
* ff_rtsp_send_cmd_async, but reset it here just in case we never
* called the auth code (if we didn't have any credentials set). */
rt->auth_state.stale = ;
}
}

2:
如果数据是rtp/rtsp/tcp传输,如果没有设置stimeout(socket timeout),那么server断线后av_read_frame会阻塞

AVFormatContext *pFormatCtx = NULL;
AVPacket packet;
AVDictionary *optionsDict = NULL;
char *streamurl1 = "rtsp://test:test@192.168.11.111:554/test.stream"; // Register all formats and codecs
av_register_all();
avformat_network_init(); pFormatCtx = avformat_alloc_context();
av_dict_set(&optionsDict, "rtsp_transport", "tcp", ); //采用tcp传输
av_dict_set(&optionsDict, "stimeout", "", ); //如果没有设置stimeout,那么把ipc网线拔掉,av_read_frame会阻塞(时间单位是微妙) // Open video file
if(avformat_open_input(&pFormatCtx, streamurl, NULL, &optionsDict)!=){
printf("linesize = %d\n", __LINE__);
return -; // Couldn't open file
} // Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<){
printf("linesize = %d\n", __LINE__);
return -; // Couldn't find stream information
} // Find the first video stream
int videoStream=-;
for(i=; i<pFormatCtx->nb_streams; i++){
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
} if(videoStream==-){
printf("linesize = %d\n", __LINE__);
return -; // Didn't find a video stream
} // Read frames and save first five frames to disk
while(av_read_frame(pFormatCtx, &packet)>=) {
av_free_packet(&packet);
}

如果是ffmpeg2.0.1版本的话,添加一句就ok了。可是旧版本ffmpeg1.2.*没有stimeout这个字段可以设置
那么只好自己添加了。
在ffmpeg/libavformat/rtsp.h  struct RTSPState添加一个stimeout字段

typedef struct RTSPState {
/**
* timeout of socket i/o operations.(this version do not have this segment. added by yqing, refer to ffmpeg-2.0.1)
*/
int stimeout;
} RTSPState;

在ffmpeg/libavformat/rtsp.c  ff_rtsp_options添加一个stimeout字段

const AVOption ff_rtsp_options[] = {
{ "stimeout", "timeout (in micro seconds) of socket i/o operations.", OFFSET(stimeout), AV_OPT_TYPE_INT, {.i64 = }, INT_MIN, INT_MAX, DEC }, //添加这行代码
RTSP_REORDERING_OPTS(),
{ NULL },
};

在ffmpeg/libavformat/rtsp.c  ff_rtsp_connect函数修改部分代码如下

//ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);                            //原代码
ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, "?timeout=%d", rt->stimeout); //修改后的代码

原贴地址:http://blog.chinaunix.net/uid-27091949-id-4186640.html

1:
如果数据是rtp/rtsp传输的话,ffmpeg会每隔30s(哪里设置该值?)发送一个keepalive包,如果ipc支持GET_PARAMETER命令,就发该命令等ipc回复以确认ipc还活着。
某些ipc(IPCamera)不支持GET_PARAMETER的rtsp命令。则会通过OPTIONS *来是keepalive,不过这会导致连接断掉
原代码,及修改部分如下。先这样处理,有时间再研究原代码为什么不这么处理。

点击(此处)折叠或打开

  1. //ffmpeg/libavformat/rtspdec.c rtsp_read_packet()
  2. if (!(rt->rtsp_flags & RTSP_FLAG_LISTEN)) {
  3. /* send dummy request to keep TCP connection alive */
  4. if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2 ||
  5. rt->auth_state.stale) {
  6. if (rt->server_type == RTSP_SERVER_WMS ||
  7. (rt->server_type != RTSP_SERVER_REAL &&
  8. rt->get_parameter_supported)) {
  9. ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
  10. } else {
  11. // ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);                 //原代码
  12. ff_rtsp_send_cmd_async(s, "OPTIONS", rt->control_uri, NULL);        //修改后的代码,这样就算ipc不支持GET_PARAMETER的命令可以keepalive了
  13. }
  14. /* The stale flag should be reset when creating the auth response in
  15. * ff_rtsp_send_cmd_async, but reset it here just in case we never
  16. * called the auth code (if we didn't have any credentials set). */
  17. rt->auth_state.stale = 0;
  18. }
  19. }

2:
如果数据是rtp/rtsp/tcp传输,如果没有设置stimeout(socket timeout),那么server断线后av_read_frame会阻塞

点击(此处)折叠或打开

  1. AVFormatContext *pFormatCtx = NULL;
  2. AVPacket packet;
  3. AVDictionary *optionsDict = NULL;
  4. char *streamurl1 = "rtsp://test:test@192.168.11.111:554/test.stream";
  5. // Register all formats and codecs
  6. av_register_all();
  7. avformat_network_init();
  8. pFormatCtx = avformat_alloc_context();
  9. av_dict_set(&optionsDict, "rtsp_transport", "tcp", 0);                //采用tcp传输
  10. av_dict_set(&optionsDict, "stimeout", "2000000", 0);                  //如果没有设置stimeout,那么把ipc网线拔掉,av_read_frame会阻塞(时间单位是微妙)
  11. // Open video file
  12. if(avformat_open_input(&pFormatCtx, streamurl, NULL, &optionsDict)!=0){
  13. printf("linesize = %d\n", __LINE__);
  14. return -1; // Couldn't open file
  15. }
  16. // Retrieve stream information
  17. if(avformat_find_stream_info(pFormatCtx, NULL)<0){
  18. printf("linesize = %d\n", __LINE__);
  19. return -1; // Couldn't find stream information
  20. }
  21. // Find the first video stream
  22. int videoStream=-1;
  23. for(i=0; i<pFormatCtx->nb_streams; i++){
  24. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  25. videoStream=i;
  26. break;
  27. }
  28. }
  29. if(videoStream==-1){
  30. printf("linesize = %d\n", __LINE__);
  31. return -1; // Didn't find a video stream
  32. }
  33. // Read frames and save first five frames to disk
  34. while(av_read_frame(pFormatCtx, &packet)>=0) {
  35. av_free_packet(&packet);
  36. }

如果是ffmpeg2.0.1版本的话,添加一句就ok了。可是旧版本ffmpeg1.2.*没有stimeout这个字段可以设置
那么只好自己添加了。
在ffmpeg/libavformat/rtsp.h  struct RTSPState添加一个stimeout字段

点击(此处)折叠或打开

  1. typedef struct RTSPState {
  2. /**
  3. * timeout of socket i/o operations.(this version do not have this segment. added by yqing, refer to ffmpeg-2.0.1)
  4. */
  5. int stimeout;
  6. } RTSPState;

在ffmpeg/libavformat/rtsp.c  ff_rtsp_options添加一个stimeout字段

点击(此处)折叠或打开

  1. const AVOption ff_rtsp_options[] = {
  2. { "stimeout", "timeout (in micro seconds) of socket i/o operations.", OFFSET(stimeout), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC },    //添加这行代码
  3. RTSP_REORDERING_OPTS(),
  4. { NULL },
  5. };

在ffmpeg/libavformat/rtsp.c  ff_rtsp_connect函数修改部分代码如下

点击(此处)折叠或打开

  1. //ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);                            //原代码
  2. ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, "?timeout=%d", rt->stimeout);     //修改后的代码

ffmpeg问题汇总及解决方案 <设置avformat_open_input 超时><转>的更多相关文章

  1. ffmpeg设置avformat_open_input( )超时 -stimeout

    ffmpeg用avformat_open_input()解析网络流时,默认是阻塞的. 当遇到解析错误的网络流时,会导致该函数长时间不返回. 为此可以设置ffmpeg的-stimeout 的参数,要注意 ...

  2. 【轮询】【ajax】【js】【spring boot】ajax超时请求:前端轮询处理超时请求解决方案 + spring boot服务设置接口超时时间的设置

    场景描述: ajax设置timeout在本机测试有效,但是在生产环境等外网环境无效的问题 1.ajax的timeout属性设置 前端请求超时事件[网络连接不稳定时候,就无效了] var data = ...

  3. flume常见异常汇总以及解决方案

    flume常见异常汇总以及解决方案 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 实际生产环境中,我用flume将kafka的数据定期的往hdfs集群中上传数据,也遇到过一系列的坑 ...

  4. H5项目常见问题汇总及解决方案

    H5项目常见问题汇总及解决方案 H5   2015-12-06 10:15:33 发布 您的评价:       4.5   收藏     4收藏 H5项目常见问题及注意事项 Meta基础知识: H5页 ...

  5. oracle连接由于防火墙设置导致超时的问题

        当应用程序使用数据库连接池进行数据连接时,防火墙的设置有可能会导致连接出现超时或者被重置的问题.当从数据库读数据的时候 有可能会 Connection timed out, 这是由于应用会缓存 ...

  6. 【转】使用kettle工具遇到的问题汇总及解决方案

    使用kettle工具遇到的问题汇总及解决方案   转载文章版权声明:本文转载,原作者薄海 ,原文网址链接 http://blog.csdn.net/bohai0409/article/details/ ...

  7. H5 常见问题汇总及解决方案

    原文链接:http://mp.weixin.qq.com/s/JVUpsz9QHsNV0_7U-3HCMg H5 项目常见问题汇总及解决方案 -- 由钟平勇分享 转自 https://github.c ...

  8. C# 的tcp Socket设置自定义超时时间

    简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的TCP Socket (异步方式) C# 的tcp Socket设置自定义超时时间 C# TCP ...

  9. 【开源项目13】Volley框架 以及 设置request超时时间

    Volley提供了优美的框架,使android程序网络访问更容易.更快. Volley抽象实现了底层的HTTP Client库,我们不需关注HTTP Client细节,专注于写出更加漂亮.干净的RES ...

随机推荐

  1. 使 WPF 支持触摸板的横向滚动

    微软终于开始学苹果一样好好做触摸板了(就是键盘空格键下面那一大块).然而鉴于以前没有好好做,以至于 WPF 程序甚至都没有对触摸板的横向滚动提供支持(竖向滚动是直接使用了 MouseWheel,汗-- ...

  2. jquery ajax 超时设置

    自:jquery ajax超时设置 var ajaxTimeoutTest = $.ajax({ url:'',  //请求的URL timeout : 1000, //超时时间设置,单位毫秒 typ ...

  3. jQuery.1.9 live 代替事件 on 新增内容无法触发事件

    如果是新增 append 或者 html() 事件添加的内容,无法触发 click 事件, 在1.9 可以用live 事件来代替 1.9以后用 <div class="search-r ...

  4. 《DSP using MATLAB》示例Example 8.15

  5. 【angularJS】简介

    简介 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 指令 扩展了 HTML,且通过 表达式 绑 ...

  6. 开启opcache提高性能

    在开启opcache之前,我们先介绍一下编译与解释: 编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快:而解释器则是只在执行程 ...

  7. rem自适应原理

    rem自适应原理 rem是根据html的font-size大小来变化,正是基于这个出发,我们可以在每一个设备下根据设备的宽度设置对应的html字号,从而实现了自适应布局.更多介绍请看这篇文章:rem是 ...

  8. java-文件流正确关闭资源

    用文件流来拷贝一个文件,用到文件字节输入流(FileInputStream)和文件字节输出流(FileOutputStream),用输入流把字节文件读到缓冲数组中,然后将缓冲数组中的字节写到文件中,就 ...

  9. spring--集合注入(常规方法)

    数据,list,set,map,Properties 集合注入 package Spring_collections; /** * Created by luozhitao on 2017/8/11. ...

  10. odoo10 修改产品单价的小数点位数

    odoo10 修改产品单价的小数点位数 由于产品价格原因,单价需要保留小数点后 5 位,所以需要修改单价的小数点位数. 开启开发模式 找到数据库编辑 找到小数点精度 修改产品的小数点位数