#include "wgc_capturer.hpp" #include #include #include #include #include #include #include #include #include #include #include #include "common.hpp" namespace linkshell { namespace WGC = ABI::Windows::Graphics::Capture; namespace WGD = ABI::Windows::Graphics::DirectX; namespace WGD3D = ABI::Windows::Graphics::DirectX::Direct3D11; namespace WG = ABI::Windows::Graphics; using Microsoft::WRL::Callback; using Microsoft::WRL::ComPtr; using Microsoft::WRL::Wrappers::HStringReference; namespace { using CreateD3D11DeviceFromDXGIFn = HRESULT(WINAPI*)(IDXGIDevice*, IInspectable**); CreateD3D11DeviceFromDXGIFn LoadCreateD3D11DeviceFn() { static CreateD3D11DeviceFromDXGIFn fn = []() -> CreateD3D11DeviceFromDXGIFn { HMODULE mod = LoadLibraryW(L"d3d11.dll"); if (!mod) return nullptr; return reinterpret_cast( GetProcAddress(mod, "CreateDirect3D11DeviceFromDXGIDevice")); }(); return fn; } ComPtr WrapD3D11Device(ID3D11Device* device) { auto fn = LoadCreateD3D11DeviceFn(); if (!fn || !device) return nullptr; ComPtr dxgi_device; if (FAILED(device->QueryInterface(IID_PPV_ARGS(&dxgi_device))) || !dxgi_device) return nullptr; ComPtr inspectable; if (FAILED(fn(dxgi_device.Get(), &inspectable)) || !inspectable) return nullptr; ComPtr winrt_device; inspectable.As(&winrt_device); return winrt_device; } template HRESULT ActivateFactory(const wchar_t* class_name, ComPtr& out) { HStringReference hstr(class_name, static_cast(wcslen(class_name))); return RoGetActivationFactory(hstr.Get(), IID_PPV_ARGS(out.ReleaseAndGetAddressOf())); } } // namespace struct WgcCapturer::Impl { std::mutex mu; ComPtr winrt_device; ComPtr item; ComPtr frame_pool; ComPtr session; EventRegistrationToken frame_token{}; EventRegistrationToken closed_token{}; bool has_frame_token = false; bool has_closed_token = false; WG::SizeInt32 pool_size{0, 0}; }; bool WgcCapturer::IsSupported() { if (!LoadCreateD3D11DeviceFn()) return false; ComPtr statics; if (FAILED(ActivateFactory(RuntimeClass_Windows_Graphics_Capture_GraphicsCaptureSession, statics)) || !statics) { return false; } boolean supported = FALSE; if (FAILED(statics->IsSupported(&supported))) return false; return supported != FALSE; } WgcCapturer::WgcCapturer(std::shared_ptr d3d, FrameCallback on_frame, ClosedCallback on_closed) : d3d_(std::move(d3d)), on_frame_(std::move(on_frame)), on_closed_(std::move(on_closed)) {} WgcCapturer::~WgcCapturer() { Stop(); } bool WgcCapturer::StartMonitor(HMONITOR hmonitor, std::string* error) { Stop(); if (!hmonitor) { if (error) *error = "invalid HMONITOR"; return false; } ComPtr interop; HRESULT hr = ActivateFactory(RuntimeClass_Windows_Graphics_Capture_GraphicsCaptureItem, interop); if (FAILED(hr) || !interop) { if (error) *error = "WGC GraphicsCaptureItem activation failed"; return false; } ComPtr item; hr = interop->CreateForMonitor(hmonitor, IID_PPV_ARGS(&item)); if (FAILED(hr) || !item) { if (error) *error = "WGC CreateForMonitor failed: " + std::to_string(hr); return false; } return StartWithItem(item.Get(), error); } bool WgcCapturer::StartWindow(HWND hwnd, std::string* error) { Stop(); if (!hwnd || !IsWindow(hwnd)) { if (error) *error = "invalid HWND"; return false; } ComPtr interop; HRESULT hr = ActivateFactory(RuntimeClass_Windows_Graphics_Capture_GraphicsCaptureItem, interop); if (FAILED(hr) || !interop) { if (error) *error = "WGC GraphicsCaptureItem activation failed"; return false; } ComPtr item; hr = interop->CreateForWindow(hwnd, IID_PPV_ARGS(&item)); if (FAILED(hr) || !item) { if (error) *error = "WGC CreateForWindow failed: " + std::to_string(hr); return false; } return StartWithItem(item.Get(), error); } bool WgcCapturer::StartWithItem(void* raw_item_unknown, std::string* error) { if (!d3d_ || !d3d_->device()) { if (error) *error = "missing D3D11 device"; return false; } auto impl = std::make_unique(); impl->winrt_device = WrapD3D11Device(d3d_->device()); if (!impl->winrt_device) { if (error) *error = "CreateDirect3D11DeviceFromDXGIDevice failed"; return false; } auto* item_ptr = reinterpret_cast(raw_item_unknown); impl->item = item_ptr; WG::SizeInt32 size{0, 0}; if (FAILED(impl->item->get_Size(&size)) || size.Width <= 0 || size.Height <= 0) { if (error) *error = "WGC item size is empty"; return false; } impl->pool_size = size; ComPtr pool_statics2; HRESULT hr = ActivateFactory(RuntimeClass_Windows_Graphics_Capture_Direct3D11CaptureFramePool, pool_statics2); if (FAILED(hr) || !pool_statics2) { if (error) *error = "WGC Direct3D11CaptureFramePoolStatics2 unavailable (requires Win10 1903+)"; return false; } hr = pool_statics2->CreateFreeThreaded( impl->winrt_device.Get(), WGD::DirectXPixelFormat_B8G8R8A8UIntNormalized, 2, size, &impl->frame_pool); if (FAILED(hr) || !impl->frame_pool) { if (error) *error = "WGC CreateFreeThreaded failed: " + std::to_string(hr); return false; } hr = impl->frame_pool->CreateCaptureSession(impl->item.Get(), &impl->session); if (FAILED(hr) || !impl->session) { if (error) *error = "WGC CreateCaptureSession failed: " + std::to_string(hr); return false; } // Hide system cursor in video track (LinkShell sends crisp cursor shape/position over DataChannels). ComPtr session2; if (SUCCEEDED(impl->session.As(&session2)) && session2) { session2->put_IsCursorCaptureEnabled(FALSE); } // Hide Win11 yellow capture border where supported. ComPtr session3; if (SUCCEEDED(impl->session.As(&session3)) && session3) { session3->put_IsBorderRequired(FALSE); } impl_ = std::move(impl); running_.store(true); using FrameArrivedHandler = ABI::Windows::Foundation::ITypedEventHandler; auto on_arrived = Callback( [this](WGC::IDirect3D11CaptureFramePool* sender, IInspectable*) -> HRESULT { if (!running_.load()) return S_OK; ComPtr frame; if (FAILED(sender->TryGetNextFrame(&frame)) || !frame) return S_OK; WG::SizeInt32 content_size{0, 0}; frame->get_ContentSize(&content_size); ComPtr surface; if (SUCCEEDED(frame->get_Surface(&surface)) && surface) { ComPtr access; if (SUCCEEDED(surface.As(&access)) && access) { ComPtr texture; if (SUCCEEDED(access->GetInterface(IID_PPV_ARGS(&texture))) && texture) { D3D11_TEXTURE2D_DESC desc{}; texture->GetDesc(&desc); int cw = content_size.Width > 0 ? std::min(content_size.Width, static_cast(desc.Width)) : static_cast(desc.Width); int ch = content_size.Height > 0 ? std::min(content_size.Height, static_cast(desc.Height)) : static_cast(desc.Height); if (cw > 0 && ch > 0 && on_frame_) { on_frame_(texture.Get(), cw, ch); } } } } // Recreate pool if monitor/window resized. if (content_size.Width > 0 && content_size.Height > 0 && impl_) { std::lock_guard lock(impl_->mu); if (running_.load() && impl_->frame_pool && impl_->winrt_device && (content_size.Width != impl_->pool_size.Width || content_size.Height != impl_->pool_size.Height)) { impl_->pool_size = content_size; impl_->frame_pool->Recreate( impl_->winrt_device.Get(), WGD::DirectXPixelFormat_B8G8R8A8UIntNormalized, 2, content_size); } } return S_OK; }); hr = impl_->frame_pool->add_FrameArrived(on_arrived.Get(), &impl_->frame_token); if (SUCCEEDED(hr)) impl_->has_frame_token = true; using ClosedHandler = ABI::Windows::Foundation::ITypedEventHandler; auto on_item_closed = Callback( [this](WGC::IGraphicsCaptureItem*, IInspectable*) -> HRESULT { if (running_.exchange(false) && on_closed_) { on_closed_("capture target closed"); } return S_OK; }); if (SUCCEEDED(impl_->item->add_Closed(on_item_closed.Get(), &impl_->closed_token))) { impl_->has_closed_token = true; } hr = impl_->session->StartCapture(); if (FAILED(hr)) { if (error) *error = "WGC StartCapture failed: " + std::to_string(hr); Stop(); return false; } return true; } void WgcCapturer::Stop() { running_.store(false); if (!impl_) return; auto impl = std::move(impl_); std::lock_guard lock(impl->mu); if (impl->frame_pool && impl->has_frame_token) { impl->frame_pool->remove_FrameArrived(impl->frame_token); impl->has_frame_token = false; } if (impl->item && impl->has_closed_token) { impl->item->remove_Closed(impl->closed_token); impl->has_closed_token = false; } ComPtr closable; if (impl->session && SUCCEEDED(impl->session.As(&closable)) && closable) closable->Close(); closable.Reset(); if (impl->frame_pool && SUCCEEDED(impl->frame_pool.As(&closable)) && closable) closable->Close(); impl->session.Reset(); impl->frame_pool.Reset(); impl->item.Reset(); impl->winrt_device.Reset(); } } // namespace linkshell