Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

diag(windows): add opt-in audio device-state watcher for #724

abduznikc41443f6ec4a
diff --git a/electron/native/wgc-capture/CMakeLists.txt b/electron/native/wgc-capture/CMakeLists.txt
index 99041a02b..f9bea5354 100644
--- a/electron/native/wgc-capture/CMakeLists.txt
+++ b/electron/native/wgc-capture/CMakeLists.txt
@@ -46,6 +46,8 @@ add_executable(wgc-capture
src/mf_encoder.h
src/monitor_utils.cpp
src/monitor_utils.h
+ src/wasapi_device_watcher.cpp
+ src/wasapi_device_watcher.h
src/wasapi_loopback_capture.cpp
src/wasapi_loopback_capture.h
src/webcam_capture.cpp
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index 223211c9f..b3c353a5e 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -2,6 +2,7 @@
#include "dpi_awareness.h"
#include "mf_encoder.h"
#include "monitor_utils.h"
+#include "wasapi_device_watcher.h"
#include "wasapi_loopback_capture.h"
#include "webcam_capture.h"
#include "wgc_session.h"
@@ -796,6 +797,19 @@ int wmain(int argc, wchar_t* argv[]) {
WasapiLoopbackCapture loopbackCapture;
WasapiLoopbackCapture microphoneCapture;
+ // getopenscreen/openscreen#724, diagnostic only: logs render/capture endpoint
+ // state transitions for the duration of the recording, so a report of a headset
+ // dropping mid-take can be correlated against a real timestamped Windows event
+ // instead of guessed at. Does not change recording behavior in any way.
+ const bool deviceWatchEnabled = readEnvInt("OPENSCREEN_WGC_LOG_AUDIO_DEVICE_EVENTS", 0) == 1;
+ WasapiDeviceWatcher deviceWatcher;
+ bool deviceWatchActive = false;
+ const auto stopDeviceWatchIfActive = [&]() {
+ if (deviceWatchActive) {
+ deviceWatcher.stop();
+ deviceWatchActive = false;
+ }
+ };
const AudioInputFormat* audioFormat = nullptr;
AudioInputFormat encoderAudioFormat{};
AudioInputFormat systemAudioFormat{};
@@ -1378,13 +1392,23 @@ int wmain(int argc, wchar_t* argv[]) {
return true;
};
+ if (deviceWatchEnabled) {
+ deviceWatchActive = deviceWatcher.start();
+ if (!deviceWatchActive) {
+ std::cerr << "WARNING: Failed to start audio device watcher; continuing without it"
+ << std::endl;
+ }
+ }
+
if (!startAudioCaptures()) {
+ stopDeviceWatchIfActive();
return 1;
}
if (config.webcamEnabled) {
if (!webcamCapture.start()) {
microphoneCapture.stop();
loopbackCapture.stop();
+ stopDeviceWatchIfActive();
if (audioMixer) {
audioMixer->stop();
}
@@ -1416,6 +1440,7 @@ int wmain(int argc, wchar_t* argv[]) {
webcamCapture.stop();
microphoneCapture.stop();
loopbackCapture.stop();
+ stopDeviceWatchIfActive();
if (audioMixer) {
audioMixer->stop();
}
@@ -1459,6 +1484,7 @@ int wmain(int argc, wchar_t* argv[]) {
stopVideoWriter();
microphoneCapture.stop();
loopbackCapture.stop();
+ stopDeviceWatchIfActive();
webcamCapture.stop();
if (audioMixer) {
audioMixer->stop();
@@ -1594,6 +1620,11 @@ int wmain(int argc, wchar_t* argv[]) {
beginStopStep("loopback", stepBudgetMs);
loopbackCapture.stop();
logStopStep("loopback");
+ if (deviceWatchActive) {
+ beginStopStep("device-watcher", stepBudgetMs);
+ stopDeviceWatchIfActive();
+ logStopStep("device-watcher");
+ }
beginStopStep("webcam", stepBudgetMs);
webcamCapture.stop();
logStopStep("webcam");
diff --git a/electron/native/wgc-capture/src/wasapi_device_watcher.cpp b/electron/native/wgc-capture/src/wasapi_device_watcher.cpp
new file mode 100644
index 000000000..d5d78310c
--- /dev/null
+++ b/electron/native/wgc-capture/src/wasapi_device_watcher.cpp
@@ -0,0 +1,227 @@
+#include "wasapi_device_watcher.h"
+
+#include <Functiondiscoverykeys_devpkey.h>
+#include <propvarutil.h>
+
+#include <chrono>
+#include <cstdio>
+#include <iostream>
+
+namespace {
+
+std::string wideToUtf8(const std::wstring& value) {
+ if (value.empty()) {
+ return {};
+ }
+ const int size = WideCharToMultiByte(
+ CP_UTF8, 0, value.data(), static_cast<int>(value.size()), nullptr, 0, nullptr, nullptr);
+ std::string result(static_cast<size_t>(size), '\0');
+ WideCharToMultiByte(
+ CP_UTF8, 0, value.data(), static_cast<int>(value.size()), result.data(), size, nullptr, nullptr);
+ return result;
+}
+
+std::string jsonEscape(const std::string& value) {
+ std::string result;
+ result.reserve(value.size());
+ for (const char c : value) {
+ switch (c) {
+ case '\\':
+ result += "\\\\";
+ break;
+ case '"':
+ result += "\\\"";
+ break;
+ case '\n':
+ result += "\\n";
+ break;
+ default:
+ result += c;
+ }
+ }
+ return result;
+}
+
+std::string deviceStateLabel(DWORD state) {
+ switch (state) {
+ case DEVICE_STATE_ACTIVE:
+ return "active";
+ case DEVICE_STATE_DISABLED:
+ return "disabled";
+ case DEVICE_STATE_NOTPRESENT:
+ return "not-present";
+ case DEVICE_STATE_UNPLUGGED:
+ return "unplugged";
+ default:
+ return "unknown";
+ }
+}
+
+int64_t nowUnixMillis() {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::system_clock::now().time_since_epoch())
+ .count();
+}
+
+std::wstring friendlyNameForDevice(IMMDeviceEnumerator* enumerator, LPCWSTR deviceId) {
+ if (!enumerator || !deviceId) {
+ return {};
+ }
+ Microsoft::WRL::ComPtr<IMMDevice> device;
+ if (FAILED(enumerator->GetDevice(deviceId, &device)) || !device) {
+ return {};
+ }
+ Microsoft::WRL::ComPtr<IPropertyStore> properties;
+ if (FAILED(device->OpenPropertyStore(STGM_READ, &properties)) || !properties) {
+ return {};
+ }
+ PROPVARIANT value;
+ PropVariantInit(&value);
+ std::wstring name;
+ if (SUCCEEDED(properties->GetValue(PKEY_Device_FriendlyName, &value)) && value.vt == VT_LPWSTR &&
+ value.pwszVal) {
+ name = value.pwszVal;
+ }
+ PropVariantClear(&value);
+ return name;
+}
+
+} // namespace
+
+WasapiDeviceWatcher::~WasapiDeviceWatcher() {
+ stop();
+}
+
+bool WasapiDeviceWatcher::start() {
+ HRESULT hr = CoCreateInstance(
+ __uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, IID_PPV_ARGS(&deviceEnumerator_));
+ if (FAILED(hr) || !deviceEnumerator_) {
+ std::cerr << "WARNING: [device-watcher] CoCreateInstance(MMDeviceEnumerator) failed (hr=0x"
+ << std::hex << hr << std::dec << ")" << std::endl;
+ return false;
+ }
+
+ hr = deviceEnumerator_->RegisterEndpointNotificationCallback(this);
+ if (FAILED(hr)) {
+ std::cerr << "WARNING: [device-watcher] RegisterEndpointNotificationCallback failed (hr=0x"
+ << std::hex << hr << std::dec << ")" << std::endl;
+ deviceEnumerator_.Reset();
+ return false;
+ }
+ registered_ = true;
+
+ emitBaseline(eRender, L"render");
+ emitBaseline(eCapture, L"capture");
+ return true;
+}
+
+void WasapiDeviceWatcher::stop() {
+ if (registered_ && deviceEnumerator_) {
+ deviceEnumerator_->UnregisterEndpointNotificationCallback(this);
+ }
+ registered_ = false;
+ deviceEnumerator_.Reset();
+}
+
+void WasapiDeviceWatcher::emitBaseline(EDataFlow flow, const wchar_t* flowLabel) {
+ Microsoft::WRL::ComPtr<IMMDevice> device;
+ HRESULT hr = deviceEnumerator_->GetDefaultAudioEndpoint(flow, eConsole, &device);
+ if (FAILED(hr) || !device) {
+ return;
+ }
+
+ LPWSTR rawId = nullptr;
+ std::wstring id;
+ if (SUCCEEDED(device->GetId(&rawId)) && rawId) {
+ id = rawId;
+ CoTaskMemFree(rawId);
+ }
+
+ DWORD state = 0;
+ device->GetState(&state);
+ const std::wstring name = friendlyNameForDevice(deviceEnumerator_.Get(), id.c_str());
+
+ std::lock_guard<std::mutex> lock(outputMutex_);
+ std::cout << "{\"event\":\"audio-device-watch\",\"schemaVersion\":1,\"type\":\"baseline\","
+ "\"flow\":\""
+ << wideToUtf8(flowLabel) << "\",\"deviceId\":\"" << jsonEscape(wideToUtf8(id))
+ << "\",\"deviceName\":\"" << jsonEscape(wideToUtf8(name)) << "\",\"state\":\""
+ << deviceStateLabel(state) << "\",\"timestampMs\":" << nowUnixMillis() << "}"
+ << std::endl;
+}
+
+void WasapiDeviceWatcher::emitDeviceEvent(const wchar_t* eventName, LPCWSTR deviceId, const char* extraJson) {
+ const std::wstring name = friendlyNameForDevice(deviceEnumerator_.Get(), deviceId);
+
+ std::lock_guard<std::mutex> lock(outputMutex_);
+ std::cout << "{\"event\":\"audio-device-watch\",\"schemaVersion\":1,\"type\":\""
+ << wideToUtf8(eventName) << "\",\"deviceId\":\""
+ << jsonEscape(wideToUtf8(deviceId ? deviceId : L"")) << "\",\"deviceName\":\""
+ << jsonEscape(wideToUtf8(name)) << "\"";
+ if (extraJson) {
+ std::cout << "," << extraJson;
+ }
+ std::cout << ",\"timestampMs\":" << nowUnixMillis() << "}" << std::endl;
+}
+
+ULONG STDMETHODCALLTYPE WasapiDeviceWatcher::AddRef() {
+ return ++refCount_;
+}
+
+ULONG STDMETHODCALLTYPE WasapiDeviceWatcher::Release() {
+ // This object's lifetime is owned by main.cpp, not by COM: it lives on the
+ // stack for the duration of the recording and unregisters in stop() before
+ // destruction, so a ref count reaching zero here must not delete `this`.
+ const ULONG count = --refCount_;
+ return count;
+}
+
+HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::QueryInterface(REFIID riid, void** ppvObject) {
+ if (!ppvObject) {
+ return E_POINTER;
+ }
+ if (riid == __uuidof(IUnknown) || riid == __uuidof(IMMNotificationClient)) {
+ *ppvObject = static_cast<IMMNotificationClient*>(this);
+ AddRef();
+ return S_OK;
+ }
+ *ppvObject = nullptr;
+ return E_NOINTERFACE;
+}
+
+HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnDeviceStateChanged(LPCWSTR deviceId, DWORD newState) {
+ char extra[64];
+ snprintf(extra, sizeof(extra), "\"state\":\"%s\"", deviceStateLabel(newState).c_str());
+ emitDeviceEvent(L"state-changed", deviceId, extra);
+ return S_OK;
+}
+
+HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnDeviceAdded(LPCWSTR deviceId) {
+ emitDeviceEvent(L"added", deviceId);
+ return S_OK;
+}
+
+HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnDeviceRemoved(LPCWSTR deviceId) {
+ emitDeviceEvent(L"removed", deviceId);
+ return S_OK;
+}
+
+HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnDefaultDeviceChanged(
+ EDataFlow flow, ERole role, LPCWSTR defaultDeviceId) {
+ // Only eConsole is what this app's capture paths use (GetDefaultAudioEndpoint
+ // calls elsewhere all pass eConsole); the other roles fire independently and
+ // would just be noise here.
+ if (role != eConsole) {
+ return S_OK;
+ }
+ char extra[16];
+ snprintf(extra, sizeof(extra), "\"flow\":\"%s\"", flow == eRender ? "render" : "capture");
+ emitDeviceEvent(L"default-changed", defaultDeviceId, extra);
+ return S_OK;
+}
+
+HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnPropertyValueChanged(LPCWSTR, const PROPERTYKEY) {
+ // Not interesting for this diagnosis: fires on things like a renamed endpoint,
+ // not on power/connection state.
+ return S_OK;
+}
diff --git a/electron/native/wgc-capture/src/wasapi_device_watcher.h b/electron/native/wgc-capture/src/wasapi_device_watcher.h
new file mode 100644
index 000000000..b17752bed
--- /dev/null
+++ b/electron/native/wgc-capture/src/wasapi_device_watcher.h
@@ -0,0 +1,75 @@
+#pragma once
+
+// DIAGNOSTIC ONLY, opt-in via OPENSCREEN_WGC_LOG_AUDIO_DEVICE_EVENTS=1 (see main.cpp).
+//
+// getopenscreen/openscreen#724: before committing to a fix, we need a real,
+// timestamped signal on WHY a headset's render/capture endpoints change state
+// mid-recording -- USB selective suspend, WASAPI endpoint idle, and the headset's
+// own firmware auto-off all look the same to the user ("headphones turned off"),
+// but only some of them are fixable from inside this process. This watcher answers
+// that by registering an IMMNotificationClient for the duration of the recording
+// and emitting a structured JSON event on every state transition of the default
+// render and capture endpoints, so it can be correlated against the moment a user
+// hears their headset drop.
+//
+// This does not attempt to fix anything -- it only observes and reports. See the
+// issue for the three-way split this is meant to distinguish:
+// 1. USB selective suspend (device level): the endpoint would go NOTPRESENT/
+// UNPLUGGED, i.e. the whole device disappears, not just the audio state.
+// 2. WASAPI endpoint idle (audio-engine level): the endpoint would typically stay
+// ACTIVE while going quiet -- unlikely to show anything here at all, since nothing
+// in the device's own docs treats "unwritten render buffer" as a state change.
+// 3. Headset firmware auto-off: same observable shape as (1) from Windows' point of
+// view (the endpoint disappears), but no amount of keeping the render endpoint
+// "busy" in software prevents a keyed hardware timer from firing.
+//
+// A DEVICE_STATE_NOTPRESENT/UNPLUGGED transition on the render or capture endpoint,
+// correlated with the moment the user hears the drop, points at (1) or (3). No event
+// at all around the drop, with the endpoint remaining ACTIVE throughout, would point
+// at (2) instead.
+
+#include <Windows.h>
+#include <mmdeviceapi.h>
+#include <wrl/client.h>
+
+#include <atomic>
+#include <mutex>
+#include <string>
+
+class WasapiDeviceWatcher : public IMMNotificationClient {
+public:
+ WasapiDeviceWatcher() = default;
+ ~WasapiDeviceWatcher();
+
+ WasapiDeviceWatcher(const WasapiDeviceWatcher&) = delete;
+ WasapiDeviceWatcher& operator=(const WasapiDeviceWatcher&) = delete;
+
+ // Registers for notifications and emits one baseline event per endpoint
+ // (render + capture) with their state at the moment the recording starts, so a
+ // report has a starting point even if nothing changes afterward.
+ bool start();
+ void stop();
+
+ // IUnknown
+ ULONG STDMETHODCALLTYPE AddRef() override;
+ ULONG STDMETHODCALLTYPE Release() override;
+ HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) override;
+
+ // IMMNotificationClient
+ HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR deviceId, DWORD newState) override;
+ HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR deviceId) override;
+ HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR deviceId) override;
+ HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR defaultDeviceId) override;
+ HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR deviceId, const PROPERTYKEY key) override;
+
+private:
+ void emitBaseline(EDataFlow flow, const wchar_t* flowLabel);
+ void emitDeviceEvent(const wchar_t* eventName, LPCWSTR deviceId, const char* extraJson = nullptr);
+
+ std::atomic<ULONG> refCount_ = 1;
+ Microsoft::WRL::ComPtr<IMMDeviceEnumerator> deviceEnumerator_;
+ bool registered_ = false;
+ // Guards std::cout: notifications can arrive on a COM callback thread
+ // concurrently with the main thread's own JSON event writes.
+ std::mutex outputMutex_;
+};