fix: add COM init on WASAPI capture thread for Windows 11 compatibility

- Call CoInitializeEx(MTA) at start of captureThread() and CoUninitialize() on exit
- Windows 11 enforces per-thread COM initialization more strictly than Win10,
  causing GetBuffer/GetNextPacketSize to fail silently without this
- Add HRESULT error logging for WASAPI capture failures (GetNextPacketSize, GetBuffer)
This commit is contained in:
webadderall
2026-03-30 15:27:40 +11:00
parent 6f8891529a
commit ae392e573e
@@ -215,6 +215,12 @@ bool WasapiCapture::writeWavHeader(HANDLE file, DWORD dataSize) {
}
void WasapiCapture::captureThread() {
// COM must be initialized on every thread that uses COM objects.
// The main thread calls winrt::init_apartment(MTA) but that only covers
// the main thread. Windows 11 enforces per-thread COM init more strictly
// than Windows 10, causing silent audio capture failures without this.
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
WORD channels = static_cast<WORD>(mixFormat_->nChannels);
bool isFloat = (mixFormat_->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) ||
(mixFormat_->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
@@ -235,7 +241,10 @@ void WasapiCapture::captureThread() {
UINT32 packetLength = 0;
HRESULT hr = captureClient_->GetNextPacketSize(&packetLength);
if (FAILED(hr)) break;
if (FAILED(hr)) {
std::cerr << "WASAPI: GetNextPacketSize failed hr=0x" << std::hex << hr << std::dec << std::endl;
break;
}
while (packetLength > 0) {
BYTE* data = nullptr;
@@ -243,7 +252,10 @@ void WasapiCapture::captureThread() {
DWORD flags = 0;
hr = captureClient_->GetBuffer(&data, &numFrames, &flags, nullptr, nullptr);
if (FAILED(hr)) break;
if (FAILED(hr)) {
std::cerr << "WASAPI: GetBuffer failed hr=0x" << std::hex << hr << std::dec << std::endl;
break;
}
UINT32 totalSamples = numFrames * channels;
@@ -268,7 +280,12 @@ void WasapiCapture::captureThread() {
totalDataBytes_ += written;
hr = captureClient_->GetNextPacketSize(&packetLength);
if (FAILED(hr)) break;
if (FAILED(hr)) {
std::cerr << "WASAPI: GetNextPacketSize failed hr=0x" << std::hex << hr << std::dec << std::endl;
break;
}
}
}
CoUninitialize();
}