Fix VideoPlayer from stop after just few frames

This commit is contained in:
2026-05-07 18:17:33 +10:00
parent a526aa6cdb
commit 2e0fee5e3d
6 changed files with 55 additions and 32 deletions
+22 -13
View File
@@ -252,9 +252,9 @@ namespace ANSCENTER {
// --- HW decode path ---
if (_hwDecodeActive && _hwPlayer) {
_hwPlayer->play(); // starts read/video/audio threads
_hwPlayer->clearLooped(); // drop any wrap state from a prior session
_hwEOF = false;
_hwFrameCount = 0;
_hwLastElapseMs = 0;
_isPlaying = true;
// Wait for first frame outside the mutex to let decode threads run
@@ -312,7 +312,6 @@ namespace ANSCENTER {
_hwCudaAccel = false;
_hwEOF = false;
_hwFrameCount = 0;
_hwLastElapseMs = 0;
}
else {
// --- cv::VideoCapture fallback ---
@@ -526,21 +525,30 @@ namespace ANSCENTER {
_hwCudaAccel = _hwPlayer->isCudaHWAccel();
_totalFrames = static_cast<int64_t>(_hwPlayer->getDuration() / 1000.0 * 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",
"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),
__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
// (file_player.cpp:434), so ANSVideoPlayer must detect the wrap itself.
// imgPts from getImage() is a monotonic call-counter, not a real PTS —
// 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.
const int64_t elapseMs = _hwPlayer->getElapse();
if (_hwFrameCount > 10 && _hwLastElapseMs > 0 &&
elapseMs + 500 < _hwLastElapseMs) {
// (file_player.cpp:434). It now flips m_bLooped when that happens; we
// poll hasLooped() to detect the wrap.
//
// Why not getElapse(): m_nCurPos is non-monotonic in normal playback —
// MP4 packets are in decode order so video PTS oscillates with B-frames,
// AND the audio thread also writes m_nCurPos with audio PTS. Either can
// cause a >500ms backwards drop and falsely trip EOF mid-playback.
if (_hwPlayer->hasLooped()) {
_hwEOF = true;
if (_resHeight <= 0 || _resWidth <= 0) { _resHeight = imgH; _resWidth = imgW; }
_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++;
else _previousPTS = 0;
pts = _previousPTS;
ANS_DBG("ANSVP_EOF",
"hasLooped() -> EOF locked at frameCount=%lld elapseMs=%lld",
(long long)_hwFrameCount, (long long)_hwPlayer->getElapse());
return _previousImage;
}
_hwLastElapseMs = elapseMs;
cv::Mat result = frame; // CFilePlayer returns owned Mat (already cloned internally)
// Apply cropping if enabled