fix(windows): preserve WGC duration through static frames

This commit is contained in:
wiiiii123
2026-05-22 15:56:36 +07:00
parent 5924f92c9a
commit 6227eb8955
4 changed files with 24 additions and 7 deletions
@@ -5,10 +5,10 @@
"helpers": {
"wgc-capture": {
"binaryName": "wgc-capture.exe",
"binarySha256": "4f8873abbff58add37672114184c154dee838939ee8f2b7df45d658d078af28c",
"binarySha256": "a04ecf49a5c0b9b92dfb9cd3aa1468178f7a144ac8b78b433e99b34425e35eac",
"sourceDir": "electron/native/wgc-capture",
"sourceFingerprint": "6f725fad6eb515d81b2d553ec45ddbf7c681d2725bba48f0c0722ce00166d967",
"updatedAt": "2026-05-09T00:49:28.306Z"
"sourceFingerprint": "ef3602f1641538a9a19def9da84f1fcd8e37eb53f3e55c1ddf46f26ba75eb54c",
"updatedAt": "2026-05-22T08:56:01.149Z"
},
"cursor-monitor": {
"binaryName": "cursor-monitor.exe",
Binary file not shown.
+20 -4
View File
@@ -227,6 +227,12 @@ bool MFEncoder::writeFrame(ID3D11Texture2D* texture, int64_t timestampHns) {
context_->Unmap(stagingTexture_.Get(), 0);
// WGC may stop delivering frames while the scene is static; keep the MP4
// timeline continuous by repeating the previous frame before writing a new one.
if (!lastFrameBuffer_.empty() && !extendLastFrameToLocked(timestampHns)) {
return false;
}
bool wroteSample = writeNv12SampleLocked(nv12Buffer_, timestampHns);
if (wroteSample) {
lastFrameBuffer_ = nv12Buffer_;
@@ -238,19 +244,29 @@ bool MFEncoder::writeFrame(ID3D11Texture2D* texture, int64_t timestampHns) {
bool MFEncoder::extendLastFrameTo(int64_t timestampHns) {
std::lock_guard<std::mutex> lock(mutex_);
return extendLastFrameToLocked(timestampHns);
}
bool MFEncoder::extendLastFrameToLocked(int64_t timestampHns) {
if (!initialized_ || !sinkWriter_) return false;
if (lastFrameBuffer_.empty()) return false;
if (lastSampleTimeHns_ < 0) return false;
const int64_t frameDurationHns = 10000000LL / fps_;
if (lastSampleTimeHns_ >= 0 && timestampHns <= lastSampleTimeHns_ + frameDurationHns) {
if (frameDurationHns <= 0) return false;
if (timestampHns <= lastSampleTimeHns_ + frameDurationHns) {
return true;
}
if (!writeNv12SampleLocked(lastFrameBuffer_, timestampHns)) {
return false;
int64_t nextSampleTimeHns = lastSampleTimeHns_ + frameDurationHns;
while (nextSampleTimeHns + frameDurationHns <= timestampHns) {
if (!writeNv12SampleLocked(lastFrameBuffer_, nextSampleTimeHns)) {
return false;
}
lastSampleTimeHns_ = nextSampleTimeHns;
nextSampleTimeHns += frameDurationHns;
}
lastSampleTimeHns_ = timestampHns;
return true;
}
@@ -24,6 +24,7 @@ public:
bool finalize();
private:
bool extendLastFrameToLocked(int64_t timestampHns);
bool writeNv12SampleLocked(const std::vector<uint8_t>& frameBuffer, int64_t timestampHns);
ComPtr<IMFSinkWriter> sinkWriter_;