DxgiCapturer.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef DXGICAPTURER_H
  2. #define DXGICAPTURER_H
  3. #include "IVideoCapturer.h"
  4. #include "d3d/buffer_filler.h"
  5. #include "d3d/convert.h"
  6. #include <d3d11.h>
  7. #include <dxgi1_2.h>
  8. #include <mutex>
  9. namespace avrecorder {
  10. namespace video {
  11. class DxgiCapturerPrivate {
  12. public:
  13. DxgiCapturerPrivate() = default;
  14. ~DxgiCapturerPrivate() = default;
  15. bool Open(int left, int top, int width, int height);
  16. void Close();
  17. void CloseInternal(); // 内部清理方法,不使用锁
  18. AVFrame* GetFrame(bool drawCursor, int left, int top, int right, int bottom);
  19. bool _bInit = false;
  20. bool _isCaptureSuccess = false;
  21. ID3D11Device* _hDevice = nullptr;
  22. ID3D11DeviceContext* _hContext = nullptr;
  23. IDXGIOutputDuplication* _hDeskDupl = nullptr;
  24. IDXGISurface1* _hStagingSurf = nullptr;
  25. ID3D11Texture2D* _gdiImage = nullptr;
  26. D3D11_TEXTURE2D_DESC _desc{};
  27. bool _isAttached = false;
  28. AVFrame* _xrgbFrame = nullptr;
  29. AVFrame* _nv12Frame = nullptr;
  30. BufferFiller _xrgbBuffers;
  31. BufferFiller _nv12Buffers;
  32. D3dConverter _rgbToNv12;
  33. mutable std::mutex _deviceMutex; // D3D设备访问的线程同步
  34. private:
  35. void drawCursor(HDC hdc, int left, int top, int right, int bottom);
  36. };
  37. class DxgiCapturer : public IVideoCapturer {
  38. public:
  39. DxgiCapturer();
  40. ~DxgiCapturer() override;
  41. bool open(const CaptureTarget& target, int width, int height) override;
  42. void close() override;
  43. AVFrame* getFrame() override;
  44. void setDrawCursor(bool enable) override { m_drawCursor = enable; }
  45. private:
  46. DxgiCapturerPrivate* d;
  47. bool m_drawCursor = true;
  48. int m_left = 0, m_top = 0, m_width = 0, m_height = 0;
  49. int m_right = 0, m_bottom = 0;
  50. };
  51. } // namespace video
  52. } // namespace avrecorder
  53. #endif // DXGICAPTURER_H