Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
perf(windows): raise the capture helpers' timer resolution and use MMCSS
EtienneLescote9430049f5fa
diff --git a/electron/native/wgc-capture/CMakeLists.txt b/electron/native/wgc-capture/CMakeLists.txtindex 7a80f3214..243c5bca5 100644--- a/electron/native/wgc-capture/CMakeLists.txt+++ b/electron/native/wgc-capture/CMakeLists.txt@@ -41,6 +41,7 @@ add_executable(wgc-capturesrc/desktop_icon_cover.cppsrc/desktop_icon_cover.hsrc/dpi_awareness.h+ src/realtime_scheduling.hsrc/frame_visibility.cppsrc/frame_visibility.hsrc/dshow_webcam_capture.cpp@@ -73,6 +74,7 @@ target_compile_definitions(wgc-capture PRIVATEtarget_compile_options(wgc-capture PRIVATE /EHsc /W4 /utf-8)target_link_libraries(wgc-capture PRIVATE+ avrtd3d11dxgimf@@ -81,11 +83,13 @@ target_link_libraries(wgc-capture PRIVATEmfuuidruntimeobjectwindowsapp+ winmm)add_executable(cursor-samplersrc/cursor-sampler.cppsrc/dpi_awareness.h+ src/realtime_scheduling.h)target_compile_definitions(cursor-sampler PRIVATE@@ -96,8 +100,10 @@ target_compile_definitions(cursor-sampler PRIVATEtarget_compile_options(cursor-sampler PRIVATE /EHsc /W4 /utf-8)target_link_libraries(cursor-sampler PRIVATE+ avrtgdi32gdiplus+ winmm)add_executable(audio_sample_utils_test@@ -115,11 +121,13 @@ target_compile_definitions(audio_sample_utils_test PRIVATEtarget_compile_options(audio_sample_utils_test PRIVATE /EHsc /W4 /utf-8)target_link_libraries(audio_sample_utils_test PRIVATE+ avrtmfmfplatmfreadwritemfuuidole32+ winmm)add_executable(webcam_format_testdiff --git a/electron/native/wgc-capture/src/audio_sample_utils.cpp b/electron/native/wgc-capture/src/audio_sample_utils.cppindex cab9aab61..4ffe3ff50 100644--- a/electron/native/wgc-capture/src/audio_sample_utils.cpp+++ b/electron/native/wgc-capture/src/audio_sample_utils.cpp@@ -1,4 +1,5 @@#include "audio_sample_utils.h"+#include "realtime_scheduling.h"#include <mfapi.h>@@ -522,6 +523,7 @@ bool AudioMixer::start() {systemDecimator_.reset();microphoneDecimator_.reset();thread_ = std::thread([this] {+ const MmcssThread mmcss(L"Pro Audio");mixLoop();});return true;diff --git a/electron/native/wgc-capture/src/audio_sample_utils_test.cpp b/electron/native/wgc-capture/src/audio_sample_utils_test.cppindex ea235d1e9..2998a5d5d 100644--- a/electron/native/wgc-capture/src/audio_sample_utils_test.cpp+++ b/electron/native/wgc-capture/src/audio_sample_utils_test.cpp@@ -1,4 +1,5 @@#include "audio_sample_utils.h"+#include "realtime_scheduling.h"#include <mfapi.h>#include <mferror.h>@@ -215,6 +216,32 @@ HRESULT trySetAacPcmRateWithVideo(UINT32 sampleRate) {} // namespaceint main() {+ // --- Timer resolution (getopenscreen/openscreen#921) ----------------------+ //+ // First, before anything in this process could have raised it: a 5 ms sleep+ // is what the WASAPI poll asks for, and at the default 15.625 ms tick it+ // lasts a whole tick. Median of 20, so one preempted sleep cannot decide it.+ {+ const auto medianSleepMs = [] {+ std::vector<double> samples;+ for (int i = 0; i < 20; i += 1) {+ const auto start = std::chrono::steady_clock::now();+ std::this_thread::sleep_for(std::chrono::milliseconds(5));+ samples.push_back(+ std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count());+ }+ std::sort(samples.begin(), samples.end());+ return samples[samples.size() / 2];+ };+ const double before = medianSleepMs();+ const HighResolutionTiming timing;+ const double after = medianSleepMs();+ char detail[96]{};+ sprintf_s(detail, "5 ms sleep: median %.2f ms before, %.2f ms after", before, after);+ std::cout << "TIMER_RAW " << detail << std::endl;+ expect("timer-5ms-sleep-lasts-about-5ms", after < 8.0, detail);+ }+const AudioInputFormat diagnostic = makeFormat(MFAudioFormat_Float, 96000, 8, 32);const AudioInputFormat snapped = makeAacCompatibleAudioFormat(diagnostic);expect(diff --git a/electron/native/wgc-capture/src/cursor-sampler.cpp b/electron/native/wgc-capture/src/cursor-sampler.cppindex aa58a1cd7..cf4947c55 100644--- a/electron/native/wgc-capture/src/cursor-sampler.cpp+++ b/electron/native/wgc-capture/src/cursor-sampler.cpp@@ -3,6 +3,7 @@#include <objbase.h>#include "dpi_awareness.h"+#include "realtime_scheduling.h"#include <atomic>#include <algorithm>@@ -429,6 +430,7 @@ int main(int argc, char* argv[]) {std::cerr << "ERROR: Could not enable per-monitor-v2 DPI awareness" << std::endl;return 1;}+ const HighResolutionTiming highResolutionTiming;if (argc < 2) {std::cerr << "Usage: cursor-sampler <intervalMs> [windowHandle]" << std::endl;diff --git a/electron/native/wgc-capture/src/dshow_webcam_capture.cpp b/electron/native/wgc-capture/src/dshow_webcam_capture.cppindex fcbb04d58..01f2e34f4 100644--- a/electron/native/wgc-capture/src/dshow_webcam_capture.cpp+++ b/electron/native/wgc-capture/src/dshow_webcam_capture.cpp@@ -1,5 +1,6 @@#include "dshow_webcam_capture.h"+#include "realtime_scheduling.h"#include "webcam_format.h"#include <initguid.h>@@ -480,6 +481,7 @@ void DirectShowWebcamCapture::stop() {}void DirectShowWebcamCapture::captureLoop() {+ const MmcssThread mmcss(L"Capture");const HRESULT coinitHr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);while (!stopRequested_ && impl_ && impl_->sampleGrabber) {long bufferSize = 0;diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cppindex b094e31f5..cb6f3a043 100644--- a/electron/native/wgc-capture/src/main.cpp+++ b/electron/native/wgc-capture/src/main.cpp@@ -1,6 +1,7 @@#include "audio_sample_utils.h"#include "desktop_icon_cover.h"#include "dpi_awareness.h"+#include "realtime_scheduling.h"#include "mf_encoder.h"#include "monitor_utils.h"#include "wasapi_device_watcher.h"@@ -650,6 +651,7 @@ int wmain(int argc, wchar_t* argv[]) {std::cerr << "ERROR: Could not enable per-monitor-v2 DPI awareness" << std::endl;return 1;}+ const HighResolutionTiming highResolutionTiming;if (argc < 2) {std::cerr << "ERROR: Missing JSON config argument" << std::endl;@@ -1072,6 +1074,7 @@ int wmain(int argc, wchar_t* argv[]) {}auto writeVideoFrames = [&]() {+ const MmcssThread mmcss(L"Capture");const auto frameDuration = std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<double>(1.0 / config.fps));uint64_t frameIndex = 0;diff --git a/electron/native/wgc-capture/src/realtime_scheduling.h b/electron/native/wgc-capture/src/realtime_scheduling.hnew file mode 100644index 000000000..dc8ca5abc--- /dev/null+++ b/electron/native/wgc-capture/src/realtime_scheduling.h@@ -0,0 +1,77 @@+#pragma once++#include <Windows.h>+#include <avrt.h>+#include <timeapi.h>++#include <iostream>++// Every wait in the capture helpers -- the video writer's deadline, the WASAPI+// poll, the audio mixer's chunk clock, the cursor sampler's interval -- lands on+// the system timer tick, and a process that asks for nothing gets the default+// 15.625 ms one. Measured on a real take (getopenscreen/openscreen#920, #921):+// webcam frames spaced in whole ticks, 25.9 unique frames/s for a 30 fps camera;+// the cursor sampled at 22.7 Hz for 30 asked; WASAPI packets handed over late+// enough to leave holes in the voice (#911).+//+// Held for the life of the process. Windows 11 ignores the timer request of a+// process it throttles (a minimized or occluded app, which a recording helper+// always is), so the process is also opted out of power throttling for both+// execution speed and timer resolution. Either call failing is logged and+// survived: the recording still works at the coarser tick, as it always has.+class HighResolutionTiming {+public:+ HighResolutionTiming() {+ PROCESS_POWER_THROTTLING_STATE throttling{};+ throttling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;+ throttling.ControlMask =+ PROCESS_POWER_THROTTLING_EXECUTION_SPEED | PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION;+ throttling.StateMask = 0;+ if (!SetProcessInformation(+ GetCurrentProcess(), ProcessPowerThrottling, &throttling, sizeof(throttling))) {+ std::cerr << "WARNING: Could not opt out of power throttling (error " << GetLastError()+ << ")" << std::endl;+ }+ periodSet_ = timeBeginPeriod(1) == TIMERR_NOERROR;+ if (!periodSet_) {+ std::cerr << "WARNING: Could not raise the timer resolution to 1 ms" << std::endl;+ }+ }+ ~HighResolutionTiming() {+ if (periodSet_) {+ timeEndPeriod(1);+ }+ }+ HighResolutionTiming(const HighResolutionTiming&) = delete;+ HighResolutionTiming& operator=(const HighResolutionTiming&) = delete;++private:+ bool periodSet_ = false;+};++// Registers the calling thread with MMCSS for as long as it lives, so the+// scheduler favours it over ordinary work while the machine is busy -- the+// stalls that outlast the timer tick. `task` is a task name from the registry's+// MMCSS profile: "Pro Audio" for the audio threads, "Capture" for video. A thread+// that cannot register runs at its normal priority, as before.+class MmcssThread {+public:+ explicit MmcssThread(const wchar_t* task) {+ DWORD taskIndex = 0;+ handle_ = AvSetMmThreadCharacteristicsW(task, &taskIndex);+ if (!handle_) {+ std::wcerr << L"WARNING: Could not register thread with MMCSS task \"" << task+ << L"\" (error " << GetLastError() << L")" << std::endl;+ }+ }+ ~MmcssThread() {+ if (handle_) {+ AvRevertMmThreadCharacteristics(handle_);+ }+ }+ MmcssThread(const MmcssThread&) = delete;+ MmcssThread& operator=(const MmcssThread&) = delete;++private:+ HANDLE handle_ = nullptr;+};diff --git a/electron/native/wgc-capture/src/wasapi_loopback_capture.cpp b/electron/native/wgc-capture/src/wasapi_loopback_capture.cppindex c19c44160..875dda566 100644--- a/electron/native/wgc-capture/src/wasapi_loopback_capture.cpp+++ b/electron/native/wgc-capture/src/wasapi_loopback_capture.cpp@@ -1,4 +1,5 @@#include "wasapi_loopback_capture.h"+#include "realtime_scheduling.h"#include <Functiondiscoverykeys_devpkey.h>#include <ksmedia.h>@@ -352,6 +353,7 @@ bool WasapiLoopbackCapture::start(AudioCallback callback) {}thread_ = std::thread([this] {+ const MmcssThread mmcss(L"Pro Audio");captureLoop();});return true;diff --git a/electron/native/wgc-capture/src/webcam_capture.cpp b/electron/native/wgc-capture/src/webcam_capture.cppindex 27145e227..e925264de 100644--- a/electron/native/wgc-capture/src/webcam_capture.cpp+++ b/electron/native/wgc-capture/src/webcam_capture.cpp@@ -1,5 +1,6 @@#include "webcam_capture.h"+#include "realtime_scheduling.h"#include "webcam_format.h"#include <mfapi.h>@@ -506,6 +507,7 @@ void WebcamCapture::stop() {}void WebcamCapture::captureLoop() {+ const MmcssThread mmcss(L"Capture");CoInitializeEx(nullptr, COINIT_MULTITHREADED);const auto loopStartedAt = std::chrono::steady_clock::now();