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
fix(windows): give the screen and webcam clocks one origin per tick
EtienneLescote8b85444f216
diff --git a/electron/native/wgc-capture/src/frame_slot_clock_test.cpp b/electron/native/wgc-capture/src/frame_slot_clock_test.cppindex 1b67c8ce5..566e1f188 100644--- a/electron/native/wgc-capture/src/frame_slot_clock_test.cpp+++ b/electron/native/wgc-capture/src/frame_slot_clock_test.cpp@@ -41,6 +41,35 @@ int64_t writeFor(int fps, int64_t durationHns, int64_t workHns, int64_t stallAtHreturn frames;}+// Both streams of one take, the screen at 60 fps and the webcam at 30, on the writer's+// rule: each clock is advanced with the instant its tick STARTED, so a stall inside the+// tick (the screen readback) cannot give the two streams different origins. The first+// tick stalls `firstStallHns`. Returns the written durations, screen minus webcam.+int64_t streamsDriftFor(int64_t durationHns, int64_t firstStallHns) {+ FrameSlotClock screen(60);+ FrameSlotClock webcam(30);+ int64_t nowHns = 0;+ int64_t screenFrames = 0;+ int64_t webcamFrames = 0;+ bool stalled = false;+ while (nowHns < durationHns) {+ const int64_t tickStartHns = nowHns;+ if (tickStartHns >= webcam.nextDueHns()) {+ ++webcamFrames;+ webcam.advance(tickStartHns);+ }+ nowHns += 50'000;+ if (!stalled) {+ nowHns += firstStallHns;+ stalled = true;+ }+ ++screenFrames;+ screen.advance(tickStartHns);+ nowHns += std::clamp<int64_t>(screen.nextDueHns() - nowHns, 0, kSecondHns / 60);+ }+ return screenFrames * kSecondHns / 60 - webcamFrames * kSecondHns / 30;+}+} // namespaceint main() {@@ -61,6 +90,12 @@ int main() {expectEqual("steady writer keeps the count", writeFor(60, 3 * kSecondHns, 50'000, 3 * kSecondHns, 0), 180);expectEqual("stalled writer catches up", writeFor(60, 3 * kSecondHns, 50'000, kSecondHns, 3'000'000), 180);+ // A first tick that stalls 300 ms must not set the screen's origin 300 ms after the+ // webcam's: both streams of a 3 s take come out within one webcam frame of each other.+ const int64_t drift = streamsDriftFor(3 * kSecondHns, 3'000'000);+ expectEqual("screen and webcam stay aligned after a first-tick stall",+ drift > -kSecondHns / 30 && drift < kSecondHns / 30, 1);+// Due times come from the origin, not from adding a truncated 1/fps// (166'666 hns at 60 fps) per frame, which lost 14 ms an hour.FrameSlotClock hour(60);diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cppindex d0962a8d4..90db83665 100644--- a/electron/native/wgc-capture/src/main.cpp+++ b/electron/native/wgc-capture/src/main.cpp@@ -1110,6 +1110,10 @@ int wmain(int argc, wchar_t* argv[]) {int64_t latestFrameTimestampHns = 0;while (!control.stopRequested && !encodeFailed) {+ // One instant per tick for both clocks: read after the screen readback, the+ // screen's would trail the webcam's by any stall in it, and the first tick+ // would give the two streams different origins.+ const int64_t tickStartHns = recordingElapsedHns();Microsoft::WRL::ComPtr<IMFSample> videoSample;Microsoft::WRL::ComPtr<IMFSample> webcamSample;bool hasVideoSample = false;@@ -1225,7 +1229,7 @@ int wmain(int argc, wchar_t* argv[]) {}if (writeSeparateWebcam && webcamFrame.data) {// Anchored to the same recording-start origin as screen video/audio.- const int64_t targetElapsedHns = recordingElapsedHns();+ const int64_t targetElapsedHns = tickStartHns;// The H.264 encoder MFT does not honor irregular per-sample// timestamps for a VFR source -- it numbers output samples// sequentially at its configured nominal rate regardless of the@@ -1363,7 +1367,7 @@ int wmain(int argc, wchar_t* argv[]) {// Capped at one period so a stop is never waited on longer than// that, whatever the count says.const int64_t nowHns = recordingElapsedHns();- videoClock.advance(nowHns);+ videoClock.advance(tickStartHns);std::this_thread::sleep_for(std::min<std::chrono::steady_clock::duration>(std::chrono::duration_cast<std::chrono::steady_clock::duration>(Hns(videoClock.nextDueHns() - nowHns)),