Skip to content

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

fix(windows): address CodeRabbit findings on the audio-device watcher

abduznik79c3039ede99
diff --git a/electron/native/wgc-capture/src/wasapi_device_watcher.cpp b/electron/native/wgc-capture/src/wasapi_device_watcher.cpp
index d5d78310c..60384678d 100644
--- a/electron/native/wgc-capture/src/wasapi_device_watcher.cpp
+++ b/electron/native/wgc-capture/src/wasapi_device_watcher.cpp
@@ -6,6 +6,7 @@
#include <chrono>
#include <cstdio>
#include <iostream>
+#include <sstream>
namespace {
@@ -63,12 +64,15 @@ int64_t nowUnixMillis() {
.count();
}
-std::wstring friendlyNameForDevice(IMMDeviceEnumerator* enumerator, LPCWSTR deviceId) {
- if (!enumerator || !deviceId) {
+// Only ever called from the worker thread or from start() (before the worker
+// exists), never from an IMMNotificationClient callback -- this does a property
+// store round trip and must not run on the callback thread.
+std::wstring friendlyNameForDevice(IMMDeviceEnumerator* enumerator, const std::wstring& deviceId) {
+ if (!enumerator || deviceId.empty()) {
return {};
}
Microsoft::WRL::ComPtr<IMMDevice> device;
- if (FAILED(enumerator->GetDevice(deviceId, &device)) || !device) {
+ if (FAILED(enumerator->GetDevice(deviceId.c_str(), &device)) || !device) {
return {};
}
Microsoft::WRL::ComPtr<IPropertyStore> properties;
@@ -110,8 +114,13 @@ bool WasapiDeviceWatcher::start() {
}
registered_ = true;
- emitBaseline(eRender, L"render");
- emitBaseline(eCapture, L"capture");
+ workerStopRequested_ = false;
+ worker_ = std::thread([this] {
+ workerLoop();
+ });
+
+ enqueueBaseline(eRender, L"render");
+ enqueueBaseline(eCapture, L"capture");
return true;
}
@@ -121,11 +130,58 @@ void WasapiDeviceWatcher::stop() {
}
registered_ = false;
deviceEnumerator_.Reset();
+
+ if (worker_.joinable()) {
+ {
+ std::lock_guard<std::mutex> lock(queueMutex_);
+ workerStopRequested_ = true;
+ }
+ queueCv_.notify_one();
+ worker_.join();
+ }
}
-void WasapiDeviceWatcher::emitBaseline(EDataFlow flow, const wchar_t* flowLabel) {
+void WasapiDeviceWatcher::workerLoop() {
+ // This thread owns all name resolution and all std::cout writes for this
+ // watcher, so nothing here runs on an IMMNotificationClient callback thread.
+ while (true) {
+ PendingEvent event;
+ {
+ std::unique_lock<std::mutex> lock(queueMutex_);
+ queueCv_.wait(lock, [this] { return !queue_.empty() || workerStopRequested_; });
+ if (queue_.empty()) {
+ if (workerStopRequested_) {
+ return;
+ }
+ continue;
+ }
+ event = std::move(queue_.front());
+ queue_.pop();
+ }
+
+ if (event.needsBaselineLookup) {
+ writeBaseline(event);
+ } else {
+ writeDeviceEvent(event);
+ }
+ }
+}
+
+void WasapiDeviceWatcher::enqueueBaseline(EDataFlow flow, const wchar_t* flowLabel) {
+ PendingEvent event;
+ event.needsBaselineLookup = true;
+ event.baselineFlow = flow;
+ event.baselineFlowLabel = flowLabel;
+ {
+ std::lock_guard<std::mutex> lock(queueMutex_);
+ queue_.push(std::move(event));
+ }
+ queueCv_.notify_one();
+}
+
+void WasapiDeviceWatcher::writeBaseline(const PendingEvent& event) {
Microsoft::WRL::ComPtr<IMMDevice> device;
- HRESULT hr = deviceEnumerator_->GetDefaultAudioEndpoint(flow, eConsole, &device);
+ HRESULT hr = deviceEnumerator_->GetDefaultAudioEndpoint(event.baselineFlow, eConsole, &device);
if (FAILED(hr) || !device) {
return;
}
@@ -139,29 +195,49 @@ void WasapiDeviceWatcher::emitBaseline(EDataFlow flow, const wchar_t* flowLabel)
DWORD state = 0;
device->GetState(&state);
- const std::wstring name = friendlyNameForDevice(deviceEnumerator_.Get(), id.c_str());
+ const std::wstring name = friendlyNameForDevice(deviceEnumerator_.Get(), id);
+
+ // Built as one complete string before the write, and written with a single
+ // stream operation under outputMutex_, so main.cpp's own JSON writes can't
+ // land in the middle of this line.
+ std::ostringstream line;
+ line << "{\"event\":\"audio-device-watch\",\"schemaVersion\":1,\"type\":\"baseline\","
+ "\"flow\":\""
+ << wideToUtf8(event.baselineFlowLabel) << "\",\"deviceId\":\"" << jsonEscape(wideToUtf8(id))
+ << "\",\"deviceName\":\"" << jsonEscape(wideToUtf8(name)) << "\",\"state\":\""
+ << deviceStateLabel(state) << "\",\"timestampMs\":" << nowUnixMillis() << "}\n";
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;
+ std::cout << line.str() << std::flush;
+}
+
+void WasapiDeviceWatcher::enqueue(const wchar_t* eventName, LPCWSTR deviceId, const char* extraJson) {
+ PendingEvent event;
+ event.eventName = eventName ? eventName : L"";
+ event.deviceId = deviceId ? deviceId : L"";
+ event.extraJson = extraJson ? extraJson : "";
+ {
+ std::lock_guard<std::mutex> lock(queueMutex_);
+ queue_.push(std::move(event));
+ }
+ queueCv_.notify_one();
}
-void WasapiDeviceWatcher::emitDeviceEvent(const wchar_t* eventName, LPCWSTR deviceId, const char* extraJson) {
- const std::wstring name = friendlyNameForDevice(deviceEnumerator_.Get(), deviceId);
+void WasapiDeviceWatcher::writeDeviceEvent(const PendingEvent& event) {
+ const std::wstring name = friendlyNameForDevice(deviceEnumerator_.Get(), event.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::ostringstream line;
+ line << "{\"event\":\"audio-device-watch\",\"schemaVersion\":1,\"type\":\""
+ << wideToUtf8(event.eventName) << "\",\"deviceId\":\""
+ << jsonEscape(wideToUtf8(event.deviceId)) << "\",\"deviceName\":\""
+ << jsonEscape(wideToUtf8(name)) << "\"";
+ if (!event.extraJson.empty()) {
+ line << "," << event.extraJson;
}
- std::cout << ",\"timestampMs\":" << nowUnixMillis() << "}" << std::endl;
+ line << ",\"timestampMs\":" << nowUnixMillis() << "}\n";
+
+ std::lock_guard<std::mutex> lock(outputMutex_);
+ std::cout << line.str() << std::flush;
}
ULONG STDMETHODCALLTYPE WasapiDeviceWatcher::AddRef() {
@@ -189,20 +265,25 @@ HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::QueryInterface(REFIID riid, void*
return E_NOINTERFACE;
}
+// Every method below runs on a COM callback thread and must be nonblocking per
+// IMMNotificationClient's documented contract: no name resolution, no I/O, no
+// lock that can wait. Each one only copies its arguments and enqueues them --
+// see workerLoop() for where the real work happens.
+
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);
+ enqueue(L"state-changed", deviceId, extra);
return S_OK;
}
HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnDeviceAdded(LPCWSTR deviceId) {
- emitDeviceEvent(L"added", deviceId);
+ enqueue(L"added", deviceId, nullptr);
return S_OK;
}
HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnDeviceRemoved(LPCWSTR deviceId) {
- emitDeviceEvent(L"removed", deviceId);
+ enqueue(L"removed", deviceId, nullptr);
return S_OK;
}
@@ -216,7 +297,7 @@ HRESULT STDMETHODCALLTYPE WasapiDeviceWatcher::OnDefaultDeviceChanged(
}
char extra[16];
snprintf(extra, sizeof(extra), "\"flow\":\"%s\"", flow == eRender ? "render" : "capture");
- emitDeviceEvent(L"default-changed", defaultDeviceId, extra);
+ enqueue(L"default-changed", defaultDeviceId, extra);
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
index b17752bed..930d104f1 100644
--- a/electron/native/wgc-capture/src/wasapi_device_watcher.h
+++ b/electron/native/wgc-capture/src/wasapi_device_watcher.h
@@ -27,14 +27,22 @@
// 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.
+//
+// IMMNotificationClient callbacks must be nonblocking (never resolve names, take a
+// lock that can wait, or do I/O) per Microsoft's documented contract, so the
+// callbacks here only copy their arguments into a PendingEvent and hand it to a
+// worker thread, which does the (possibly slow) name lookup and the actual write.
#include <Windows.h>
#include <mmdeviceapi.h>
#include <wrl/client.h>
#include <atomic>
+#include <condition_variable>
#include <mutex>
+#include <queue>
#include <string>
+#include <thread>
class WasapiDeviceWatcher : public IMMNotificationClient {
public:
@@ -44,10 +52,12 @@ public:
WasapiDeviceWatcher(const WasapiDeviceWatcher&) = delete;
WasapiDeviceWatcher& operator=(const WasapiDeviceWatcher&) = delete;
- // Registers for notifications and emits one baseline event per endpoint
+ // Registers for notifications and enqueues 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();
+ // Unregisters callbacks, drains the queue, and joins the worker before
+ // returning, so every event queued before stop() is guaranteed to be written.
void stop();
// IUnknown
@@ -63,13 +73,33 @@ public:
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);
+ struct PendingEvent {
+ std::wstring eventName;
+ std::wstring deviceId;
+ std::string extraJson;
+ // Baseline events resolve the endpoint themselves (they run on start(), not
+ // a callback thread, so there is no blocking concern) and need no lookup.
+ bool needsBaselineLookup = false;
+ EDataFlow baselineFlow = eRender;
+ std::wstring baselineFlowLabel;
+ };
+
+ void enqueue(const wchar_t* eventName, LPCWSTR deviceId, const char* extraJson);
+ void enqueueBaseline(EDataFlow flow, const wchar_t* flowLabel);
+ void workerLoop();
+ void writeDeviceEvent(const PendingEvent& event);
+ void writeBaseline(const PendingEvent& event);
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::thread worker_;
+ std::mutex queueMutex_;
+ std::condition_variable queueCv_;
+ std::queue<PendingEvent> queue_;
+ bool workerStopRequested_ = false;
+ // Guards std::cout so the worker's writes and main.cpp's own JSON event writes
+ // don't interleave into a malformed line.
std::mutex outputMutex_;
};
diff --git a/electron/native/wgc-capture/src/wasapi_render_keepalive.cpp b/electron/native/wgc-capture/src/wasapi_render_keepalive.cpp
index b16ae7991..d8a9c306c 100644
--- a/electron/native/wgc-capture/src/wasapi_render_keepalive.cpp
+++ b/electron/native/wgc-capture/src/wasapi_render_keepalive.cpp
@@ -77,7 +77,16 @@ bool WasapiRenderKeepAlive::start() {
}
thread_ = std::thread([this] {
+ // GetBuffer/ReleaseBuffer/GetCurrentPadding are called from this thread,
+ // which is otherwise never COM-initialized. Both this and the wmain thread
+ // are MTA (see winrt::init_apartment in main.cpp), so no marshaling is
+ // needed -- this only satisfies the "calling thread must be initialized"
+ // requirement.
+ const HRESULT comInit = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
renderLoop();
+ if (SUCCEEDED(comInit)) {
+ CoUninitialize();
+ }
});
return true;
}