Skip to content

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

fix(wgc): keep an encode off the frame lock, and name where it wedges

Etienne Lescot160806f799a3
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index 376378f72..bc96b150a 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -1118,8 +1118,12 @@ int main(int argc, char* argv[]) {
// sleep still be killed.
if (stopElapsedMs() >= currentStepDeadlineMs.load() && !shutdownComplete.load()) {
const char* step = currentStopStep.load();
+ // The encoder stage is what turns "video-writer-join was
+ // abandoned" into something actionable: it names the call the
+ // writer thread is sitting in, instead of leaving the next
+ // report to guess the way issue #252 had to.
std::cerr << "[stop-timing] step=" << step << " elapsed_ms=" << stopElapsedMs()
- << " phase=abandoned" << std::endl;
+ << " phase=abandoned encode_stage=" << encoder.encodeStage() << std::endl;
std::cout << "{\"event\":\"stop-timeout\",\"schemaVersion\":2,\"step\":\"" << step
<< "\"}" << std::endl;
std::cout.flush();
diff --git a/electron/native/wgc-capture/src/mf_encoder.cpp b/electron/native/wgc-capture/src/mf_encoder.cpp
index d880e3669..046a63e22 100644
--- a/electron/native/wgc-capture/src/mf_encoder.cpp
+++ b/electron/native/wgc-capture/src/mf_encoder.cpp
@@ -362,6 +362,39 @@ bool MFEncoder::usesDxgiInput() const {
return useDxgiInput_;
}
+const char* MFEncoder::encodeStage() const {
+ return encodeStage_.load();
+}
+
+int64_t MFEncoder::nextSampleTime(int64_t timestampHns, int64_t sampleDuration) {
+ // On `timestampMutex_` and not `writerMutex_`, deliberately. Every caller
+ // of this runs under main.cpp's frame lock, and `writerMutex_` is held
+ // across IMFSinkWriter::WriteSample by both submitVideoSample and
+ // writeAudio. Taking it here put a synchronous encode inside the frame
+ // lock: an audio WriteSample would stall the video writer, the video
+ // writer would stall every WGC callback waiting on that lock, and stop
+ // would find wgc-quiesce undrained and the writer unjoinable. That is the
+ // system-audio reproduction in the issue #252 follow-up. Nothing else
+ // touches these two fields, so they get a lock of their own that no
+ // blocking call is ever held across.
+ //
+ // No sinkWriter_/finalized_ check any more either: that check was the only
+ // other reason to be on writerMutex_, and submitVideoSample already makes
+ // it before writing. A sample built for a writer that has since gone away
+ // is discarded there, which costs one wasted buffer on a path that is
+ // shutting down anyway.
+ std::scoped_lock lock(timestampMutex_);
+ if (firstTimestampHns_ < 0) {
+ firstTimestampHns_ = timestampHns;
+ }
+ int64_t sampleTime = timestampHns - firstTimestampHns_;
+ if (sampleTime <= lastTimestampHns_) {
+ sampleTime = lastTimestampHns_ + sampleDuration;
+ }
+ lastTimestampHns_ = sampleTime;
+ return sampleTime;
+}
+
bool MFEncoder::initialize(
const std::wstring& outputPath,
int width,
@@ -996,13 +1029,18 @@ MFEncoder::Nv12ConvertResult MFEncoder::convertBgraTextureToNv12(
// Key 0 is the capture side's, key 1 the encoder's. Timing out here leaves
// key 0 exactly where it was, so the next frame simply tries again; that
// is the whole reason this one is recoverable and the one below is not.
+ encodeStage_ = "bridge-acquire-capture";
if (FAILED(captureBridgeMutex_->AcquireSync(0, acquireTimeoutMs))) {
+ encodeStage_ = "idle";
return Nv12ConvertResult::Contended;
}
+ encodeStage_ = "bridge-copy";
captureContext_->CopyResource(captureBridgeTexture_.Get(), texture);
+ encodeStage_ = "bridge-release-capture";
if (!succeeded(captureBridgeMutex_->ReleaseSync(1), "Release capture bridge")) {
return Nv12ConvertResult::Failed;
}
+ encodeStage_ = "bridge-acquire-encoder";
// Key 1 was just handed over by this same thread and nothing else in the
// process can hold it, so a failure here means the bridge is broken rather
// than busy, and no later frame could recover it.
@@ -1021,6 +1059,7 @@ MFEncoder::Nv12ConvertResult MFEncoder::convertBgraTextureToNv12(
outputViewDesc.Texture2D.MipSlice = 0;
Microsoft::WRL::ComPtr<ID3D11VideoProcessorOutputView> outputView;
+ encodeStage_ = "output-view";
if (!succeeded(
videoDevice_->CreateVideoProcessorOutputView(
outputTexture,
@@ -1040,6 +1079,7 @@ MFEncoder::Nv12ConvertResult MFEncoder::convertBgraTextureToNv12(
stream.FutureFrames = 0;
stream.pInputSurface = bridgeInputView_.Get();
+ encodeStage_ = "video-processor-blt";
const bool converted = succeeded(
videoContext_->VideoProcessorBlt(
videoProcessor_.Get(),
@@ -1048,7 +1088,9 @@ MFEncoder::Nv12ConvertResult MFEncoder::convertBgraTextureToNv12(
1,
&stream),
"VideoProcessorBlt");
+ encodeStage_ = "bridge-release-encoder";
const bool released = releaseEncoderBridge();
+ encodeStage_ = "idle";
return converted && released ? Nv12ConvertResult::Ok : Nv12ConvertResult::Failed;
}
@@ -1071,7 +1113,9 @@ bool MFEncoder::captureDxgiSample(
}
Microsoft::WRL::ComPtr<IMFSample> sample;
+ encodeStage_ = "allocate-sample";
if (!succeeded(videoSampleAllocator_->AllocateSample(&sample), "Allocate DXGI video sample")) {
+ encodeStage_ = "idle";
return false;
}
@@ -1107,21 +1151,7 @@ bool MFEncoder::captureDxgiSample(
// does, would let every dropped frame still advance lastTimestampHns_ and
// stretch the timeline by the frames that were never written.
const int64_t sampleDuration = 10'000'000LL / fps_;
- int64_t sampleTime = 0;
- {
- std::scoped_lock writerLock(writerMutex_);
- if (!sinkWriter_ || finalized_) {
- return false;
- }
- if (firstTimestampHns_ < 0) {
- firstTimestampHns_ = timestampHns;
- }
- sampleTime = timestampHns - firstTimestampHns_;
- if (sampleTime <= lastTimestampHns_) {
- sampleTime = lastTimestampHns_ + sampleDuration;
- }
- lastTimestampHns_ = sampleTime;
- }
+ const int64_t sampleTime = nextSampleTime(timestampHns, sampleDuration);
DWORD maximumLength = 0;
if (!succeeded(buffer->GetMaxLength(&maximumLength), "IMFMediaBuffer::GetMaxLength(DXGI)")) {
@@ -1147,23 +1177,7 @@ bool MFEncoder::captureVideoSample(
outSample.Reset();
const int64_t sampleDuration = 10'000'000LL / fps_;
- int64_t sampleTime = 0;
- {
- std::scoped_lock writerLock(writerMutex_);
- if (!sinkWriter_ || finalized_) {
- return false;
- }
-
- if (firstTimestampHns_ < 0) {
- firstTimestampHns_ = timestampHns;
- }
-
- sampleTime = timestampHns - firstTimestampHns_;
- if (sampleTime <= lastTimestampHns_) {
- sampleTime = lastTimestampHns_ + sampleDuration;
- }
- lastTimestampHns_ = sampleTime;
- }
+ const int64_t sampleTime = nextSampleTime(timestampHns, sampleDuration);
// The GPU readback below (copyFrameToBuffer -> CopyResource/Map on
// `texture`) is not internally synchronized here. Callers must hold their
@@ -1210,23 +1224,7 @@ bool MFEncoder::captureBgraSample(
outSample.Reset();
const int64_t sampleDuration = 10'000'000LL / fps_;
- int64_t sampleTime = 0;
- {
- std::scoped_lock writerLock(writerMutex_);
- if (!sinkWriter_ || finalized_) {
- return false;
- }
-
- if (firstTimestampHns_ < 0) {
- firstTimestampHns_ = timestampHns;
- }
-
- sampleTime = timestampHns - firstTimestampHns_;
- if (sampleTime <= lastTimestampHns_) {
- sampleTime = lastTimestampHns_ + sampleDuration;
- }
- lastTimestampHns_ = sampleTime;
- }
+ const int64_t sampleTime = nextSampleTime(timestampHns, sampleDuration);
Microsoft::WRL::ComPtr<IMFMediaBuffer> buffer;
const DWORD frameBytes = static_cast<DWORD>(width_ * height_ * 4);
@@ -1270,11 +1268,15 @@ bool MFEncoder::submitVideoSample(IMFSample* sample) {
// encode synchronously on the calling thread. Callers must NOT hold any
// lock shared with a thread that needs to make timely progress (e.g. a
// stop-request check) across this call.
+ encodeStage_ = "write-sample";
std::scoped_lock writerLock(writerMutex_);
if (!sinkWriter_ || finalized_) {
+ encodeStage_ = "idle";
return false;
}
- return succeeded(sinkWriter_->WriteSample(videoStreamIndex_, sample), "WriteSample");
+ const bool written = succeeded(sinkWriter_->WriteSample(videoStreamIndex_, sample), "WriteSample");
+ encodeStage_ = "idle";
+ return written;
}
bool MFEncoder::writeAudio(const BYTE* data, DWORD byteCount, int64_t timestampHns, int64_t durationHns) {
diff --git a/electron/native/wgc-capture/src/mf_encoder.h b/electron/native/wgc-capture/src/mf_encoder.h
index 689c85cfa..fd2797006 100644
--- a/electron/native/wgc-capture/src/mf_encoder.h
+++ b/electron/native/wgc-capture/src/mf_encoder.h
@@ -7,6 +7,7 @@
#include <mfreadwrite.h>
#include <wrl/client.h>
+#include <atomic>
#include <cstdint>
#include <mutex>
#include <string>
@@ -91,6 +92,8 @@ public:
// call, or a machine that fell back would be fed DXGI samples the sink
// writer was never configured for.
bool usesDxgiInput() const;
+ // A breadcrumb, not state: safe to read from another thread at any time.
+ const char* encodeStage() const;
private:
// Contended is not Failed: the bridge is a two-key handshake and a missed
@@ -108,6 +111,7 @@ private:
bool initializeVideoProcessor();
bool initializeSampleAllocator(IMFMediaType* inputType);
void applyHardwareRateControl(int bitrate);
+ int64_t nextSampleTime(int64_t timestampHns, int64_t sampleDuration);
Nv12ConvertResult convertBgraTextureToNv12(
ID3D11Texture2D* texture,
ID3D11Texture2D* outputTexture);
@@ -138,7 +142,19 @@ private:
Microsoft::WRL::ComPtr<ID3D11VideoProcessorEnumerator> videoProcessorEnumerator_;
Microsoft::WRL::ComPtr<ID3D11VideoProcessor> videoProcessor_;
UINT dxgiResetToken_ = 0;
+ // Guards the sink writer, and is held across IMFSinkWriter::WriteSample --
+ // a synchronous encode. Only threads that can afford to wait out an encode
+ // may take it, which rules out anything holding a caller's frame lock.
std::mutex writerMutex_;
+ // Guards the sample clock alone, so the capture* entry points (which do run
+ // under a caller's frame lock) never queue behind an encode. Splitting this
+ // out is what stops an audio WriteSample from wedging the video writer, and
+ // through it the WGC callbacks, at stop (issue #252 follow-up).
+ std::mutex timestampMutex_;
+ // Where the encoder is right now, for the shutdown watchdog to name when a
+ // step overruns. `video-writer-join phase=abandoned` says which thread is
+ // stuck; this says which call it is stuck in.
+ std::atomic<const char*> encodeStage_{"idle"};
DWORD videoStreamIndex_ = 0;
DWORD audioStreamIndex_ = 0;
bool hasAudioStream_ = false;