说明
该错误处理不区分点直播
一、连接时报错
超时、地址错误、参数缺失
//该类错误在拉取流是try
try {
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
// 微秒 大概为设置时间的两倍
ff.setOption(TimeoutOption.RW_TIMEOUT.getKey(), "10000000");
// rtsp 默认udp 丢包 改为tcp
ff.setOption("rtsp_transport", "tcp");
ff.start();
}catch (Exception e) {
//TODO 日志
}finally {
if (grabber != null) {
grabber.stop();
grabber.release();
}
}
二、空包处理
int nullCount = 0;
if (null != f) {
//处理逻辑
}} else {
log.info("frame:" + null);
nullCount++;
if (nullCount >= 5) {
log.info("frame is null count:5 关闭任务");
break;
}else {continue;}
}
三、无数据传输
这个问题是最恼火的。你被限制了或者对方出了问题,导致你连接成功后什么都没有,空包都没有,一直占用资源不是释放还没有任何产出
解决方案:重写FFmpegFrameGrabber
复制FFmpegFrameGrabber类后 在特定位置加 回调(亲测:在调用FFmpegFrameGrabber的时候加回调不好使)
效果:规定时间(可设置)内无数据传输杀掉该线程
//1、在createDefault方法后添加 我都是直接杀线程 不用返回值
/**
* 读流超时时间 : 60秒
*/
private static final int FRAME_READ_TIMEOUT = 60000;
// 增加callbacjk 监听
private final AtomicLong lastFrameTime = new AtomicLong(System.currentTimeMillis());
private final org.bytedeco.ffmpeg.avformat.AVIOInterruptCB.Callback_Pointer cp = new org.bytedeco.ffmpeg.avformat.AVIOInterruptCB.Callback_Pointer() {
@Override
public int call(Pointer pointer) {
// 0:continue, 1:exit
if (lastFrameTime.get() + FRAME_READ_TIMEOUT < System.currentTimeMillis()) {
try {
log.info("frame read timeout 60s.---{}", System.currentTimeMillis());
Thread t = Thread.currentThread();
log.info(t.getId() + ":" + t.getName());
t.interrupt();
} catch (java.lang.Exception e) {
log.error("Thread interrupt", e);
}
return 1;
}
return 0;
}
};
//2、在startUnsafe()方法中
//if ((ret = avformat_open_input(oc, filename, f, options)) < 0) 代码前 添加
// 回调
lastFrameTime.set(System.currentTimeMillis());
oc = avformat_alloc_context();
org.bytedeco.ffmpeg.avformat.AVIOInterruptCB cb = new org.bytedeco.ffmpeg.avformat.AVIOInterruptCB();
cb.callback(cp);
oc.interrupt_callback(cb);
//3、在grabFrame方法中if (oc == null || oc.isNull()) 判断结束后添加
// 回调
lastFrameTime.set(System.currentTimeMillis());