Fix VideoPlayer from stop after just few frames
This commit is contained in:
+1
Submodule .claude/worktrees/lucid-grothendieck-201ca6 added at a526aa6cdb
@@ -59,6 +59,7 @@ CFilePlayer::CFilePlayer()
|
|||||||
, m_bSeek(0)
|
, m_bSeek(0)
|
||||||
, m_dSeekPos(0)
|
, m_dSeekPos(0)
|
||||||
, m_nNalLength(0)
|
, m_nNalLength(0)
|
||||||
|
, m_bLooped(FALSE)
|
||||||
, m_pFormatContext(NULL)
|
, m_pFormatContext(NULL)
|
||||||
, m_hReadThread(0)
|
, m_hReadThread(0)
|
||||||
, m_hVideoThread(0)
|
, m_hVideoThread(0)
|
||||||
@@ -282,6 +283,7 @@ BOOL CFilePlayer::play()
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_bPlaying = TRUE;
|
m_bPlaying = TRUE;
|
||||||
|
m_bLooped = FALSE; // fresh playback — no wrap yet
|
||||||
|
|
||||||
if (m_nVideoIndex >= 0)
|
if (m_nVideoIndex >= 0)
|
||||||
{
|
{
|
||||||
@@ -441,6 +443,11 @@ BOOL CFilePlayer::readFrame()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// File wrapped — flag it so the consumer (ANSVideoPlayer) can detect
|
||||||
|
// EOF without watching m_nCurPos, which is non-monotonic during normal
|
||||||
|
// playback (decode-order packet PTS + audio thread also writes to it).
|
||||||
|
m_bLooped = TRUE;
|
||||||
|
|
||||||
if (av_read_frame(m_pFormatContext, &pkt) != 0)
|
if (av_read_frame(m_pFormatContext, &pkt) != 0)
|
||||||
{
|
{
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ public:
|
|||||||
BOOL seek(int pos);
|
BOOL seek(int pos);
|
||||||
int64 getElapse() {return m_nCurPos;};
|
int64 getElapse() {return m_nCurPos;};
|
||||||
int64 getDuration() {return m_nDuration;}
|
int64 getDuration() {return m_nDuration;}
|
||||||
|
// Set inside readFrame() when av_read_frame returns AVERROR_EOF and the
|
||||||
|
// reader auto-loops via av_seek_frame. Caller (ANSVideoPlayer) polls this
|
||||||
|
// to detect EOF without relying on m_nCurPos, which is non-monotonic
|
||||||
|
// (decode-order packet PTS + audio thread also writes to it).
|
||||||
|
BOOL hasLooped() {return m_bLooped;}
|
||||||
|
void clearLooped() {m_bLooped = FALSE;}
|
||||||
int getVideoCodec();
|
int getVideoCodec();
|
||||||
int getAudioCodec();
|
int getAudioCodec();
|
||||||
BOOL onRecord();
|
BOOL onRecord();
|
||||||
@@ -79,6 +85,7 @@ private:
|
|||||||
int m_bSeek; // seek request
|
int m_bSeek; // seek request
|
||||||
double m_dSeekPos; // seek position, nit is millisecond
|
double m_dSeekPos; // seek position, nit is millisecond
|
||||||
int m_nNalLength;
|
int m_nNalLength;
|
||||||
|
BOOL m_bLooped; // set by readFrame() when EOF triggered an av_seek_frame back to start
|
||||||
|
|
||||||
AVFormatContext * m_pFormatContext; // format context
|
AVFormatContext * m_pFormatContext; // format context
|
||||||
|
|
||||||
|
|||||||
@@ -252,9 +252,9 @@ namespace ANSCENTER {
|
|||||||
// --- HW decode path ---
|
// --- HW decode path ---
|
||||||
if (_hwDecodeActive && _hwPlayer) {
|
if (_hwDecodeActive && _hwPlayer) {
|
||||||
_hwPlayer->play(); // starts read/video/audio threads
|
_hwPlayer->play(); // starts read/video/audio threads
|
||||||
|
_hwPlayer->clearLooped(); // drop any wrap state from a prior session
|
||||||
_hwEOF = false;
|
_hwEOF = false;
|
||||||
_hwFrameCount = 0;
|
_hwFrameCount = 0;
|
||||||
_hwLastElapseMs = 0;
|
|
||||||
_isPlaying = true;
|
_isPlaying = true;
|
||||||
|
|
||||||
// Wait for first frame outside the mutex to let decode threads run
|
// Wait for first frame outside the mutex to let decode threads run
|
||||||
@@ -312,7 +312,6 @@ namespace ANSCENTER {
|
|||||||
_hwCudaAccel = false;
|
_hwCudaAccel = false;
|
||||||
_hwEOF = false;
|
_hwEOF = false;
|
||||||
_hwFrameCount = 0;
|
_hwFrameCount = 0;
|
||||||
_hwLastElapseMs = 0;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// --- cv::VideoCapture fallback ---
|
// --- cv::VideoCapture fallback ---
|
||||||
@@ -526,21 +525,30 @@ namespace ANSCENTER {
|
|||||||
_hwCudaAccel = _hwPlayer->isCudaHWAccel();
|
_hwCudaAccel = _hwPlayer->isCudaHWAccel();
|
||||||
_totalFrames = static_cast<int64_t>(_hwPlayer->getDuration() / 1000.0 * 30.0);
|
_totalFrames = static_cast<int64_t>(_hwPlayer->getDuration() / 1000.0 * 30.0);
|
||||||
_fps = 30.0;
|
_fps = 30.0;
|
||||||
std::string hwType = _hwCudaAccel ? "CUDA" : "D3D11VA/DXVA2";
|
// Decoder mode: CUDA when zero-copy NVDEC is active; otherwise the
|
||||||
|
// underlying CFilePlayer is running software decode (HW decoding is
|
||||||
|
// disabled by default in Setup() — saves VRAM). The old log message
|
||||||
|
// always said "D3D11VA/DXVA2" which was misleading.
|
||||||
|
const std::string hwType = _hwCudaAccel ? "CUDA" : "SW";
|
||||||
_logger.LogInfo("ANSVIDEOPLAYER::GetImage",
|
_logger.LogInfo("ANSVIDEOPLAYER::GetImage",
|
||||||
"HW decode ACTIVE (" + hwType + ") GPU[" + std::to_string(_hwGpuIndex) +
|
"Decode ACTIVE (" + hwType + ") GPU[" + std::to_string(_hwGpuIndex) +
|
||||||
"] " + std::to_string(_resWidth) + "x" + std::to_string(_resHeight),
|
"] " + std::to_string(_resWidth) + "x" + std::to_string(_resHeight),
|
||||||
__FILE__, __LINE__);
|
__FILE__, __LINE__);
|
||||||
|
ANS_DBG("ANSVP_EOF",
|
||||||
|
"first frame received: %dx%d totalFrames~=%lld duration=%lldms",
|
||||||
|
_resWidth, _resHeight,
|
||||||
|
(long long)_totalFrames, (long long)_hwPlayer->getDuration());
|
||||||
}
|
}
|
||||||
|
|
||||||
// EOF detection: CFilePlayer auto-loops via av_seek_frame on AVERROR_EOF
|
// EOF detection: CFilePlayer auto-loops via av_seek_frame on AVERROR_EOF
|
||||||
// (file_player.cpp:434), so ANSVideoPlayer must detect the wrap itself.
|
// (file_player.cpp:434). It now flips m_bLooped when that happens; we
|
||||||
// imgPts from getImage() is a monotonic call-counter, not a real PTS —
|
// poll hasLooped() to detect the wrap.
|
||||||
// use getElapse() (ms of real video position, updated from packet.pts
|
//
|
||||||
// in file_player.cpp:608). When the file loops, elapse drops back to ~0.
|
// Why not getElapse(): m_nCurPos is non-monotonic in normal playback —
|
||||||
const int64_t elapseMs = _hwPlayer->getElapse();
|
// MP4 packets are in decode order so video PTS oscillates with B-frames,
|
||||||
if (_hwFrameCount > 10 && _hwLastElapseMs > 0 &&
|
// AND the audio thread also writes m_nCurPos with audio PTS. Either can
|
||||||
elapseMs + 500 < _hwLastElapseMs) {
|
// cause a >500ms backwards drop and falsely trip EOF mid-playback.
|
||||||
|
if (_hwPlayer->hasLooped()) {
|
||||||
_hwEOF = true;
|
_hwEOF = true;
|
||||||
if (_resHeight <= 0 || _resWidth <= 0) { _resHeight = imgH; _resWidth = imgW; }
|
if (_resHeight <= 0 || _resWidth <= 0) { _resHeight = imgH; _resWidth = imgW; }
|
||||||
_previousImage = cv::Mat(_resHeight, _resWidth, CV_8UC3, cv::Scalar(0, 0, 0));
|
_previousImage = cv::Mat(_resHeight, _resWidth, CV_8UC3, cv::Scalar(0, 0, 0));
|
||||||
@@ -549,11 +557,12 @@ namespace ANSCENTER {
|
|||||||
if (_previousPTS < std::numeric_limits<int64_t>::max()) _previousPTS++;
|
if (_previousPTS < std::numeric_limits<int64_t>::max()) _previousPTS++;
|
||||||
else _previousPTS = 0;
|
else _previousPTS = 0;
|
||||||
pts = _previousPTS;
|
pts = _previousPTS;
|
||||||
|
ANS_DBG("ANSVP_EOF",
|
||||||
|
"hasLooped() -> EOF locked at frameCount=%lld elapseMs=%lld",
|
||||||
|
(long long)_hwFrameCount, (long long)_hwPlayer->getElapse());
|
||||||
return _previousImage;
|
return _previousImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
_hwLastElapseMs = elapseMs;
|
|
||||||
|
|
||||||
cv::Mat result = frame; // CFilePlayer returns owned Mat (already cloned internally)
|
cv::Mat result = frame; // CFilePlayer returns owned Mat (already cloned internally)
|
||||||
|
|
||||||
// Apply cropping if enabled
|
// Apply cropping if enabled
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ namespace ANSCENTER
|
|||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
cv::VideoCapture cap;
|
cv::VideoCapture cap;
|
||||||
int _resWidth;
|
int _resWidth = 0;
|
||||||
int _resHeight;
|
int _resHeight = 0;
|
||||||
int64_t _totalFrames;
|
int64_t _totalFrames = 0;
|
||||||
int64_t _currentFrame;
|
int64_t _currentFrame = 0;
|
||||||
int64_t _previousPTS;
|
int64_t _previousPTS = 0;
|
||||||
double _fps;
|
double _fps = 0.0;
|
||||||
bool _isPlaying;
|
bool _isPlaying;
|
||||||
std::string _lastJpegImage;
|
std::string _lastJpegImage;
|
||||||
cv::Mat _previousImage;
|
cv::Mat _previousImage;
|
||||||
@@ -50,7 +50,6 @@ namespace ANSCENTER
|
|||||||
bool _hwCudaAccel = false; // true = NVIDIA CUDA zero-copy available
|
bool _hwCudaAccel = false; // true = NVIDIA CUDA zero-copy available
|
||||||
bool _hwEOF = false; // true when video reached end of file
|
bool _hwEOF = false; // true when video reached end of file
|
||||||
int64_t _hwFrameCount = 0; // frame counter for EOF detection
|
int64_t _hwFrameCount = 0; // frame counter for EOF detection
|
||||||
int64_t _hwLastElapseMs = 0; // last CFilePlayer elapse (ms); EOF detected when it wraps backwards
|
|
||||||
ANSVIDEOPLAYER();
|
ANSVIDEOPLAYER();
|
||||||
~ANSVIDEOPLAYER() noexcept;
|
~ANSVIDEOPLAYER() noexcept;
|
||||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||||
|
|||||||
@@ -1481,7 +1481,7 @@ int VideoPlayerClientTest() {
|
|||||||
int64_t pts = 0;
|
int64_t pts = 0;
|
||||||
ANSVIDEOPLAYER* videoClient;
|
ANSVIDEOPLAYER* videoClient;
|
||||||
|
|
||||||
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
|
std::string testVideoFile = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";//"E:\\Programs\\DemoAssets\\ALPR_Test\\cam17.mp4"
|
||||||
CreateANSVideoPlayerHandle(&videoClient, "", testVideoFile.c_str());
|
CreateANSVideoPlayerHandle(&videoClient, "", testVideoFile.c_str());
|
||||||
|
|
||||||
StartVideoPlayer(&videoClient);
|
StartVideoPlayer(&videoClient);
|
||||||
@@ -1580,7 +1580,7 @@ int FilePlayerClientCVTest() {
|
|||||||
if ((index == 1800) || (index == 2200) || (index == 2500)) { StopFilePlayer(&filePlayerClient); }
|
if ((index == 1800) || (index == 2200) || (index == 2500)) { StopFilePlayer(&filePlayerClient); }
|
||||||
if ((index == 2000) || (index == 2300) || (index == 2700)) { StartFilePlayer(&filePlayerClient); }
|
if ((index == 2000) || (index == 2300) || (index == 2700)) { StartFilePlayer(&filePlayerClient); }
|
||||||
if (index > 20000) break;*/
|
if (index > 20000) break;*/
|
||||||
if (index > 2000) break;
|
// if (index > 2000) break;
|
||||||
auto start = std::chrono::system_clock::now();
|
auto start = std::chrono::system_clock::now();
|
||||||
cv::Mat* image = nullptr;
|
cv::Mat* image = nullptr;
|
||||||
GetFilePlayerCVImage(&filePlayerClient, width, height, pts, &image);
|
GetFilePlayerCVImage(&filePlayerClient, width, height, pts, &image);
|
||||||
@@ -1647,17 +1647,17 @@ int main()
|
|||||||
// resolved inside ANSCV.dll (which is linked against libavcodec etc.),
|
// resolved inside ANSCV.dll (which is linked against libavcodec etc.),
|
||||||
// so this works without the unit test having to link FFmpeg itself.
|
// so this works without the unit test having to link FFmpeg itself.
|
||||||
//ANSCV_PrintFFmpegLicense_S();
|
//ANSCV_PrintFFmpegLicense_S();
|
||||||
FilePlayerClientDoubleDestroy();
|
//FilePlayerClientDoubleDestroy();
|
||||||
FilePlayerClientCVTest();
|
//FilePlayerClientCVTest();
|
||||||
VideoPlayerClientDoubleDestroy();
|
//VideoPlayerClientDoubleDestroy();
|
||||||
VideoPlayerClientTest();
|
// VideoPlayerClientTest();
|
||||||
//OpenCVFunctionTest();
|
//OpenCVFunctionTest();
|
||||||
//GenerateVideo();
|
//GenerateVideo();
|
||||||
//VideoTestClient();
|
//VideoTestClient();
|
||||||
// TestGetImage();
|
// TestGetImage();
|
||||||
//PureOpenCV();
|
//PureOpenCV();
|
||||||
//RSTPTestClient();
|
//RSTPTestClient();
|
||||||
//RSTPTestCVClient();
|
RSTPTestCVClient();
|
||||||
//TestCreateImageFromJpegStringFile();
|
//TestCreateImageFromJpegStringFile();
|
||||||
//TestCreateImageFromFile();
|
//TestCreateImageFromFile();
|
||||||
//for (int i = 0; i < 100; i++) {
|
//for (int i = 0; i < 100; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user