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++

fix(capture): preserve audio fallback state and guard failed vendor shutdown

Veyra Goal Agent9ef2aeacf5f3
diff --git a/src/source/AverMediaVendorAudio.cpp b/src/source/AverMediaVendorAudio.cpp
index 518cbb94..45dbb64e 100644
--- a/src/source/AverMediaVendorAudio.cpp
+++ b/src/source/AverMediaVendorAudio.cpp
@@ -65,6 +65,7 @@ struct AverMediaVendorAudio::Impl {
std::array<ForeignFn,EntryCount> api{};
alignas(16) std::array<unsigned char,64> storage{};
bool constructed=false;
+ bool quarantine=false;
HMODULE module=nullptr;
std::vector<DLL_DIRECTORY_COOKIE> directories;
std::mutex decodeMutex;
@@ -139,7 +140,18 @@ struct AverMediaVendorAudio::Impl {
void teardown(){
// Stop joins the vendor callback; never hold decodeMutex across it.
{std::lock_guard lock(decodeMutex);tearingDown=true;carrier.reset();}
- if(constructed){call(Stop);call(SetCallback,nullptr);call(Dtor,nullptr,nullptr,false);constructed=false;}
+ if(constructed){
+ // A failed external Stop cannot prove its callback thread joined.
+ // Retain this one opaque graph/module until process exit instead
+ // of unloading code underneath that thread or reusing its storage.
+ if(!call(Stop)){
+ quarantine=true;recordError("vendor Stop failed; graph quarantined until process exit");
+ }else{
+ call(SetCallback,nullptr);
+ if(!call(Dtor,nullptr,nullptr,false))quarantine=true;
+ }
+ constructed=false;
+ }
{std::lock_guard lock(mutex);state.started=state.running=false;}
}
bool onCarrier(avermedia::SampleInfo info,const uint8_t* data,size_t bytes){
@@ -168,14 +180,14 @@ struct AverMediaVendorAudio::Impl {
if(stalled||restartRequested.exchange(false)){
if(attempts>=maxRestarts){recordError("vendor audio recovery limit reached; falling back to DirectShow");ok=false;break;}
++attempts;{std::lock_guard lock(mutex);++state.losses;state.starting=true;}
- teardown();if(stopRequested)break;if(!setup()){ok=false;break;}
+ teardown();if(quarantine){ok=false;break;}if(stopRequested)break;if(!setup()){ok=false;break;}
{std::lock_guard lock(mutex);++state.restarts;}
}
}
}
}catch(...){recordError("vendor audio worker exception");ok=false;}
try{teardown();}catch(...){ok=false;}
- if(module){FreeLibrary(module);module=nullptr;}
+ if(module&&!quarantine){FreeLibrary(module);module=nullptr;}
for(auto cookie:directories)RemoveDllDirectory(cookie);directories.clear();api.fill(nullptr);
{std::lock_guard lock(mutex);state.starting=state.started=state.running=false;state.failed=!ok&&!stopRequested;
if(state.failed&&state.error.empty())state.error=L"vendor audio graph unavailable";}
@@ -186,9 +198,10 @@ struct AverMediaVendorAudio::Impl {
AverMediaVendorAudio::AverMediaVendorAudio(unsigned stallMs,unsigned maxRestarts):p_(std::make_unique<Impl>()){
p_->stallMs=std::max(50u,stallMs);p_->maxRestarts=maxRestarts;
}
-AverMediaVendorAudio::~AverMediaVendorAudio(){stop();}
+AverMediaVendorAudio::~AverMediaVendorAudio(){stop();if(p_->quarantine)p_.release();}
bool AverMediaVendorAudio::start(PcmCallback callback,const std::wstring& name,const std::wstring& path){
if(p_->worker.joinable()){const auto s=state();return !s.failed&&(s.starting||s.running);}
+ if(p_->quarantine)return false;
if(!avermedia::eligible(name,path,0)){p_->recordError("selected input is not an AVerMedia capture device");return false;}
if(AverMediaAudioSwitch::locateComponentRoot().empty()){p_->recordError("AVerMedia Multichannel Audio component not installed");return false;}
p_->onPcm=std::move(callback);p_->preferName=name;p_->preferPath=path;p_->stopRequested=false;p_->restartRequested=false;
diff --git a/src/source/CaptureCardSource.cpp b/src/source/CaptureCardSource.cpp
index 809f4134..3581879e 100644
--- a/src/source/CaptureCardSource.cpp
+++ b/src/source/CaptureCardSource.cpp
@@ -1419,6 +1419,12 @@ bool CaptureCardSource::applyVendorAudioSwitch(const std::wstring& audioName,con
// the USB functions are still there.
if(!avermedia::eligible(name,path,p.audioIngressMode))return false;
auto functions=AverMediaAudioSwitch::findUsbFunctions(L"vid_07ca");
+ const auto audioFunction=[](const AverMediaUsbFunction& f){
+ return avermedia::lower(f.instanceId).find(L"mi_02")!=std::wstring::npos
+ ||avermedia::lower(f.friendlyName).find(L"audio")!=std::wstring::npos;
+ };
+ if(std::any_of(functions.begin(),functions.end(),audioFunction))
+ std::erase_if(functions,[&](const auto& f){return !audioFunction(f);});
// A class-manager audio moniker has no USB serial. Require one unique
// matching audio function, rather than arming every installed card.
std::vector<avermedia::DeviceInfo> identities;
@@ -1818,6 +1824,7 @@ void CaptureCardSource::recoverAudio(float gain,unsigned syncMode,int offsetMs){
if(p.vendorDelivering)p.vendorDelivering->store(false);
p.vendorAudio->stop();p.vendorAudio.reset();p.vendorDelivering.reset();
vendorFailed=true;
+ p.audioError=L"AVerMedia audio graph failed; trying the original DirectShow audio input";
log::warn("capture-audio-vendor","vendor recovery exhausted; reconnecting the original DirectShow audio input");
}
CaptureSelection selection;if(!parseCapturePath(reconnectDesc_.path,selection)||!selection.stable||selection.audio==kCaptureAudioDisabled||selection.audio==kCaptureAudioWasapi)return;
@@ -1978,6 +1985,7 @@ void CaptureCardSource::close()noexcept{
if(p.audioSession)p.audioSession->stop();p.audioError.clear();
if(p.audioPassthrough)p.audioPassthrough->close();
p.events.Reset();p.control.Reset();p.grab.Reset();p.nullFilter.Reset();p.grabFilter.Reset();p.audioSink.Reset();p.audioFilter.Reset();p.config.Reset();p.device.Reset();p.builder.Reset();p.graph.Reset();p.referenceClock.Reset();p.ageClock.Reset();p.runReference=0;p.audioSession.reset();p.audioPassthrough.reset();
+ p.audioSessionDeferred=false;p.audioBitstream.reset();p.audioBitstreamKind.clear();
if(p.decodeThread.joinable()){{std::lock_guard lock(p.mutex);p.decodeStop=true;p.compressedQueue.clear();p.payloadPool.clear();}p.decodeWake.notify_all();p.decodeThread.join();}
for(auto& thread:p.extraDecodeThreads)if(thread.joinable())thread.join();
p.extraDecodeThreads.clear();p.extraDecoders.clear();p.mjpegParallel=false;
diff --git a/tests/fixtures/FakeAverMediaVendor.cpp b/tests/fixtures/FakeAverMediaVendor.cpp
index f8f1b972..a68a4e8c 100644
--- a/tests/fixtures/FakeAverMediaVendor.cpp
+++ b/tests/fixtures/FakeAverMediaVendor.cpp
@@ -21,7 +21,7 @@ extern "C" {
__declspec(dllexport) void FakeMode(const wchar_t* value){mode=value;starts=stops=callbacks=live=0;sta=true;}
__declspec(dllexport) unsigned FakeCount(unsigned which){return which==0?starts.load():which==1?stops.load():which==2?callbacks.load():which==3?live.load():sta?1:0;}
void* FakeCtor(void* self){apartment();graph(self)=new Graph;++live;return self;}
-void FakeStop(void* self){++stops;auto* g=graph(self);if(!g)return;g->stop=true;if(g->thread.joinable())g->thread.join();}
+bool FakeStop(void* self){++stops;auto* g=graph(self);if(!g)return true;if(mode==L"stop-fail")return false;g->stop=true;if(g->thread.joinable())g->thread.join();return true;}
void FakeDtor(void* self){FakeStop(self);delete graph(self);graph(self)=nullptr;--live;}
bool FakeReset(void*){apartment();if(mode==L"seh")RaiseException(0xC0000005,0,0,nullptr);if(mode==L"exception")throw std::runtime_error("test vendor exception");return mode!=L"reset-fail";}
void FakeDevices(std::vector<DeviceInfo>& out){
diff --git a/tests/unit/AverMediaVendorAudioTests.cpp b/tests/unit/AverMediaVendorAudioTests.cpp
index 045eb9d4..d69cfd04 100644
--- a/tests/unit/AverMediaVendorAudioTests.cpp
+++ b/tests/unit/AverMediaVendorAudioTests.cpp
@@ -64,6 +64,14 @@ int wmain(int argc,wchar_t** argv){try{
mode(L"normal");AverMediaVendorAudio audio;
for(unsigned i=0;i<10;++i){require(audio.start(callback,name,path),"repeat startup");audio.stop();require(count(3)==0,"repeat teardown");}
}
+ {
+ mode(L"stop-fail");AverMediaVendorAudio audio;
+ const auto previous=received.load();require(audio.start(callback,name,path),"start faulty Stop fixture");
+ require(wait([&]{return received>previous;}),"fixture callback is active before Stop");audio.stop();
+ const auto stopped=received.load();std::this_thread::sleep_for(std::chrono::milliseconds(50));
+ require(received==stopped,"failed vendor Stop cannot call released PCM session");
+ require(count(3)==1&&!audio.start(callback,name,path),"one failed external graph is quarantined without reuse/unload");
+ }
FreeLibrary(module);SetEnvironmentVariableW(L"VEYRA_AVERMEDIA_COMPONENT",nullptr);
std::cout<<"PASS "<<checks<<" checks: public ABI, STA, delayed startup, device identity, callback shutdown, SEH/C++ faults, bounded watchdog\n";return 0;
}catch(const std::exception& e){std::cerr<<"FAIL "<<e.what()<<" after "<<checks<<" checks\n";return 1;}}