Skip to content

Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.

C++

Live reconnect fixes: flush instead of skip_frame, skip damaged network audio, tolerate Waiting in the preview skip loop

Veyra Goal Agent2ad5559547d2
diff --git a/include/veyra/sink/WasapiAudioSink.h b/include/veyra/sink/WasapiAudioSink.h
index e2048f4d..f9e2a102 100644
--- a/include/veyra/sink/WasapiAudioSink.h
+++ b/include/veyra/sink/WasapiAudioSink.h
@@ -163,6 +163,7 @@ private:
// Shared session: reposition the decoder and PCM ring at `target` without moving
// the (shared) demuxer. The engine has already repositioned the session for a seek.
bool rewindSession(double target);
+ void noteDamagedAudio(int code);
void trimRingBefore(double targetMs); // mutex_ held
friend class AudioThread;
@@ -175,6 +176,9 @@ private:
bool sessionDiscontinuity_ = false; // next packet starts a new live connection: reset the decoder
std::chrono::steady_clock::time_point prefillDeadline_{};
bool prefillWait_ = false; // audio thread: wait for session packets (open / seek prefill)
+ uint64_t damagedAudioPackets_ = 0; // network audio packets the decoder rejected and we skipped
+ unsigned damagedAudioRuns_ = 0;
+ std::chrono::steady_clock::time_point damagedAudioLogAt_{};
media::InputInterrupt inputInterrupt_;
AVCodecContext* codecCtx_ = nullptr;
AVRational streamTimeBase_{1, 1000};
diff --git a/include/veyra/source/MediaFileSource.h b/include/veyra/source/MediaFileSource.h
index bf837fba..d6fe42b0 100644
--- a/include/veyra/source/MediaFileSource.h
+++ b/include/veyra/source/MediaFileSource.h
@@ -53,6 +53,7 @@ private:
bool fallbackToSoftware(std::string_view reason);
void beginDiscontinuity();
void resetCorruptionState();
+ void startKeyframeWait();
bool tolerateDecoderError();
media::FFmpegDemuxer demuxer_;
@@ -63,7 +64,7 @@ private:
bool nonBlocking_=false,tolerant_=false,waitKeyframe_=false,pendingDiscontinuity_=false;
media::CorruptPolicy corruptPolicy_=media::CorruptPolicy::Conceal;
uint64_t corruptPacketsSeen_=0,corruptFramesSeen_=0,decoderErrorsSeen_=0;
- std::chrono::steady_clock::time_point lastReadCall_{},noOutputSince_{},lastCorruptLog_{},lastForcedReconnect_{},errorWindowStart_{};
+ std::chrono::steady_clock::time_point lastReadCall_{},noOutputSince_{},lastCorruptLog_{},lastForcedReconnect_{},errorWindowStart_{},waitKeyframeSince_{};
unsigned errorsInWindow_=0;
SourceInfo info_{};
uint64_t sequence_ = 0;
diff --git a/src/engine/EngineController.cpp b/src/engine/EngineController.cpp
index 5e427104..a1192ed2 100644
--- a/src/engine/EngineController.cpp
+++ b/src/engine/EngineController.cpp
@@ -2032,6 +2032,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
pipeline::FramePacket next;const AVFrame* raw=nullptr;
const auto rs=activeSource->read(next,&raw);
if(rs==source::SourceReadStatus::Eos)break; // final decoded candidate is kept
+ if(rs==source::SourceReadStatus::Waiting)break; // network buffering: nothing newer yet, keep this candidate
if(rs!=source::SourceReadStatus::Frame||next.pts.isUnknown()){previewSourceFailed=true;break;}
const double nextPts=next.pts.toDouble()*1000;
if(nextPts+0.1<discardBefore)continue; // post-seek decode residue keeps dropping
diff --git a/src/media/DemuxSession.cpp b/src/media/DemuxSession.cpp
index 2ac7d5df..dfd41f38 100644
--- a/src/media/DemuxSession.cpp
+++ b/src/media/DemuxSession.cpp
@@ -854,6 +854,9 @@ void DemuxSession::flushPendingAudio(int64_t anchorUs) {
}
const AVRational tb{streams_[size_t(audio)].timeBaseNum, streams_[size_t(audio)].timeBaseDen};
const int64_t offsetTb = av_rescale_q(offsetUs_, AV_TIME_BASE_Q, tb);
+ if (!pendingAudio_.empty())
+ log::info("net-session", std::format("live reconnect: {} audio packets waited for the keyframe, raw {}..{} ms", pendingAudio_.size(),
+ pendingAudio_.front().startUs / 1000, pendingAudio_.back().startUs / 1000));
for (auto& item : pendingAudio_) {
if (item.startUs + 5000 < anchorUs) {
av_packet_free(&item.packet);
@@ -916,7 +919,14 @@ void DemuxSession::onPacket(AVPacket* pkt) {
gateKeyframe_ = false;
videoDiscontinuityPending_ = true;
audioDiscontinuityPending_ = true;
- log::info("net-session", std::format("live reconnect: keyframe found, timeline offset {} ms", offsetUs_ / 1000));
+ int64_t videoEnd, audioEnd;
+ {
+ std::lock_guard lock(m_);
+ videoEnd = video_.lastEndUs;
+ audioEnd = audio_.lastEndUs;
+ }
+ log::info("net-session", std::format("live reconnect: keyframe at {} ms (raw), previous end video {} ms audio {} ms, timeline offset {} ms, pending audio {} packets",
+ startUs / 1000, videoEnd / 1000, audioEnd / 1000, offsetUs_ / 1000, pendingAudio_.size()));
flushPendingAudio(startUs);
} else {
if (startUs == AV_NOPTS_VALUE || pendingAudioBytes_ > (8 << 20)) {
diff --git a/src/sink/WasapiAudioSink.cpp b/src/sink/WasapiAudioSink.cpp
index 000b2e2b..21966823 100644
--- a/src/sink/WasapiAudioSink.cpp
+++ b/src/sink/WasapiAudioSink.cpp
@@ -69,6 +69,16 @@ bool AudioPipeline::open(const std::wstring& path,const std::atomic<bool>* cance
return selectTrack(si);
}
+void AudioPipeline::noteDamagedAudio(int code)
+{
+ ++damagedAudioPackets_;
+ const auto now = std::chrono::steady_clock::now();
+ if (now - damagedAudioLogAt_ >= std::chrono::seconds(1)) {
+ damagedAudioLogAt_ = now;
+ log::warn("audio", std::format("damaged audio data from the network skipped (code={}, packets so far {})", code, damagedAudioPackets_));
+ }
+}
+
bool AudioPipeline::open(std::shared_ptr<media::DemuxSession> session)
{
if(!session||thread_.joinable())return false;
@@ -497,6 +507,12 @@ void AudioPipeline::decodeBlock()
break;
}
if (received != AVERROR(EAGAIN)) {
+ // A shared network session delivers packets from a link that loses data: one damaged
+ // audio packet is skipped, not the end of the sound (local files stay strict).
+ if (session_ && received == AVERROR_INVALIDDATA && ++damagedAudioRuns_ <= 8) {
+ noteDamagedAudio(received);
+ continue;
+ }
log::error("audio", std::format("decode receive failed code={}", received));
decodedEof_ = true;
break;
@@ -540,7 +556,15 @@ void AudioPipeline::decodeBlock()
if (sent == 0) {
av_packet_unref(packet_);
havePacket_ = false;
+ damagedAudioRuns_ = 0;
} else if (sent != AVERROR(EAGAIN)) {
+ if (session_ && sent == AVERROR_INVALIDDATA) {
+ // Damaged network data (a live source re-joined mid-frame, lost packets): skip this packet.
+ noteDamagedAudio(sent);
+ av_packet_unref(packet_);
+ havePacket_ = false;
+ continue;
+ }
log::error("audio", std::format("decode send failed code={}", sent));
decodedEof_ = true;
break;
diff --git a/src/source/MediaFileSource.cpp b/src/source/MediaFileSource.cpp
index 12427af9..77639f97 100644
--- a/src/source/MediaFileSource.cpp
+++ b/src/source/MediaFileSource.cpp
@@ -436,16 +436,17 @@ readAgain: // a damaged picture was dropped (corrupt policy WaitKeyframe): fet
corruptPolicy_ == media::CorruptPolicy::WaitKeyframe ? "wait-keyframe" : "conceal", corruptPacketsSeen_));
}
if (corruptPolicy_ == media::CorruptPolicy::WaitKeyframe) {
- decoder_.recoverAtKeyframe();
- waitKeyframe_ = true;
- if (const auto& session = demuxer_.session()) session->addCorruptFrames(0, 0);
+ startKeyframeWait();
}
}
if (waitKeyframe_) {
- // A clean start after a damaged stretch is a keyframe that is not itself damaged.
- if (!(packet->flags & AV_PKT_FLAG_KEY) || (packet->flags & AV_PKT_FLAG_CORRUPT)) {
+ // A clean start after a damaged stretch is a keyframe that is not itself damaged. A stream
+ // that never flags one must not freeze the picture: after 5 s decoding just continues.
+ const bool clean = (packet->flags & AV_PKT_FLAG_KEY) && !(packet->flags & AV_PKT_FLAG_CORRUPT);
+ if (!clean && std::chrono::steady_clock::now() - waitKeyframeSince_ < std::chrono::seconds(5)) {
continue;
}
+ if (!clean) veyra::log::warn("source-file", "no clean keyframe within 5 s after damaged data; decoding continues without waiting");
waitKeyframe_ = false;
}
if (!decoder_.sendPacket(packet)) {
@@ -498,8 +499,7 @@ readAgain: // a damaged picture was dropped (corrupt policy WaitKeyframe): fet
frame->flags, frame->decode_error_flags, drop ? "wait-keyframe" : "conceal", corruptFramesSeen_));
}
if (drop) {
- decoder_.recoverAtKeyframe();
- waitKeyframe_ = true;
+ startKeyframeWait();
goto readAgain;
}
}
@@ -661,7 +661,7 @@ void MediaFileSource::beginDiscontinuity()
veyra::log::info("source-file", "network source reconnected: resetting the decoder at the new keyframe");
deinterlacer_.close();
filterDraining_ = false;
- decoder_.recoverAtKeyframe();
+ decoder_.flushBuffers(); // the session starts the new connection at a keyframe
waitKeyframe_ = false;
pendingDiscontinuity_ = true;
lastPtsUs_ = INT64_MIN;
@@ -686,12 +686,20 @@ bool MediaFileSource::tolerateDecoderError()
if (decoder_.lastError() != AVERROR_INVALIDDATA && decoderErrorsSeen_ > 3 && decoder_.hardwareActive()) {
return false; // not damaged data: let the hardware fallback decide
}
- if (const auto& session = demuxer_.session()) session->addCorruptFrames(0, 0);
- decoder_.recoverAtKeyframe();
- waitKeyframe_ = true;
+ startKeyframeWait();
return true;
}
+// Damaged data reached the decoder: drop what it holds and feed it again from the next clean keyframe.
+void MediaFileSource::startKeyframeWait()
+{
+ deinterlacer_.close();
+ filterDraining_ = false;
+ decoder_.flushBuffers();
+ waitKeyframe_ = true;
+ waitKeyframeSince_ = std::chrono::steady_clock::now();
+}
+
bool MediaFileSource::seek(const pipeline::Rational& targetSeconds)
{
if (!info_.opened) { return false; }