diff --git a/CMakeLists.txt b/CMakeLists.txt index e1819fa29..b65591033 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1402,6 +1402,14 @@ if(BUILD_TESTING) add_executable(ARMixIndependentFormatTests tests/independent_stream_format_fence_test.cpp) target_compile_features(ARMixIndependentFormatTests PRIVATE cxx_std_20) add_test(NAME IndependentStreamNegotiatedFormatFence COMMAND ARMixIndependentFormatTests) + # IO-0: pure simultaneous-driver/physical-mask fail-closed policies. + # Existing AdaptiveClockBridge and ClockBridgeSizing are reused, not forked. + add_executable(ARMixIo0DualAsioPolicyTests + tests/dual_asio_capture_probe_policy_test.cpp + engine/device/DualAsioCaptureProbePolicy.h) + target_compile_features(ARMixIo0DualAsioPolicyTests PRIVATE cxx_std_20) + add_test(NAME IO0DualVendorCaptureDriverIdentityAndPhysicalMasks + COMMAND ARMixIo0DualAsioPolicyTests) if(WIN32) juce_add_console_app(ARMixAsioBackendTests PRODUCT_NAME "ArSonMix ASIO Backend Registration Tests") target_sources(ARMixAsioBackendTests PRIVATE tests/asio_backend_registration_test.cpp engine/device/AsioPriorityDeviceManager.h) @@ -1414,6 +1422,22 @@ if(BUILD_TESTING) target_link_libraries(ARMixDualJuceTests PRIVATE juce::juce_audio_devices juce::juce_audio_basics juce::juce_recommended_config_flags) target_compile_features(ARMixDualJuceTests PRIVATE cxx_std_20) add_test(NAME IndependentJuceDeviceLifecycle COMMAND ARMixDualJuceTests) + # Actual pinned JUCE ASIO instance #A + separate zero-output capture B. + # CTest calls --self-test only: unattended CI NEVER opens hardware. + # Physical --probe is opt-in and does NOT run a second mixer renderer. + juce_add_console_app(ARMixDualAsioCaptureProbe + PRODUCT_NAME "ArSonMix Dual ASIO Capture Compatibility Probe") + target_sources(ARMixDualAsioCaptureProbe PRIVATE + tools/dual_asio_capture_probe.cpp + engine/device/DualAsioCaptureProbePolicy.h) + target_compile_definitions(ARMixDualAsioCaptureProbe PRIVATE JUCE_ASIO=1) + target_link_libraries(ARMixDualAsioCaptureProbe PRIVATE + juce::juce_audio_devices juce::juce_audio_basics + juce::juce_recommended_config_flags advapi32) + target_compile_features(ARMixDualAsioCaptureProbe PRIVATE cxx_std_20) + add_test(NAME IO0DualAsioNativeProbeNeverOpensDriversInCI + COMMAND ARMixDualAsioCaptureProbe --self-test) + juce_add_console_app(ARMixWasapiProbeTests PRODUCT_NAME "ArSonMix WASAPI Probe Tests") target_sources(ARMixWasapiProbeTests PRIVATE tests/wasapi_capability_probe_test.cpp engine/device/WasapiCapabilityProbe.h) target_link_libraries(ARMixWasapiProbeTests PRIVATE juce::juce_core ole32 uuid juce::juce_recommended_config_flags) diff --git a/engine/device/DualAsioCaptureProbePolicy.h b/engine/device/DualAsioCaptureProbePolicy.h new file mode 100644 index 000000000..e3e7c4d76 --- /dev/null +++ b/engine/device/DualAsioCaptureProbePolicy.h @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include "AsioRegistrationPolicy.h" +#include +#include +#include + +namespace armix::device +{ +// IO-0, CONTROL-only: conservative compatibility probe policy, NOT a +// production source binding or a claim that a registered class can open. +// Distinct ASIO driver classes are necessary but NOT sufficient evidence of +// two independently usable physical interfaces. +class DualAsioCaptureProbePolicy final +{ +public: + using Identity = AsioRegistrationPolicy::Identity; + enum class Verdict : std::uint8_t + { + ok, missingSelection, unverifiedPrimaryDriver, unverifiedCaptureDriver, + sameDriverClass, wrongPrimaryEndpoint, wrongCaptureEndpoint, + invalidFormat, primaryOutputMissing, captureInputMissing, + captureOutputUnexpected, primaryMaskChanged, captureMaskChanged + }; + + struct Request + { + std::wstring_view primaryName, captureName; + Identity primary, capture; + std::uint64_t primaryInputMask = 0x3; + std::uint64_t primaryOutputMask = 0x3; + std::uint64_t captureInputMask = 0x1; + }; + struct Readback + { + std::wstring_view primaryName, captureName; + double primaryRate = 0., captureRate = 0.; + int primaryPeriod = 0, capturePeriod = 0; + std::uint64_t primaryInputMask = 0, primaryOutputMask = 0; + std::uint64_t captureInputMask = 0, captureOutputMask = 0; + }; + + static Verdict preflight(const Request& r) noexcept + { + if (r.primaryName.empty() || r.captureName.empty()) + return Verdict::missingSelection; + if (!r.primary.valid()) return Verdict::unverifiedPrimaryDriver; + if (!r.capture.valid()) return Verdict::unverifiedCaptureDriver; + if (r.primary.sameDriverClass(r.capture) || + r.primaryName == r.captureName) + return Verdict::sameDriverClass; + if (r.primaryOutputMask == 0 || r.captureInputMask == 0) + return Verdict::invalidFormat; + return Verdict::ok; + } + + static Verdict verify(const Request& r, const Readback& a) noexcept + { + const auto ready=preflight(r); + if (ready!=Verdict::ok) return ready; + if (a.primaryName!=r.primaryName) return Verdict::wrongPrimaryEndpoint; + if (a.captureName!=r.captureName) return Verdict::wrongCaptureEndpoint; + if (!std::isfinite(a.primaryRate)||!std::isfinite(a.captureRate)|| + a.primaryRate<8000.||a.captureRate<8000.|| + a.primaryRate>384000.||a.captureRate>384000.|| + a.primaryPeriod<=0||a.capturePeriod<=0) + return Verdict::invalidFormat; + if ((a.primaryOutputMask & 0x3u)!=0x3u) + return Verdict::primaryOutputMissing; + if (a.captureInputMask==0) return Verdict::captureInputMissing; + if (a.captureOutputMask!=0) return Verdict::captureOutputUnexpected; + if (a.primaryInputMask!=r.primaryInputMask|| + a.primaryOutputMask!=r.primaryOutputMask) + return Verdict::primaryMaskChanged; + if (a.captureInputMask!=r.captureInputMask) + return Verdict::captureMaskChanged; + return Verdict::ok; + } + + static const char* reason(Verdict v) noexcept + { + switch(v) + { + case Verdict::ok: return "IO0_EXACT_ASIO_READBACK"; + case Verdict::missingSelection: return "IO0_MISSING_EXPLICIT_DRIVER"; + case Verdict::unverifiedPrimaryDriver: return "IO0_PRIMARY_CLASS_NOT_ATTESTED"; + case Verdict::unverifiedCaptureDriver: return "IO0_CAPTURE_CLASS_NOT_ATTESTED"; + case Verdict::sameDriverClass: return "IO0_REQUIRES_DISTINCT_ASIO_CLASSES"; + case Verdict::wrongPrimaryEndpoint: return "IO0_PRIMARY_NAME_SUBSTITUTED"; + case Verdict::wrongCaptureEndpoint: return "IO0_CAPTURE_NAME_SUBSTITUTED"; + case Verdict::invalidFormat: return "IO0_NEGOTIATED_FORMAT_INVALID"; + case Verdict::primaryOutputMissing: return "IO0_PRIMARY_OUTPUT_STEREO_MISSING"; + case Verdict::captureInputMissing: return "IO0_CAPTURE_INPUT_MISSING"; + case Verdict::captureOutputUnexpected: return "IO0_SECONDARY_CAPTURE_MUST_HAVE_ZERO_OUTPUTS"; + case Verdict::primaryMaskChanged: return "IO0_PRIMARY_PIN_MASK_CHANGED"; + case Verdict::captureMaskChanged: return "IO0_CAPTURE_PIN_MASK_CHANGED"; + } + return "IO0_UNKNOWN_FAILURE"; + } +}; +} // namespace armix::device diff --git a/tests/dual_asio_capture_probe_policy_test.cpp b/tests/dual_asio_capture_probe_policy_test.cpp new file mode 100644 index 000000000..047b4aed8 --- /dev/null +++ b/tests/dual_asio_capture_probe_policy_test.cpp @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#include "../engine/device/DualAsioCaptureProbePolicy.h" +#include "../engine/device/ClockBridgeSizing.h" +#include +#include +#include + +using Policy=armix::device::DualAsioCaptureProbePolicy; +using V=Policy::Verdict; +static void check(bool ok) +{ + // Always active in Release/MSVC CTest; never use NDEBUG-stripped assert. + if (!ok) {std::fputs("IO0_POLICY_FAIL\\n",stderr); std::abort();} +} + +int main() +{ + const Policy::Identity a{L"ASIO Vendor A",L"{11111111-1111-1111-1111-111111111111}"}; + const Policy::Identity b{L"ASIO Vendor B",L"{22222222-2222-2222-2222-222222222222}"}; + Policy::Request wanted{L"Vendor A ASIO",L"Vendor B ASIO",a,b,3,3,1}; + Policy::Readback actual{L"Vendor A ASIO",L"Vendor B ASIO", + 48000.,44100.,256,128,3,3,1,0}; + check(Policy::preflight(wanted)==V::ok); + check(Policy::verify(wanted,actual)==V::ok); + check(Policy::preflight({L"Vendor A ASIO",L"Vendor A ASIO",a,a})== + V::sameDriverClass); + check(Policy::preflight({L"Vendor A ASIO",L"Vendor B ASIO",a,a})== + V::sameDriverClass); + check(Policy::preflight({L"",L"Vendor B ASIO",a,b})== + V::missingSelection); + check(Policy::preflight({L"Vendor A ASIO",L"Vendor B ASIO",{},b})== + V::unverifiedPrimaryDriver); + check(Policy::preflight({L"Vendor A ASIO",L"Vendor B ASIO",a,{}})== + V::unverifiedCaptureDriver); + auto changed=actual; + changed.captureName=L"USB Audio with same channel names"; + check(Policy::verify(wanted,changed)==V::wrongCaptureEndpoint); + changed=actual; + changed.primaryName=L"Unexpected output"; + check(Policy::verify(wanted,changed)==V::wrongPrimaryEndpoint); + changed=actual; + changed.primaryOutputMask=1; + check(Policy::verify(wanted,changed)==V::primaryOutputMissing); + changed=actual; + changed.primaryInputMask=1; + check(Policy::verify(wanted,changed)==V::primaryMaskChanged); + changed=actual; + changed.captureInputMask=0; + check(Policy::verify(wanted,changed)==V::captureInputMissing); + changed=actual; + changed.captureOutputMask=3; + check(Policy::verify(wanted,changed)==V::captureOutputUnexpected); + changed=actual; + changed.captureInputMask=2; + check(Policy::verify(wanted,changed)==V::captureMaskChanged); + changed=actual; + changed.captureRate=0; + check(Policy::verify(wanted,changed)==V::invalidFormat); + changed=actual; + changed.primaryPeriod=0; + check(Policy::verify(wanted,changed)==V::invalidFormat); + + // Reuse production ASRC sizing. Different clocks always need a bounded + // prepared source bridge in IO-1; IO-0 does not insert one into primary. + for (const double source : {44100.,48000.,96000.}) + for (const double primary : {44100.,48000.,96000.}) + { + const auto sizing=armix::device::ClockBridgeSizing::choose( + source,primary,128,256,48,32768,24.); + check(sizing.valid); + check(sizing.targetMs>=24.); + check(sizing.minFrames>0); + } + const auto impossible=armix::device::ClockBridgeSizing::choose( + 48000.,48000.,32768,32768,48,32768,24.); + check(!impossible.valid); + std::puts("IO0_POLICY_PASS: identity, zero-output capture, exact masks, 9 clock profiles"); + return 0; +} diff --git a/tools/dual_asio_capture_probe.cpp b/tools/dual_asio_capture_probe.cpp new file mode 100644 index 000000000..9dded0d58 --- /dev/null +++ b/tools/dual_asio_capture_probe.cpp @@ -0,0 +1,247 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// IO-0 physical dual-vendor ASIO compatibility probe, NOT the ArSonMix mixer. +// This executable never renders DSP/VST3, never changes the product session, +// and never routes secondary capture to a speaker. Its explicit primary +// callback writes only zeroes. Run only with owner-approved disconnected or +// muted speakers and a deliberate test input signal on ASIO B Input 1. +// +// Normal CTest --self-test performs NO hardware enumeration or driver opens. +// Explicit manual: ARMixDualAsioCaptureProbe --probe "Vendor A ASIO" "Vendor B ASIO" 48000 256 5000 +#include "../engine/device/DualAsioCaptureProbePolicy.h" +#include "../engine/device/AsioRegistryIdentityProbe.h" +#include "../engine/device/AsioPriorityDeviceManager.h" +#include +#include +#include +#include +#include +#include +#include + +namespace +{ +using Policy=armix::device::DualAsioCaptureProbePolicy; +struct CaptureCounters final : juce::AudioIODeviceCallback +{ + bool primary=false; + int expectedInputs=0,expectedOutputs=0; + std::atomic calls{0},frames{0},nonzero{0},nonfinite{0}; + std::atomic violated{false}; + explicit CaptureCounters(bool isPrimary) : primary(isPrimary) {} + void audioDeviceAboutToStart(juce::AudioIODevice*) override {} + void audioDeviceStopped() override {} + void audioDeviceError(const juce::String&) override + { + violated.store(true,std::memory_order_release); + } + void audioDeviceIOCallbackWithContext( + const float* const* input,int inputs,float* const* output,int outputs, + int samples,const juce::AudioIODeviceCallbackContext&) override + { + // Never drive hardware, even on the explicitly opened primary. + for(int c=0;c(samples),std::memory_order_relaxed); + if(primary) return; // zero-output capture producer, NO graph or DSP + std::uint64_t live=0,bad=0; + for(int c=0;c0.0001f) ++live; + } + } + if(live)nonzero.fetch_add(live,std::memory_order_relaxed); + if(bad)nonfinite.fetch_add(bad,std::memory_order_relaxed); + } +}; + +struct Endpoint final +{ + armix::device::AsioPriorityDeviceManager discoveryOnly; // never initialise / open defaults + juce::OwnedArray factories; + std::unique_ptr device; + CaptureCounters counters; + explicit Endpoint(bool primary) : counters(primary) {} + ~Endpoint() + { + if(device){device->stop();device->close();device.reset();} + // Factories outlive their created devices. + } + juce::String openExact(const juce::String& selected, bool primary, + double rate,int frames) + { + discoveryOnly.createAudioDeviceTypes(factories); + juce::AudioIODeviceType* asio=nullptr; + for(auto* t:factories) if(t->getTypeName()=="ASIO"){asio=t;break;} + if(!asio)return "Pinned JUCE build has no ASIO device type"; + asio->scanForDevices(); // CONTROL, before opening; never on callback + if(!asio->getDeviceNames(true).contains(selected) || + (primary&&!asio->getDeviceNames(false).contains(selected))) + return "Selected exact ASIO driver is absent from input/output inventory"; + // For capture B, the OUTPUT name is deliberately EMPTY, never an + // implicit default speaker. Some ASIO vendors reject this outright: + // return REJECT, do NOT invent a dummy output or reopen another driver. + // JUCE createDevice signature is (OUTPUT name, INPUT name). + // B MUST have no output device even during construction. + device.reset(asio->createDevice(primary?selected:juce::String(),selected)); + if(!device)return "JUCE ASIO refused capture-only device construction"; + juce::BigInteger in,out; + in.setRange(0,primary?2:1,true); + if(primary)out.setRange(0,2,true); + const auto error=device->open(in,out,rate,frames); + if(error.isNotEmpty())return error; + if(!device->isOpen()||device->getName()!=selected) + return "ASIO device opened a different physical driver name"; + if(device->getActiveInputChannels()!=in|| + device->getActiveOutputChannels()!=out) + return "ASIO driver renegotiated physical input or output pin mask"; + if(device->getCurrentSampleRate()<8000. || + device->getCurrentBufferSizeSamples()<=0) + return "ASIO negotiated an invalid rate or period"; + counters.expectedInputs=primary?2:1; + counters.expectedOutputs=primary?2:0; + return {}; + } + void start() { if(device)device->start(&counters); } + bool playing() const {return device&&device->isOpen()&&device->isPlaying();} +}; + +std::uint64_t maskOf(const juce::BigInteger& mask) noexcept +{ + // IO-0 admits only the first 32 ASIO pins; no unsafe truncation of a + // higher sparse physical mask. The fixture checks exact masks below. + if(mask.getHighestBit()>=32)return UINT64_MAX; + return static_cast(mask.getBitRangeAsInt(0,32)); +} + +int probe(const char* primaryLabel,const char* captureLabel,double rate,int frames, + int durationMs) +{ +#if !defined(_WIN32) || !JUCE_ASIO + (void)primaryLabel;(void)captureLabel;(void)rate;(void)frames;(void)durationMs; + std::fputs("IO0_FAIL: pinned Windows JUCE ASIO backend unavailable\n",stderr); + return 3; +#else + // All registry, device enumeration and JUCE open/close calls are CONTROL. + if(!std::isfinite(rate)||rate<8000.||rate>192000.||frames<32|| + frames>8192||durationMs<1000||durationMs>30000) + return 2; + const juce::String primaryName=juce::String::fromUTF8(primaryLabel); + const juce::String captureName=juce::String::fromUTF8(captureLabel); + const auto primaryReg=armix::device::AsioRegistryIdentityProbe::resolve( + std::wstring(primaryName.toWideCharPointer())).registration; + const auto captureReg=armix::device::AsioRegistryIdentityProbe::resolve( + std::wstring(captureName.toWideCharPointer())).registration; + const std::wstring primaryWide(primaryName.toWideCharPointer()); + const std::wstring captureWide(captureName.toWideCharPointer()); + Policy::Request request{primaryWide,captureWide, + primaryReg.identity,captureReg.identity,3,3,1}; + const auto initial=Policy::preflight(request); + if(!primaryReg.ok()||!captureReg.ok()||initial!=Policy::Verdict::ok) + { + std::fprintf(stderr,"IO0_REJECT preflight=%s primary=%s capture=%s\n", + Policy::reason(initial), + armix::device::AsioRegistrationPolicy::reason(primaryReg.error), + armix::device::AsioRegistrationPolicy::reason(captureReg.error)); + return 3; + } + Endpoint primary(true),capture(false); + auto err=primary.openExact(primaryName,true,rate,frames); + if(err.isNotEmpty()){std::fprintf(stderr,"IO0_PRIMARY_REJECT %s\n",err.toRawUTF8());return 3;} + // Open primary FIRST, then B while A is still actually open. The primary + // callback is silence-only, not a second mixer render implementation. + primary.start(); + if(!primary.playing()) + {std::fputs("IO0_PRIMARY_START_REJECT\n",stderr);return 3;} + err=capture.openExact(captureName,false,rate,frames); + if(err.isNotEmpty()){std::fprintf(stderr,"IO0_CAPTURE_REJECT %s\n",err.toRawUTF8());return 3;} + const auto primaryRegAfter=armix::device::AsioRegistryIdentityProbe::resolve( + std::wstring(primaryName.toWideCharPointer())).registration; + const auto captureRegAfter=armix::device::AsioRegistryIdentityProbe::resolve( + std::wstring(captureName.toWideCharPointer())).registration; + if(!primaryRegAfter.ok()||!captureRegAfter.ok()|| + !primaryReg.identity.sameDriverClass(primaryRegAfter.identity)|| + !captureReg.identity.sameDriverClass(captureRegAfter.identity)) + {std::fputs("IO0_REJECT ASIO registry class changed while opening\n",stderr);return 3;} + capture.start(); + if(!capture.playing()) + {std::fputs("IO0_CAPTURE_START_REJECT\n",stderr);return 3;} + juce::Thread::sleep(durationMs); // CONTROL only, never the hardware callback + auto* a=primary.device.get(); + auto* b=capture.device.get(); + const std::wstring openedPrimary(a->getName().toWideCharPointer()); + const std::wstring openedCapture(b->getName().toWideCharPointer()); + const Policy::Readback readback{ + openedPrimary,openedCapture, + a->getCurrentSampleRate(),b->getCurrentSampleRate(), + a->getCurrentBufferSizeSamples(),b->getCurrentBufferSizeSamples(), + maskOf(a->getActiveInputChannels()),maskOf(a->getActiveOutputChannels()), + maskOf(b->getActiveInputChannels()),maskOf(b->getActiveOutputChannels())}; + const auto verified=Policy::verify(request,readback); + const auto aCalls=primary.counters.calls.load(); + const auto bCalls=capture.counters.calls.load(); + const auto bSignal=capture.counters.nonzero.load(); + const bool ok=verified==Policy::Verdict::ok&& + primary.playing()&&capture.playing()&&aCalls>0&&bCalls>0&& + bSignal>0&&capture.counters.nonfinite.load()==0&& + !primary.counters.violated.load()&&!capture.counters.violated.load(); + std::printf("IO0_RESULT=%s READBACK=%s PRIMARY_RATE=%.1f CAPTURE_RATE=%.1f " + "PRIMARY_PERIOD=%d CAPTURE_PERIOD=%d PRIMARY_CALLBACKS=%llu " + "CAPTURE_CALLBACKS=%llu CAPTURE_NONZERO_SAMPLES=%llu " + "CAPTURE_OUTPUT_MASK=%llu DSP_PROCESSED=0\n", + ok?"PHYSICAL_PROBE_PASS":"PHYSICAL_PROBE_FAIL",Policy::reason(verified), + readback.primaryRate,readback.captureRate, + readback.primaryPeriod,readback.capturePeriod, + static_cast(aCalls), + static_cast(bCalls), + static_cast(bSignal), + static_cast(readback.captureOutputMask)); + return ok?0:4; +#endif +} +} // namespace + +int main(int argc,char** argv) +{ + // Self-test is deliberately hardware-free; CTest NEVER attempts an + // unknown ASIO device or opens real speakers on an unattended runner. + if(argc==2&&std::string(argv[1])=="--self-test") + { + const Policy::Identity a{L"A",L"{11111111-1111-1111-1111-111111111111}"}; + const Policy::Identity b{L"B",L"{22222222-2222-2222-2222-222222222222}"}; + const Policy::Request r{L"A ASIO",L"B ASIO",a,b,3,3,1}; + if(Policy::preflight(r)!=Policy::Verdict::ok|| + Policy::preflight({L"A ASIO",L"B ASIO",a,a})!= + Policy::Verdict::sameDriverClass) + return 5; + std::puts("IO0_SELF_TEST_PASS: no physical device opened"); + return 0; + } + if(argc!=7||std::string(argv[1])!="--probe") + { + std::fputs("Usage: ARMixDualAsioCaptureProbe --self-test OR\n" + " --probe \"Primary ASIO name\" \"Secondary ASIO name\" " + "48000 256 5000\n" + "WARNING: owner-authorized physical rig only; outputs sent zeros.\n",stderr); + return 2; + } + const double rate=std::strtod(argv[4],nullptr); + const int frames=std::atoi(argv[5]); + const int ms=std::atoi(argv[6]); + // ASIO uses JUCE message-window/COM lifecycle for reset notifications. + // The attended physical probe initialises it on CONTROL only; --self-test + // deliberately never creates a MessageManager or opens a sound device. + juce::ScopedJuceInitialiser_GUI initialiseJuce; + return probe(argv[2],argv[3],rate,frames,ms); +}