SimpleCapture.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include <chrono>
  3. #include "d3d/gen_frame.h"
  4. class SimpleCapture {
  5. public:
  6. SimpleCapture(
  7. winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice const& device,
  8. winrt::Windows::Graphics::Capture::GraphicsCaptureItem const& item,
  9. int width, int height);
  10. ~SimpleCapture() { Close(); }
  11. void StartCapture();
  12. winrt::Windows::UI::Composition::ICompositionSurface CreateSurface(
  13. winrt::Windows::UI::Composition::Compositor const& compositor);
  14. void SetDrawCursor(bool isDrawCursor) { m_session.IsCursorCaptureEnabled(isDrawCursor); }
  15. void Close();
  16. AVFrame* GetFrame() const noexcept { return m_pixType == NV12 ? m_nv12Frame : m_xrgbFrame; }
  17. private:
  18. void OnFrameArrived(
  19. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const& sender,
  20. winrt::Windows::Foundation::IInspectable const& args);
  21. void CheckClosed()
  22. {
  23. if (m_closed.load() == true) {
  24. throw winrt::hresult_error(RO_E_CLOSED);
  25. }
  26. }
  27. private:
  28. enum _PixType {
  29. NV12,
  30. RGB
  31. };
  32. winrt::Windows::Graphics::Capture::GraphicsCaptureItem m_item {nullptr};
  33. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool m_framePool {nullptr};
  34. winrt::Windows::Graphics::Capture::GraphicsCaptureSession m_session {nullptr};
  35. winrt::Windows::Graphics::SizeInt32 m_lastSize;
  36. winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice m_device {nullptr};
  37. winrt::com_ptr<IDXGISwapChain1> m_swapChain {nullptr};
  38. winrt::com_ptr<ID3D11DeviceContext> m_d3dContext {nullptr};
  39. std::atomic<bool> m_closed = false;
  40. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::FrameArrived_revoker m_frameArrived;
  41. AVFrame* m_xrgbFrame = nullptr;
  42. AVFrame* m_nv12Frame = nullptr;
  43. BufferFiller m_xrgbBuffers;
  44. BufferFiller m_nv12Buffers;
  45. D3dConverter m_rgbToNv12;
  46. _PixType m_pixType;
  47. bool m_isCapture = true;
  48. int m_cnt = 5;
  49. };