#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include class ScreenEncoder { public: ScreenEncoder(); ~ScreenEncoder(); void Start(std::unique_ptr> sink); void Stop(); void ForceKeyframe(); private: void CaptureLoop(); bool InitD3D(); bool InitEncoder(UINT width, UINT height); bool CaptureFrame(std::vector& bgra, UINT& width, UINT& height); void EncodeFrame(const std::vector& bgra, UINT width, UINT height, bool keyframe); void BgraToNv12(const uint8_t* bgra, std::vector& nv12, UINT width, UINT height); void SendEvent(std::vector data); Microsoft::WRL::ComPtr d3d_dev_; Microsoft::WRL::ComPtr d3d_ctx_; Microsoft::WRL::ComPtr dupl_; Microsoft::WRL::ComPtr staging_; Microsoft::WRL::ComPtr encoder_; UINT enc_width_ = 0; UINT enc_height_ = 0; bool config_sent_ = false; LONGLONG sample_ts_ = 0; std::unique_ptr> sink_; std::mutex sink_mu_; std::atomic running_ {false}; std::atomic force_kf_ {false}; std::thread thread_; };