Skip to content

Commit

Permalink
add player analysis timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
liangzhuohua committed Jun 13, 2024
1 parent 7f94440 commit a1f26c8
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/ffmpegDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QThread>
#include <iostream>
#include <vector>
#include <QDateTime>

#define min(a, b) (a > b ? b : a)

Expand All @@ -24,15 +25,29 @@ bool FFmpegDecoder::OpenInput(string &inputFile) {
CloseInput();
return false;
}
// 超时机制
static const int timeout = 10;
auto startTime = std::make_shared<uint64_t >();
*startTime = QDateTime::currentDateTime().toSecsSinceEpoch();
pFormatCtx->interrupt_callback.callback = [](void *ctx)->int {
uint64_t now = QDateTime::currentDateTime().toSecsSinceEpoch();
return now - *(uint64_t *)ctx > timeout;
};
pFormatCtx->interrupt_callback.opaque = startTime.get();


// 分析输入流
pFormatCtx->max_analyze_duration = 32;
// pFormatCtx->probesize = 0;
if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
CloseInput();
return false;
}

// 分析超时,退出,可能格式不正确
if(QDateTime::currentDateTime().toSecsSinceEpoch() - *startTime > timeout){
return false;
}
pFormatCtx->interrupt_callback.callback = nullptr;
pFormatCtx->interrupt_callback.opaque = nullptr;

// 打开视频/音频输入
hasVideoStream = OpenVideo();
hasAudioStream = OpenAudio();
Expand Down

0 comments on commit a1f26c8

Please sign in to comment.