FFMPEG录屏(14)---- WGC 捕获桌面(二) Copy数据到CPU

该文章介绍了如何使用WebRTC进行屏幕捕获后,将捕获的窗口或桌面内容从GPU复制到内存中,以便进行后续处理如压缩。通过D3D11接口创建映射纹理,然后在帧到达时进行资源复制,并将数据保存为位图文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考资料

WEBRTC WGC CAPTURE

上一篇中我们已经成功改造官网Demo用以捕获显示器内容,现在我们支持了捕获窗口、桌面并实时的渲染在窗口中,下一步我们需要把捕获到的数据从GPU拷贝出来到我们的内存中用以压缩等等。

话不多说直接上代码

  • SimpleCapture.h 中添加成员变量 m_mappedTexture
winrt::com_ptr<ID3D11Texture2D> m_mappedTexture{nullptr};
  • SimpleCapture.h 中添加成员函数 CreateMappedTexture
HRESULT
CreateMappedTexture(winrt::com_ptr<ID3D11Texture2D> src_texture,
                    UINT width = 0, UINT height = 0);
  • SimpleCapture.cpp 中添加实现 CreateMappedTexture
HRESULT
SimpleCapture::CreateMappedTexture(winrt::com_ptr<ID3D11Texture2D> src_texture,
                                   UINT width, UINT height) {
  D3D11_TEXTURE2D_DESC src_desc;
  src_texture->GetDesc(&src_desc);
  D3D11_TEXTURE2D_DESC map_desc;
  map_desc.Width = width == 0 ? src_desc.Width : width;
  map_desc.Height = height == 0 ? src_desc.Height : height;
  map_desc.MipLevels = src_desc.MipLevels;
  map_desc.ArraySize = src_desc.ArraySize;
  map_desc.Format = src_desc.Format;
  map_desc.SampleDesc = src_desc.SampleDesc;
  map_desc.Usage = D3D11_USAGE_STAGING;
  map_desc.BindFlags = 0;
  map_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  map_desc.MiscFlags = 0;

  auto d3dDevice = GetDXGIInterfaceFromObject<ID3D11Device>(m_device);

  return d3dDevice->CreateTexture2D(&map_desc, nullptr, m_mappedTexture.put());
}
  • SimpleCapture.cpp 中 OnFrameArrived 函数中添加代码
// copy to mapped texture
{
  auto frameSurface =
      GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());

  if (!m_mappedTexture || newSize)
    CreateMappedTexture(frameSurface);

  m_d3dContext->CopyResource(m_mappedTexture.get(), frameSurface.get());

  D3D11_MAPPED_SUBRESOURCE mapInfo;
  m_d3dContext->Map(m_mappedTexture.get(), 0, D3D11_MAP_READ,
                    D3D11_MAP_FLAG_DO_NOT_WAIT, &mapInfo);
                    
  // copy data from mapInfo.pData
  
  m_d3dContext->Unmap(m_mappedTexture.get(), 0);
}
  • 保存为位图文件看看?
#if 1
if (mapInfo.pData) {
  static unsigned char *buffer = nullptr;
  if (buffer && newSize)
    delete[] buffer;

  if (!buffer)
    buffer = new unsigned char[frameContentSize.Width *
                                frameContentSize.Height * 4];

  int dstRowPitch = frameContentSize.Width * 4;
  for (int h = 0; h < frameContentSize.Height; h++) {
    memcpy_s(buffer + h * dstRowPitch, dstRowPitch,
              (BYTE *)mapInfo.pData + h * mapInfo.RowPitch,
              min(mapInfo.RowPitch, dstRowPitch));
  }

  BITMAPINFOHEADER bi;

  bi.biSize = sizeof(BITMAPINFOHEADER);
  bi.biWidth = frameContentSize.Width;
  bi.biHeight = frameContentSize.Height * (-1);
  bi.biPlanes = 1;
  bi.biBitCount = 32; // should get from system color bits
  bi.biCompression = BI_RGB;
  bi.biSizeImage = 0;
  bi.biXPelsPerMeter = 0;
  bi.biYPelsPerMeter = 0;
  bi.biClrUsed = 0;
  bi.biClrImportant = 0;

  BITMAPFILEHEADER bf;
  bf.bfType = 0x4d42;
  bf.bfReserved1 = 0;
  bf.bfReserved2 = 0;
  bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  bf.bfSize =
      bf.bfOffBits + frameContentSize.Width * frameContentSize.Height * 4;

  FILE *fp = nullptr;

  fopen_s(&fp, ".\\save.bmp", "wb+");

  fwrite(&bf, 1, sizeof(bf), fp);
  fwrite(&bi, 1, sizeof(bi), fp);
  fwrite(buffer, 1, frameContentSize.Width * frameContentSize.Height * 4,
          fp);

  fflush(fp);
  fclose(fp);
}
#endif

查看工程目录下保存的文件,至此我们已经完成了对WGC捕获方式的全部调研工作。

已完成Demo

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值