Skip to content

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

perf(windows): skip the webcam frame copy when its sequence has not changed

EtienneLescot1f5d9d8f490b
diff --git a/electron/native/wgc-capture/CMakeLists.txt b/electron/native/wgc-capture/CMakeLists.txt
index 243c5bca5..c242db712 100644
--- a/electron/native/wgc-capture/CMakeLists.txt
+++ b/electron/native/wgc-capture/CMakeLists.txt
@@ -157,3 +157,16 @@ target_compile_definitions(frame_visibility_test PRIVATE
)
target_compile_options(frame_visibility_test PRIVATE /EHsc /W4 /utf-8)
+
+add_executable(webcam_snapshot_test
+ src/dshow_webcam_capture.h
+ src/webcam_snapshot_test.cpp
+)
+
+target_compile_definitions(webcam_snapshot_test PRIVATE
+ NOMINMAX
+ WIN32_LEAN_AND_MEAN
+ _WIN32_WINNT=0x0A00
+)
+
+target_compile_options(webcam_snapshot_test PRIVATE /EHsc /W4 /utf-8)
diff --git a/electron/native/wgc-capture/src/dshow_webcam_capture.cpp b/electron/native/wgc-capture/src/dshow_webcam_capture.cpp
index 01f2e34f4..915e3dc4a 100644
--- a/electron/native/wgc-capture/src/dshow_webcam_capture.cpp
+++ b/electron/native/wgc-capture/src/dshow_webcam_capture.cpp
@@ -576,17 +576,10 @@ void DirectShowWebcamCapture::storeFrame(const BYTE* buffer, long length) {
latestFrameSequence_ += 1;
}
-bool DirectShowWebcamCapture::copyLatestFrame(WebcamFrameSnapshot& destination) {
+bool DirectShowWebcamCapture::copyLatestFrame(WebcamFrameSnapshot& destination, uint64_t lastSeenSequence) {
std::scoped_lock lock(frameMutex_);
- if (latestFrame_.empty() || width_ <= 0 || height_ <= 0) {
- return false;
- }
-
- destination.data = latestFrame_;
- destination.width = width_;
- destination.height = height_;
- destination.sequence = latestFrameSequence_;
- return true;
+ return snapshotWebcamFrame(
+ latestFrame_, width_, height_, latestFrameSequence_, lastSeenSequence, destination);
}
int DirectShowWebcamCapture::width() const {
diff --git a/electron/native/wgc-capture/src/dshow_webcam_capture.h b/electron/native/wgc-capture/src/dshow_webcam_capture.h
index c0ba3503d..be4f759a5 100644
--- a/electron/native/wgc-capture/src/dshow_webcam_capture.h
+++ b/electron/native/wgc-capture/src/dshow_webcam_capture.h
@@ -16,6 +16,33 @@ struct WebcamFrameSnapshot {
uint64_t sequence = 0;
};
+/**
+ * Copies the latest camera frame into `destination`, unless the caller already has it.
+ *
+ * The video writer asks on every screen tick, 60 times a second, while a camera
+ * delivers 30: copying first and comparing sequences after cost a full frame
+ * copied for nothing on every other tick. Returns false, leaving `destination`
+ * untouched, when there is no frame yet or its sequence is `lastSeenSequence`.
+ * Both capture backends route `copyLatestFrame` through this.
+ */
+inline bool snapshotWebcamFrame(
+ const std::vector<BYTE>& frame,
+ int width,
+ int height,
+ uint64_t sequence,
+ uint64_t lastSeenSequence,
+ WebcamFrameSnapshot& destination) {
+ if (frame.empty() || width <= 0 || height <= 0 || sequence == lastSeenSequence) {
+ return false;
+ }
+
+ destination.data = frame;
+ destination.width = width;
+ destination.height = height;
+ destination.sequence = sequence;
+ return true;
+}
+
class DirectShowWebcamCapture {
public:
DirectShowWebcamCapture() = default;
@@ -33,7 +60,7 @@ public:
int requestedFps);
bool start();
void stop();
- bool copyLatestFrame(WebcamFrameSnapshot& destination);
+ bool copyLatestFrame(WebcamFrameSnapshot& destination, uint64_t lastSeenSequence);
int width() const;
int height() const;
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index cb6f3a043..5f27c2bb7 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -1175,8 +1175,7 @@ int wmain(int argc, wchar_t* argv[]) {
}
if (webcamActive) {
WebcamFrameSnapshot candidateWebcamFrame;
- if (webcamCapture.copyLatestFrame(candidateWebcamFrame) &&
- candidateWebcamFrame.sequence != latestWebcamSequence &&
+ if (webcamCapture.copyLatestFrame(candidateWebcamFrame, latestWebcamSequence) &&
hasVisibleWebcamContent(candidateWebcamFrame.data, webcamCapture.deliversNv12())) {
latestWebcamFrame = std::move(candidateWebcamFrame.data);
latestWebcamWidth = candidateWebcamFrame.width;
@@ -1470,7 +1469,7 @@ int wmain(int argc, wchar_t* argv[]) {
const auto webcamDeadline = std::chrono::steady_clock::now() + std::chrono::seconds(3);
while (std::chrono::steady_clock::now() < webcamDeadline && !hasVisibleWebcamFrame) {
WebcamFrameSnapshot candidateWebcamFrame;
- if (webcamCapture.copyLatestFrame(candidateWebcamFrame) &&
+ if (webcamCapture.copyLatestFrame(candidateWebcamFrame, latestWebcamSequence) &&
hasVisibleWebcamContent(candidateWebcamFrame.data, webcamCapture.deliversNv12())) {
latestWebcamFrame = std::move(candidateWebcamFrame.data);
latestWebcamWidth = candidateWebcamFrame.width;
diff --git a/electron/native/wgc-capture/src/webcam_capture.cpp b/electron/native/wgc-capture/src/webcam_capture.cpp
index e925264de..2a367c67d 100644
--- a/electron/native/wgc-capture/src/webcam_capture.cpp
+++ b/electron/native/wgc-capture/src/webcam_capture.cpp
@@ -621,20 +621,13 @@ void WebcamCapture::captureLoop() {
CoUninitialize();
}
-bool WebcamCapture::copyLatestFrame(WebcamFrameSnapshot& destination) {
+bool WebcamCapture::copyLatestFrame(WebcamFrameSnapshot& destination, uint64_t lastSeenSequence) {
if (usingDirectShow_) {
- return directShowCapture_.copyLatestFrame(destination);
+ return directShowCapture_.copyLatestFrame(destination, lastSeenSequence);
}
std::scoped_lock lock(frameMutex_);
- if (latestFrame_.empty() || width_ <= 0 || height_ <= 0) {
- return false;
- }
-
- destination.data = latestFrame_;
- destination.width = width_;
- destination.height = height_;
- destination.sequence = latestFrameSequence_;
- return true;
+ return snapshotWebcamFrame(
+ latestFrame_, width_, height_, latestFrameSequence_, lastSeenSequence, destination);
}
bool WebcamCapture::deliversNv12() const {
diff --git a/electron/native/wgc-capture/src/webcam_capture.h b/electron/native/wgc-capture/src/webcam_capture.h
index 612e36603..949282bf9 100644
--- a/electron/native/wgc-capture/src/webcam_capture.h
+++ b/electron/native/wgc-capture/src/webcam_capture.h
@@ -32,7 +32,7 @@ public:
bool preferNv12);
bool start();
void stop();
- bool copyLatestFrame(WebcamFrameSnapshot& destination);
+ bool copyLatestFrame(WebcamFrameSnapshot& destination, uint64_t lastSeenSequence);
int width() const;
int height() const;
diff --git a/electron/native/wgc-capture/src/webcam_snapshot_test.cpp b/electron/native/wgc-capture/src/webcam_snapshot_test.cpp
new file mode 100644
index 000000000..0dd555cb4
--- /dev/null
+++ b/electron/native/wgc-capture/src/webcam_snapshot_test.cpp
@@ -0,0 +1,46 @@
+#include "dshow_webcam_capture.h"
+
+#include <cstdio>
+
+namespace {
+
+int failures = 0;
+
+void expect(const char* label, bool condition) {
+ if (condition) {
+ return;
+ }
+ std::printf("FAIL %s\n", label);
+ ++failures;
+}
+
+} // namespace
+
+int main() {
+ const std::vector<BYTE> frame(4 * 2 * 2, 0x80);
+
+ // A new sequence is copied, with the size and sequence it was stored with.
+ WebcamFrameSnapshot fresh;
+ expect("new sequence is copied", snapshotWebcamFrame(frame, 2, 2, 7, 6, fresh));
+ expect("new sequence carries its data", fresh.data == frame);
+ expect("new sequence carries its size", fresh.width == 2 && fresh.height == 2);
+ expect("new sequence carries its number", fresh.sequence == 7);
+
+ // The regression this file exists for: the writer polls at the screen's
+ // rate, twice the camera's, and a frame it already holds must cost nothing.
+ WebcamFrameSnapshot unchanged;
+ expect("unchanged sequence reports nothing new", !snapshotWebcamFrame(frame, 2, 2, 7, 7, unchanged));
+ expect("unchanged sequence copies nothing", unchanged.data.empty() && unchanged.sequence == 0);
+
+ // No frame stored yet: nothing to hand out, whatever the caller last saw.
+ WebcamFrameSnapshot empty;
+ expect("empty frame is not copied", !snapshotWebcamFrame({}, 2, 2, 1, 0, empty));
+ expect("unknown size is not copied", !snapshotWebcamFrame(frame, 0, 2, 1, 0, empty));
+
+ if (failures > 0) {
+ std::printf("%d webcam snapshot check(s) failed\n", failures);
+ return 1;
+ }
+ std::printf("webcam snapshot checks passed\n");
+ return 0;
+}
diff --git a/scripts/build-windows-wgc-helper.mjs b/scripts/build-windows-wgc-helper.mjs
index 219d03b08..42594fe27 100644
--- a/scripts/build-windows-wgc-helper.mjs
+++ b/scripts/build-windows-wgc-helper.mjs
@@ -125,3 +125,12 @@ if (!fs.existsSync(frameVisibilityTestPath)) {
// reads every black frame as content.
await run(frameVisibilityTestPath, [], { cwd: BUILD_DIR });
console.log(`Passed ${frameVisibilityTestPath}`);
+
+const webcamSnapshotTestPath = path.join(BUILD_DIR, "webcam_snapshot_test.exe");
+if (!fs.existsSync(webcamSnapshotTestPath)) {
+ throw new Error(`WGC helper build completed but ${webcamSnapshotTestPath} was not found.`);
+}
+// Guards the per-tick webcam poll. The writer asks at the screen's rate, twice
+// the camera's, so a frame it already holds must not be copied again.
+await run(webcamSnapshotTestPath, [], { cwd: BUILD_DIR });
+console.log(`Passed ${webcamSnapshotTestPath}`);