当前位置:网站首页>FFmpeg长时间无响应的解决方法
FFmpeg长时间无响应的解决方法
2022-08-09 13:05:00 【zhouyongku】
FFmpeg长时间无响应的解决方法
需要解决的问题
1、FFmpeg去连接的时候相机不在线导致avformat_open_input等函数一直死等,造成程序卡死
2、av_read_frame的过程中相机断开连接导致读取码流一直死等
解决方法
打开流媒体之前注册FFmpeg回调函数
AVFormatContext *m_pRtspFmt = avformat_alloc_context();
m_pRtspFmt->interrupt_callback.callback = AVInterruptCallBackFun;
m_pRtspFmt->interrupt_callback.opaque = this;
回调函数类型为:
typedef struct AVIOInterruptCB {int (*callback)(void*);void *opaque;} AVIOInterruptCB;
回调函数中返回1则代表ffmpeg结束阻塞可以将操纵权交给用户线程并返回错误码
回调函数中返回0则代表ffmpeg继续阻塞直到ffmpeg正常工作为止
所以要退出死等则需要返回1
伪代码如下:
//相机连接类
class CIPCamera
{
public:
CIPCamera();
~CIPCamera();
//AVReadFrame超时回调函数
static int AVInterruptCallBackFun(void *ctx);
//读取rtsp码流线程
static DWORD WINAPI ReadStreamThread(LPVOID param);
//心跳监控线程--监控线程是否死掉
static DWORD WINAPI MonitorThread(LPVOID param);
};
int CIPCamera::AVInterruptCallBackFun(void *param)
{
CIPCamera *pCamera = (CIPCamera*)param;
if (NULL == pCamera) return 0;
if (pCamera->m_bQuitFFmpegBlock)
{
//通知FFMpeg可以从阻塞工作线程中释放操作
return 1;
}
else
{
//通知FFMpeg继续阻塞工作
return 0;
}
}
//连接相机 读取rtsp码流线程
DWORD WINAPI CIPCamera::ReadStreamThread(LPVOID param)
{
CIPCamera *pCapture = (CIPCamera*)param;
if (NULL == pCapture) return -1;
pCapture->ConnectCamera();
while (pCapture->m_bWorkOK)
{
//FFmpeg读取码流
pCapture->ReadStream();
//发送心跳
pCapture->HeartBeat();
Sleep(1);
}
return TRUE;
}
//监控线程
DWORD WINAPI CIPCamera::MonitorThread(LPVOID param)
{
CIPCamera *pCamera = (CIPCamera*)param;
if (NULL == pCamera) return -1;
while (pCamera->m_bReWork)
{
//如果心跳超时
if ( OK != pCamera->GetState(&nTimeOut);)
{
//则通知ffmpeg返回
pCamera->m_bWorkOK = FALSE;
pCamera->m_bQuitFFmpegBlock = TRUE;
}
else
{
//ffmpeg继续工作
pCamera->m_bWorkOK = TRUE;
}
Sleep(100);
}
return 0;
}
边栏推荐
猜你喜欢
快来扔鸡蛋。
程序员的七夕怎么过?不会是写代码吧
面试攻略系列(二)-- 秒杀系统
面试攻略系列(四)-- 你不知道的大厂面试
gin的中间件和路由分组
The sword refers to the offer, cuts the rope 2
[极客大挑战 2019]Upload
【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
PO、DO、TO、VO、DO、DTO、DAO、POJO都是什么?
随机推荐
GIN Bind mode to get parameters and form validation
Rmarkdown Tutorial
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
GIN a preliminary study, the environment is installed
陈强教授《机器学习及R应用》课程 第十六章作业
蓝桥历届真题-门牌制作
JZ7 重建二叉树
Professor Chen Qiang "application in machine learning and R" course chapter 17
FFmpeg多媒体文件处理(FFMPEG日志系统)
行程和用户[阅读理解法]
NC161 二叉树的中序遍历
用plot_hist_numeric()实现画直方图
5G China unicom repeater network management protocol real-time requirements
NFS pays special attention to the problem of permissions
基于 R 语言的深度学习——简单回归案例
程序员的七夕怎么过?不会是写代码吧
NC15 求二叉树的层序遍历
时间序列分析课程实验报告
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
正则表达式-re模块