#pragma once #include #include #include #include #include #include #include "d3d_context.hpp" #include "display_info.hpp" #include "dxgi_capturer.hpp" #include "media/base/adapted_video_track_source.h" #include "wgc_capturer.hpp" namespace linkshell { // Unified GPU-backed webrtc::VideoTrackSource for both full-screen capture (WGC -> DXGI -> GDI/SecureDesktop) // and Fusion single-window fixed-canvas capture (WGC HWND -> GPU top-left blit + SEI layout attachment). class ScreenVideoTrackSource : public webrtc::AdaptedVideoTrackSource { public: using LogFn = std::function; using StopFn = std::function; explicit ScreenVideoTrackSource(std::shared_ptr d3d, LogFn log = {}); ~ScreenVideoTrackSource() override; // VideoTrackSourceInterface overrides SourceState state() const override { return SourceState::kLive; } bool remote() const override { return false; } bool is_screencast() const override { return true; } std::optional needs_denoising() const override { return false; } // Full monitor capture (supports plugged-in, headless, virtual display, and UAC/Lock Screen handoff). bool StartDisplay(const DisplayInfo& display, int dst_w, int dst_h, int target_fps, std::string* error = nullptr); // Fusion single-window fixed-canvas capture (zero encoder restarts on window resize). bool StartWindow(HWND hwnd, int canvas_w, int canvas_h, int target_fps, std::string* error = nullptr); void SetFusionLayout(const std::string& layout_json); void SetTargetFps(int fps); void SetPaused(bool paused); void ResendLatest(); void Stop(); void SetOnStop(StopFn on_stop) { on_stop_ = std::move(on_stop); } uint64_t ConsumeCapturedFrames() { return captured_frames_.exchange(0); } std::string active_backend() const { std::lock_guard lock(state_mu_); return backend_name_; } private: void OnWgcFrame(ID3D11Texture2D* tex, int content_w, int content_h); void EmitFrame(webrtc::scoped_refptr buf, bool fresh_capture); void WorkerLoop(); std::shared_ptr d3d_; LogFn log_; StopFn on_stop_; mutable std::mutex state_mu_; bool window_mode_ = false; DisplayInfo display_{}; HWND hwnd_ = nullptr; int dst_w_ = 1920; int dst_h_ = 1080; std::atomic target_fps_{60}; std::atomic paused_{false}; std::atomic running_{false}; std::atomic captured_frames_{0}; std::atomic wgc_frames_{0}; std::atomic logged_first_frame_{false}; std::atomic logged_convert_err_{false}; std::string backend_name_ = "none"; std::string fusion_layout_; std::unique_ptr wgc_; std::thread worker_; std::mutex latest_mu_; webrtc::scoped_refptr latest_frame_; int64_t last_fresh_ms_ = 0; int64_t last_emit_us_ = 0; }; } // namespace linkshell