Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

Fix corrupted MP4 recordings on Windows by serializing encoder writes

Pierre-Adrien Lairaf93de1fb1a5
diff --git a/electron/native/wgc-capture/src/mf_encoder.cpp b/electron/native/wgc-capture/src/mf_encoder.cpp
index a1474c20..1adef1b6 100644
--- a/electron/native/wgc-capture/src/mf_encoder.cpp
+++ b/electron/native/wgc-capture/src/mf_encoder.cpp
@@ -124,6 +124,7 @@ bool MFEncoder::initialize(const std::wstring& outputPath, int width, int height
}
bool MFEncoder::writeFrame(ID3D11Texture2D* texture, int64_t timestampHns) {
+ std::lock_guard<std::mutex> lock(writeMutex_);
if (!initialized_ || !sinkWriter_) return false;
context_->CopyResource(stagingTexture_.Get(), texture);
@@ -190,6 +191,7 @@ bool MFEncoder::writeFrame(ID3D11Texture2D* texture, int64_t timestampHns) {
}
bool MFEncoder::finalize() {
+ std::lock_guard<std::mutex> lock(writeMutex_);
if (!initialized_) return false;
initialized_ = false;
diff --git a/electron/native/wgc-capture/src/mf_encoder.h b/electron/native/wgc-capture/src/mf_encoder.h
index 7eb8e642..11b9c774 100644
--- a/electron/native/wgc-capture/src/mf_encoder.h
+++ b/electron/native/wgc-capture/src/mf_encoder.h
@@ -8,6 +8,7 @@
#include <wrl/client.h>
#include <string>
#include <vector>
+#include <mutex>
using Microsoft::WRL::ComPtr;
@@ -32,4 +33,5 @@ private:
int height_ = 0;
int fps_ = 60;
bool initialized_ = false;
+ std::mutex writeMutex_;
};