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-capture): attribute encoder-setup failures to the correct stage

My-Denia110659e58855
diff --git a/electron/native/README.md b/electron/native/README.md
index 8306a2458..2ac0ce96f 100644
--- a/electron/native/README.md
+++ b/electron/native/README.md
@@ -84,7 +84,7 @@ Current V2 JSON shape:
The current helper implementation supports display/window video capture, system audio loopback, selected-microphone capture, Media Foundation webcam capture, and a DirectShow webcam fallback for virtual cameras that are not exposed through Media Foundation. Webcam frames are currently composed into the primary MP4 as a bottom-right picture-in-picture overlay. Browser `deviceId` values do not always map to Media Foundation symbolic links or WASAPI endpoint IDs, so the renderer passes both browser IDs and user-visible device names. For microphones, the helper tries the requested WASAPI endpoint ID first, then resolves an active capture endpoint by `microphoneDeviceName`, then falls back to the default endpoint. For webcams, Electron resolves a matching DirectShow filter CLSID for the selected label; the helper uses Media Foundation first, then that exact DirectShow filter when the requested camera is absent from Media Foundation.
-Encoder selection: by default the helper keeps the existing sink-writer path first. If that path fails while setting up H.264, it retries with the Microsoft software H.264 encoder (`mfh264enc.dll`) registered locally in the helper process and hardware transforms disabled for that retry. Set `preferSoftwareEncoder: true` in the helper JSON, or set `OPENSCREEN_WGC_PREFER_SOFTWARE_ENCODER=true` before launching Electron, to force the software path from the first attempt.
+Encoder selection: by default the helper keeps the existing sink-writer path first. If that path fails while setting up H.264, it retries with the Microsoft software H.264 encoder (`mfh264enc.dll`). The key of this retry is registering that encoder locally in the helper process via `MFTRegisterLocalByCLSID`, which makes a software H.264 encoder available even when the machine's hardware encoders are missing or broken; hardware transforms are disabled for the retry only as a secondary guard so the sink writer prefers the locally registered software encoder, not as the fallback mechanism itself. Set `preferSoftwareEncoder: true` in the helper JSON, or set `OPENSCREEN_WGC_PREFER_SOFTWARE_ENCODER=true` before launching Electron, to force the software path from the first attempt.
Encoder diagnostic on final sink-writer failure: when the final `MFCreateSinkWriterFromURL` attempt fails, the helper logs the registered H.264 video encoder MFT count (via `MFTEnumEx`), the registered AAC encoder count when audio was requested, and the hex HRESULT. If no H.264 encoder is registered, it additionally emits the four-bullet actionable error (missing Media Feature Pack / GPU driver registration / empty `HKLM:\SOFTWARE\Microsoft\Windows Media Foundation\Transforms` / reboot). If an H.264 encoder IS registered but the sink writer still failed, it logs a hint pointing at invalid output path, missing MP4 mux, or GPU driver incompatibility. There is still no fail-fast pre-flight gate because `MFTEnumEx` and `MFCreateSinkWriterFromURL` can disagree about which H.264 encoders are available in non-interactive / Session 0 contexts.
diff --git a/electron/native/wgc-capture/src/mf_encoder.cpp b/electron/native/wgc-capture/src/mf_encoder.cpp
index d1c779b96..0c567ecb7 100644
--- a/electron/native/wgc-capture/src/mf_encoder.cpp
+++ b/electron/native/wgc-capture/src/mf_encoder.cpp
@@ -127,6 +127,20 @@ constexpr const char* kVideoEncoderSelectionDefault = "default";
constexpr const char* kVideoEncoderSelectionSoftwarePreferred = "software-preferred";
constexpr const char* kVideoEncoderSelectionSoftwareFallback = "software-fallback";
+// Which step of createSinkWriterFromUrl produced a failing HRESULT. Only a
+// CreateSinkWriter failure means MFCreateSinkWriterFromURL itself failed and
+// warrants the encoder-enumeration diagnostics in logSinkWriterCreateFailure.
+// The earlier software-path setup steps each log their own specific error, so
+// routing them through the sink-writer diagnostics would misattribute the
+// failure (e.g. reporting a local MFT registration failure as a sink-writer
+// failure on the VM / headless / broken-driver systems this path targets).
+enum class SinkWriterCreateStage {
+ SoftwareEncoderRegistration,
+ CreateAttributes,
+ DisableHardwareTransforms,
+ CreateSinkWriter,
+};
+
HRESULT ensureSoftwareH264EncoderRegisteredForProcess() {
static std::mutex registrationMutex;
static bool attempted = false;
@@ -165,11 +179,26 @@ HRESULT ensureSoftwareH264EncoderRegisteredForProcess() {
HRESULT createSinkWriterFromUrl(
const std::wstring& outputPath,
bool forceSoftwareEncoder,
- Microsoft::WRL::ComPtr<IMFSinkWriter>& sinkWriter) {
+ Microsoft::WRL::ComPtr<IMFSinkWriter>& sinkWriter,
+ SinkWriterCreateStage& failedStage) {
+ // Default to the sink-writer creation step; the software-path steps below
+ // overwrite this only when one of them returns early with a failure.
+ failedStage = SinkWriterCreateStage::CreateSinkWriter;
+
Microsoft::WRL::ComPtr<IMFAttributes> attributes;
if (forceSoftwareEncoder) {
+ // The software fallback works by registering the Microsoft software
+ // H.264 encoder MFT *in this helper process* via MFTRegisterLocalByCLSID
+ // (see ensureSoftwareH264EncoderRegisteredForProcess). That in-process
+ // registration is the key mechanism: it makes a software H.264 encoder
+ // available even when the machine's registered hardware encoders are
+ // missing or broken. Clearing MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS is
+ // only a secondary guard so the sink writer prefers the locally
+ // registered software encoder over a hardware transform; it is not
+ // itself the fallback mechanism.
const HRESULT registerHr = ensureSoftwareH264EncoderRegisteredForProcess();
if (FAILED(registerHr)) {
+ failedStage = SinkWriterCreateStage::SoftwareEncoderRegistration;
return registerHr;
}
@@ -177,16 +206,19 @@ HRESULT createSinkWriterFromUrl(
if (FAILED(hr)) {
std::cerr << "ERROR: MFCreateAttributes(sink writer) failed (hr=0x"
<< std::hex << hr << std::dec << ")" << std::endl;
+ failedStage = SinkWriterCreateStage::CreateAttributes;
return hr;
}
hr = attributes->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, FALSE);
if (FAILED(hr)) {
std::cerr << "ERROR: Set MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS failed (hr=0x"
<< std::hex << hr << std::dec << ")" << std::endl;
+ failedStage = SinkWriterCreateStage::DisableHardwareTransforms;
return hr;
}
}
+ failedStage = SinkWriterCreateStage::CreateSinkWriter;
return MFCreateSinkWriterFromURL(
outputPath.c_str(), nullptr, attributes.Get(), &sinkWriter);
}
@@ -340,16 +372,26 @@ bool MFEncoder::initialize(
bool logCreateFailure) {
resetSinkWriterAttempt();
+ SinkWriterCreateStage failedStage = SinkWriterCreateStage::CreateSinkWriter;
const HRESULT sinkWriterHr = createSinkWriterFromUrl(
outputPath,
forceSoftwareEncoder,
- sinkWriter_);
+ sinkWriter_,
+ failedStage);
if (FAILED(sinkWriterHr)) {
- if (logCreateFailure) {
- logSinkWriterCreateFailure(sinkWriterHr, audioFormat);
- } else {
- std::cerr << "WARNING: Default MFCreateSinkWriterFromURL failed (hr=0x"
- << std::hex << sinkWriterHr << std::dec << ")" << std::endl;
+ // Only a genuine MFCreateSinkWriterFromURL failure gets the
+ // sink-writer / encoder-enumeration diagnostics. The earlier
+ // software-path steps (local MFT registration, attribute creation,
+ // hardware-transform flag) already logged their own specific error
+ // inside createSinkWriterFromUrl, so logging here as well would
+ // misattribute those failures to MFCreateSinkWriterFromURL.
+ if (failedStage == SinkWriterCreateStage::CreateSinkWriter) {
+ if (logCreateFailure) {
+ logSinkWriterCreateFailure(sinkWriterHr, audioFormat);
+ } else {
+ std::cerr << "WARNING: Default MFCreateSinkWriterFromURL failed (hr=0x"
+ << std::hex << sinkWriterHr << std::dec << ")" << std::endl;
+ }
}
return false;
}