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

参考资料

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

以下是使用 Win32 API 实现窗口抓屏的示例代码: ```c++ #include <windows.h> int main() { // 获取窗口句柄 HWND hWnd = FindWindow(NULL, TEXT("窗口标题")); // 获取窗口矩形 RECT rect; GetWindowRect(hWnd, &rect); // 计算窗口大小 int width = rect.right - rect.left; int height = rect.bottom - rect.top; // 创建设备上下文 HDC hdcScreen = GetDC(NULL); HDC hdcWindow = GetDC(hWnd); HDC hdcMem = CreateCompatibleDC(hdcScreen); HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, width, height); SelectObject(hdcMem, hBitmap); // 截取窗口图像 BitBlt(hdcMem, 0, 0, width, height, hdcWindow, 0, 0, SRCCOPY); // 保存图像到文件 BITMAPINFOHEADER bih; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = 0; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; BITMAPFILEHEADER bfh; bfh.bfType = 0x4d42; bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + width * height * 3; bfh.bfReserved1 = 0; bfh.bfReserved2 = 0; bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); HANDLE hFile = CreateFile(TEXT("screenshot.bmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); DWORD dwWritten; WriteFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwWritten, NULL); WriteFile(hFile, &bih, sizeof(BITMAPINFOHEADER), &dwWritten, NULL); BYTE* pBits = new BYTE[width * height * 3]; GetDIBits(hdcMem, hBitmap, 0, height, pBits, (BITMAPINFO*)&bih, DIB_RGB_COLORS); WriteFile(hFile, pBits, width * height * 3, &dwWritten, NULL); delete[] pBits; CloseHandle(hFile); // 释放资源 DeleteObject(hBitmap); DeleteDC(hdcMem); ReleaseDC(hWnd, hdcWindow); ReleaseDC(NULL, hdcScreen); return 0; } ``` 其中,`FindWindow` 函数用于获取窗口句柄,`GetWindowRect` 函数用于获取窗口矩形,`CreateCompatibleDC` 函数用于创建设备上下文,`CreateCompatibleBitmap` 函数用于创建位图,`SelectObject` 函数用于选择位图到设备上下文,`BitBlt` 函数用于截取窗口图像,`GetDIBits` 函数用于获取位图数据,`CreateFile` 函数用于创建文件,`WriteFile` 函数用于写入文件,`DeleteObject` 函数用于删除位图,`DeleteDC` 函数用于删除设备上下文,`ReleaseDC` 函数用于释放设备上下文。 请注意,该示例代码只能在 Windows 系统下编译和运行。如果需要在其他系统下进行窗口抓屏,需要使用相应的 API 实现。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值