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): keep the render endpoint alive during recording

abduznik50866fd0bac6
diff --git a/electron/native/wgc-capture/CMakeLists.txt b/electron/native/wgc-capture/CMakeLists.txt
index f9bea5354..8545dc087 100644
--- a/electron/native/wgc-capture/CMakeLists.txt
+++ b/electron/native/wgc-capture/CMakeLists.txt
@@ -50,6 +50,8 @@ add_executable(wgc-capture
src/wasapi_device_watcher.h
src/wasapi_loopback_capture.cpp
src/wasapi_loopback_capture.h
+ src/wasapi_render_keepalive.cpp
+ src/wasapi_render_keepalive.h
src/webcam_capture.cpp
src/webcam_capture.h
src/wgc_session.cpp
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index b3c353a5e..4ee39fc3c 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -4,6 +4,7 @@
#include "monitor_utils.h"
#include "wasapi_device_watcher.h"
#include "wasapi_loopback_capture.h"
+#include "wasapi_render_keepalive.h"
#include "webcam_capture.h"
#include "wgc_session.h"
@@ -810,6 +811,24 @@ int wmain(int argc, wchar_t* argv[]) {
deviceWatchActive = false;
}
};
+ // getopenscreen/openscreen#724: confirmed on real hardware that a mic-only
+ // recording lets Windows idle the render endpoint and drop the headset, while
+ // the same recording with system audio (which reads the render endpoint via
+ // loopback) does not -- so writing silence to it is enough to prevent this.
+ // Independent of captureSystemAudio, since mic-only is exactly the case that
+ // otherwise leaves the endpoint untouched. Non-fatal: a recording with no
+ // output device to keep alive, or one where another app holds it exclusively,
+ // is unaffected either way.
+ const bool renderKeepAliveEnabled =
+ readEnvInt("OPENSCREEN_WGC_DISABLE_AUDIO_KEEPALIVE", 0) != 1;
+ WasapiRenderKeepAlive renderKeepAlive;
+ bool renderKeepAliveActive = false;
+ const auto stopRenderKeepAliveIfActive = [&]() {
+ if (renderKeepAliveActive) {
+ renderKeepAlive.stop();
+ renderKeepAliveActive = false;
+ }
+ };
const AudioInputFormat* audioFormat = nullptr;
AudioInputFormat encoderAudioFormat{};
AudioInputFormat systemAudioFormat{};
@@ -1400,8 +1419,17 @@ int wmain(int argc, wchar_t* argv[]) {
}
}
+ if (renderKeepAliveEnabled) {
+ renderKeepAliveActive = renderKeepAlive.start();
+ if (!renderKeepAliveActive) {
+ std::cerr << "WARNING: Failed to start render keep-alive stream; continuing without it"
+ << std::endl;
+ }
+ }
+
if (!startAudioCaptures()) {
stopDeviceWatchIfActive();
+ stopRenderKeepAliveIfActive();
return 1;
}
if (config.webcamEnabled) {
@@ -1409,6 +1437,7 @@ int wmain(int argc, wchar_t* argv[]) {
microphoneCapture.stop();
loopbackCapture.stop();
stopDeviceWatchIfActive();
+ stopRenderKeepAliveIfActive();
if (audioMixer) {
audioMixer->stop();
}
@@ -1441,6 +1470,7 @@ int wmain(int argc, wchar_t* argv[]) {
microphoneCapture.stop();
loopbackCapture.stop();
stopDeviceWatchIfActive();
+ stopRenderKeepAliveIfActive();
if (audioMixer) {
audioMixer->stop();
}
@@ -1485,6 +1515,7 @@ int wmain(int argc, wchar_t* argv[]) {
microphoneCapture.stop();
loopbackCapture.stop();
stopDeviceWatchIfActive();
+ stopRenderKeepAliveIfActive();
webcamCapture.stop();
if (audioMixer) {
audioMixer->stop();
@@ -1625,6 +1656,11 @@ int wmain(int argc, wchar_t* argv[]) {
stopDeviceWatchIfActive();
logStopStep("device-watcher");
}
+ if (renderKeepAliveActive) {
+ beginStopStep("render-keepalive", stepBudgetMs);
+ stopRenderKeepAliveIfActive();
+ logStopStep("render-keepalive");
+ }
beginStopStep("webcam", stepBudgetMs);
webcamCapture.stop();
logStopStep("webcam");
diff --git a/electron/native/wgc-capture/src/wasapi_render_keepalive.cpp b/electron/native/wgc-capture/src/wasapi_render_keepalive.cpp
new file mode 100644
index 000000000..b16ae7991
--- /dev/null
+++ b/electron/native/wgc-capture/src/wasapi_render_keepalive.cpp
@@ -0,0 +1,114 @@
+#include "wasapi_render_keepalive.h"
+
+#include <chrono>
+#include <iostream>
+
+namespace {
+
+constexpr REFERENCE_TIME BufferDurationHns = 10'000'000;
+
+} // namespace
+
+WasapiRenderKeepAlive::~WasapiRenderKeepAlive() {
+ stop();
+ if (mixFormat_) {
+ CoTaskMemFree(mixFormat_);
+ mixFormat_ = nullptr;
+ }
+}
+
+bool WasapiRenderKeepAlive::start() {
+ Microsoft::WRL::ComPtr<IMMDeviceEnumerator> deviceEnumerator;
+ HRESULT hr = CoCreateInstance(
+ __uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, IID_PPV_ARGS(&deviceEnumerator));
+ if (FAILED(hr)) {
+ return false;
+ }
+
+ Microsoft::WRL::ComPtr<IMMDevice> device;
+ hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
+ if (FAILED(hr)) {
+ // No default render device to keep alive -- nothing to do.
+ return false;
+ }
+
+ hr = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, &audioClient_);
+ if (FAILED(hr)) {
+ return false;
+ }
+
+ hr = audioClient_->GetMixFormat(&mixFormat_);
+ if (FAILED(hr) || !mixFormat_) {
+ return false;
+ }
+
+ // Shared mode: mixes into whatever else may be playing rather than requesting
+ // exclusive control, so it can't block another app from using the device, and
+ // it fails cleanly (non-fatal to the caller) if another app already holds it
+ // exclusively.
+ hr = audioClient_->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, BufferDurationHns, 0, mixFormat_, nullptr);
+ if (FAILED(hr)) {
+ return false;
+ }
+
+ hr = audioClient_->GetBufferSize(&bufferFrameCount_);
+ if (FAILED(hr)) {
+ return false;
+ }
+
+ hr = audioClient_->GetService(IID_PPV_ARGS(&renderClient_));
+ if (FAILED(hr)) {
+ return false;
+ }
+
+ // Prime the full buffer with silence before Start() so there's no gap for the
+ // audio engine to glitch on.
+ BYTE* data = nullptr;
+ hr = renderClient_->GetBuffer(bufferFrameCount_, &data);
+ if (FAILED(hr)) {
+ return false;
+ }
+ renderClient_->ReleaseBuffer(bufferFrameCount_, AUDCLNT_BUFFERFLAGS_SILENT);
+
+ stopRequested_ = false;
+ hr = audioClient_->Start();
+ if (FAILED(hr)) {
+ return false;
+ }
+
+ thread_ = std::thread([this] {
+ renderLoop();
+ });
+ return true;
+}
+
+void WasapiRenderKeepAlive::stop() {
+ stopRequested_ = true;
+ if (thread_.joinable()) {
+ thread_.join();
+ }
+ if (audioClient_) {
+ audioClient_->Stop();
+ }
+ renderClient_.Reset();
+ audioClient_.Reset();
+}
+
+void WasapiRenderKeepAlive::renderLoop() {
+ while (!stopRequested_) {
+ UINT32 paddingFrames = 0;
+ if (FAILED(audioClient_->GetCurrentPadding(&paddingFrames))) {
+ break;
+ }
+
+ const UINT32 framesAvailable = bufferFrameCount_ - paddingFrames;
+ if (framesAvailable > 0) {
+ BYTE* data = nullptr;
+ if (SUCCEEDED(renderClient_->GetBuffer(framesAvailable, &data))) {
+ renderClient_->ReleaseBuffer(framesAvailable, AUDCLNT_BUFFERFLAGS_SILENT);
+ }
+ }
+
+ std::this_thread::sleep_for(std::chrono::milliseconds(20));
+ }
+}
diff --git a/electron/native/wgc-capture/src/wasapi_render_keepalive.h b/electron/native/wgc-capture/src/wasapi_render_keepalive.h
new file mode 100644
index 000000000..b7feeed07
--- /dev/null
+++ b/electron/native/wgc-capture/src/wasapi_render_keepalive.h
@@ -0,0 +1,49 @@
+#pragma once
+
+// getopenscreen/openscreen#724: a headset can be idled by Windows partway through a
+// mic-only recording, because nothing in that path ever writes to the render
+// (playback) endpoint -- WasapiLoopbackCapture's system-audio path only ever READS
+// from it via AUDCLNT_STREAMFLAGS_LOOPBACK. Confirmed on real hardware: the drop
+// reproduces with mic-only capture and does NOT reproduce with system audio (i.e.
+// loopback) also enabled, so touching the render endpoint at all is what keeps it
+// alive, and loopback capture already does that as a side effect when it runs.
+//
+// This opens an ordinary render stream on the same default output device and
+// writes silence to it for as long as a recording is running, independent of
+// whether system audio is being captured, since mic-only is exactly the case that
+// leaves the endpoint untouched otherwise.
+//
+// Kill-switch: set OPENSCREEN_WGC_DISABLE_AUDIO_KEEPALIVE=1 to turn this off.
+
+#include <Windows.h>
+#include <audioclient.h>
+#include <mmdeviceapi.h>
+#include <wrl/client.h>
+
+#include <atomic>
+#include <thread>
+
+class WasapiRenderKeepAlive {
+public:
+ WasapiRenderKeepAlive() = default;
+ ~WasapiRenderKeepAlive();
+
+ WasapiRenderKeepAlive(const WasapiRenderKeepAlive&) = delete;
+ WasapiRenderKeepAlive& operator=(const WasapiRenderKeepAlive&) = delete;
+
+ // Opens the default render endpoint in shared mode and starts writing silence.
+ // Returns false on any failure -- including another app holding the device
+ // exclusively -- which callers must treat as non-fatal to the recording itself.
+ bool start();
+ void stop();
+
+private:
+ void renderLoop();
+
+ Microsoft::WRL::ComPtr<IAudioClient> audioClient_;
+ Microsoft::WRL::ComPtr<IAudioRenderClient> renderClient_;
+ WAVEFORMATEX* mixFormat_ = nullptr;
+ UINT32 bufferFrameCount_ = 0;
+ std::thread thread_;
+ std::atomic<bool> stopRequested_ = false;
+};