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(recording): let DirectShow convert a camera format we cannot unpack

EtienneLescotbe8d50b07df8
diff --git a/electron/native/wgc-capture/src/dshow_webcam_capture.cpp b/electron/native/wgc-capture/src/dshow_webcam_capture.cpp
index 7e3f8b7a6..f39e4b065 100644
--- a/electron/native/wgc-capture/src/dshow_webcam_capture.cpp
+++ b/electron/native/wgc-capture/src/dshow_webcam_capture.cpp
@@ -112,40 +112,19 @@ DirectShowWebcamCapture::~DirectShowWebcamCapture() {
delete impl_;
}
-bool DirectShowWebcamCapture::initialize(
- const std::wstring& deviceId,
- const std::wstring& deviceName,
- const std::wstring& directShowClsid,
- int requestedWidth,
- int requestedHeight,
- int requestedFps) {
- (void)deviceId;
- stop();
- delete impl_;
- impl_ = nullptr;
- impl_ = new Impl();
- fps_ = std::clamp(requestedFps > 0 ? requestedFps : 30, 1, 60);
-
- HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
- if (SUCCEEDED(hr)) {
- impl_->comInitialized = true;
- } else if (hr != RPC_E_CHANGED_MODE) {
- return succeeded(hr, "CoInitializeEx(DirectShow webcam)");
- }
-
- if (directShowClsid.empty()) {
- std::cerr << "ERROR: DirectShow webcam fallback requires a resolved filter CLSID" << std::endl;
- return false;
- }
-
- CLSID selectedClsid{};
- if (FAILED(CLSIDFromString(directShowClsid.c_str(), &selectedClsid))) {
- std::cerr << "ERROR: DirectShow webcam fallback received an invalid filter CLSID" << std::endl;
- return false;
- }
- selectedDeviceName_ = deviceName.empty() ? directShowClsid : deviceName;
+bool DirectShowWebcamCapture::buildGraph(const CLSID& sourceClsid, const GUID* preferredSubtype) {
+ // Every attempt starts from empty filters. A RenderStream that fails can
+ // leave pins connected behind it, and retrying on top of that half-built
+ // graph is how you get a second failure that says nothing about the format.
+ impl_->mediaControl.Reset();
+ impl_->nullRenderer.Reset();
+ impl_->sampleGrabber.Reset();
+ impl_->sampleGrabberFilter.Reset();
+ impl_->captureFilter.Reset();
+ impl_->captureGraph.Reset();
+ impl_->graph.Reset();
- if (!succeeded(CoCreateInstance(selectedClsid, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&impl_->captureFilter)),
+ if (!succeeded(CoCreateInstance(sourceClsid, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&impl_->captureFilter)),
"CoCreateInstance(DirectShow webcam filter)")) {
return false;
}
@@ -176,6 +155,9 @@ bool DirectShowWebcamCapture::initialize(
AM_MEDIA_TYPE requestedType{};
requestedType.majortype = MEDIATYPE_Video;
requestedType.formattype = FORMAT_VideoInfo;
+ if (preferredSubtype) {
+ requestedType.subtype = *preferredSubtype;
+ }
if (!succeeded(impl_->sampleGrabber->SetMediaType(&requestedType), "SetMediaType(DirectShow video)")) {
return false;
}
@@ -193,16 +175,90 @@ bool DirectShowWebcamCapture::initialize(
return false;
}
- if (!succeeded(impl_->captureGraph->RenderStream(
- &PIN_CATEGORY_CAPTURE,
- &MEDIATYPE_Video,
- impl_->captureFilter.Get(),
- impl_->sampleGrabberFilter.Get(),
- impl_->nullRenderer.Get()),
- "RenderStream(DirectShow webcam)")) {
+ return succeeded(impl_->captureGraph->RenderStream(
+ &PIN_CATEGORY_CAPTURE,
+ &MEDIATYPE_Video,
+ impl_->captureFilter.Get(),
+ impl_->sampleGrabberFilter.Get(),
+ impl_->nullRenderer.Get()),
+ "RenderStream(DirectShow webcam)");
+}
+
+bool DirectShowWebcamCapture::initialize(
+ const std::wstring& deviceId,
+ const std::wstring& deviceName,
+ const std::wstring& directShowClsid,
+ int requestedWidth,
+ int requestedHeight,
+ int requestedFps) {
+ (void)deviceId;
+ stop();
+ delete impl_;
+ impl_ = nullptr;
+ impl_ = new Impl();
+ fps_ = std::clamp(requestedFps > 0 ? requestedFps : 30, 1, 60);
+
+ HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
+ if (SUCCEEDED(hr)) {
+ impl_->comInitialized = true;
+ } else if (hr != RPC_E_CHANGED_MODE) {
+ return succeeded(hr, "CoInitializeEx(DirectShow webcam)");
+ }
+
+ if (directShowClsid.empty()) {
+ std::cerr << "ERROR: DirectShow webcam fallback requires a resolved filter CLSID" << std::endl;
+ return false;
+ }
+
+ CLSID selectedClsid{};
+ if (FAILED(CLSIDFromString(directShowClsid.c_str(), &selectedClsid))) {
+ std::cerr << "ERROR: DirectShow webcam fallback received an invalid filter CLSID" << std::endl;
+ return false;
+ }
+ selectedDeviceName_ = deviceName.empty() ? directShowClsid : deviceName;
+
+ // The camera's own format first, a forced RGB32 conversion only if we cannot
+ // read it.
+ //
+ // Order matters, and not for style. Leaving the grabber unconstrained lets
+ // intelligent connect hand through the source's native output, which is what
+ // every camera that works today relies on — asking for RGB32 up front made
+ // OBS Virtual Camera connect as RGB32 and then deliver no frames at all, a
+ // straight regression. But this class can only unpack YUY2, NV12 and RGB32,
+ // so a camera speaking anything else used to be rejected after the graph was
+ // already built, and the recording simply had no webcam. NVIDIA Broadcast is
+ // exactly that: absent from Media Foundation, so it can only arrive here, and
+ // it connects with a subtype this file cannot read
+ // (getopenscreen/openscreen#387). Naming a concrete subtype on the retry is
+ // what makes DirectShow insert a colour converter for it.
+ if (!buildGraph(selectedClsid, nullptr)) {
+ return false;
+ }
+ if (!resolveConnectedFormat(requestedWidth, requestedHeight, false)) {
+ std::cerr << "WARNING: DirectShow webcam speaks a format this build cannot unpack; "
+ "asking for RGB32 so the graph converts it"
+ << std::endl;
+ if (!buildGraph(selectedClsid, &MEDIASUBTYPE_RGB32)) {
+ return false;
+ }
+ if (!resolveConnectedFormat(requestedWidth, requestedHeight, true)) {
+ return false;
+ }
+ }
+
+ impl_->sampleGrabber->SetBufferSamples(TRUE);
+ impl_->sampleGrabber->SetOneShot(FALSE);
+ if (!succeeded(impl_->graph.As(&impl_->mediaControl), "QueryInterface(IMediaControl)")) {
return false;
}
+ return true;
+}
+
+bool DirectShowWebcamCapture::resolveConnectedFormat(
+ int requestedWidth,
+ int requestedHeight,
+ bool reportUnsupported) {
AM_MEDIA_TYPE connectedType{};
if (!succeeded(impl_->sampleGrabber->GetConnectedMediaType(&connectedType), "GetConnectedMediaType(DirectShow webcam)")) {
return false;
@@ -214,8 +270,13 @@ bool DirectShowWebcamCapture::initialize(
} else if (connectedType.subtype == MEDIASUBTYPE_RGB32) {
pixelFormat_ = PixelFormat::Bgra;
} else {
- std::cerr << "ERROR: Unsupported DirectShow webcam media subtype "
- << guidToString(connectedType.subtype) << std::endl;
+ // Silent on the first pass: the caller answers an unreadable format by
+ // rebuilding the graph with a conversion, and an ERROR line for a case
+ // that is about to be handled reads like a failure that never happened.
+ if (reportUnsupported) {
+ std::cerr << "ERROR: Unsupported DirectShow webcam media subtype "
+ << guidToString(connectedType.subtype) << std::endl;
+ }
freeMediaType(connectedType);
return false;
}
@@ -242,12 +303,6 @@ bool DirectShowWebcamCapture::initialize(
sourceStride_ = pixelFormat_ == PixelFormat::Bgra ? width_ * 4 : ((width_ + 3) / 4) * 4;
}
- impl_->sampleGrabber->SetBufferSamples(TRUE);
- impl_->sampleGrabber->SetOneShot(FALSE);
- if (!succeeded(impl_->graph.As(&impl_->mediaControl), "QueryInterface(IMediaControl)")) {
- return false;
- }
-
return true;
}
diff --git a/electron/native/wgc-capture/src/dshow_webcam_capture.h b/electron/native/wgc-capture/src/dshow_webcam_capture.h
index 3debcbe09..cf510e2df 100644
--- a/electron/native/wgc-capture/src/dshow_webcam_capture.h
+++ b/electron/native/wgc-capture/src/dshow_webcam_capture.h
@@ -50,6 +50,24 @@ private:
struct Impl;
void captureLoop();
+ /**
+ * Builds source -> sample grabber -> null renderer and connects it.
+ *
+ * `preferredSubtype` is what the grabber will accept: pass a concrete one
+ * (RGB32) to make DirectShow's intelligent connect insert a colour converter,
+ * or nullptr to accept whatever the camera offers natively. Returns false
+ * without leaving the graph half-built, so the caller can retry with a
+ * different constraint.
+ */
+ bool buildGraph(const CLSID& sourceClsid, const GUID* preferredSubtype);
+ /**
+ * Reads back what the graph actually negotiated and records how to unpack it.
+ *
+ * Returns false for a subtype this class cannot decode, which is a retryable
+ * outcome rather than a failure — `reportUnsupported` is what tells it apart
+ * from the last attempt, whose rejection is worth logging.
+ */
+ bool resolveConnectedFormat(int requestedWidth, int requestedHeight, bool reportUnsupported);
Impl* impl_ = nullptr;
std::thread thread_;