当前位置:网站首页>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;
}
边栏推荐
- 陈强教授《机器学习及R应用》课程 第十四章作业
- puzzle(016.5)逻辑电路
- 技嘉显卡 RGBFusion 不能调光解决方法
- error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 13 Assignment
- 陈强教授《机器学习及R应用》课程 第十六章作业
- ArcEngine(十)创建矢量图层
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 16 Assignment
- 时间序列分析课程实验报告
- 蓝桥历届真题-蛇形填数
猜你喜欢

为什么文字不贴合边

剑指offer,剪绳子2

关于做2D游戏时,Canvas边界显示在Game窗口的问题

Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project

Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation

WSA toolkit installed app store tip doesn't work how to solve?

gin's middleware and routing grouping

FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)

蓝桥历届真题-既约分数

Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
随机推荐
为什么文字不贴合边
JS本地存储 sessionStorage和localStorage
Unity3d_API_Gyroscope 陀螺仪的接口
Q_04_06 把它放在一起:传送
Process/Thread Related in Sandbox - 2
ftplib+ tqdm upload and download progress bar
用plot_hist_numeric()实现画直方图
蓝桥历届真题-跑步锻炼
Q_06_04 语句和其他构造
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
The FPGA - work summary recently
NC193 二叉树的前序遍历
Deep Learning Based on R Language - Simple Regression Case
面试攻略系列(二)-- 秒杀系统
剑指 Offer 43. 1~n 整数中 1 出现的次数(递归、数学)
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
R 语言 2010.1至2021.12艾滋病每月发病人数 时间序列分析
javscript基础易错点集合
FFmpeg multimedia file processing (FFMPEG logging system)
gin's middleware and routing grouping