4

我正在尝试使用 Windows桌面复制 API来捕获屏幕并将原始输出保存到视频中。我正在使用具有非常高的超时值(999 毫秒)的AcquireNextFrame 。这样一来,我就应该从 windows 中获取每个新帧,不管怎样,它自然应该是 60fps。我最终得到一切看起来都不错的序列(第 6-11 帧),然后是看起来很糟糕的序列(第 12-14 帧)。如果我检查AccumulatedFrames

lFrameInfo.AccumulatedFrames

该值通常为 2 或更高。据我了解,这意味着 Windows 正在说“嘿,等等,我还没有给你的框架”,因为对 AcquireNextFrame 的调用需要很长时间。但是一旦windows最终给了我一个框架,它就会说“嘿,你实际上太慢了,最终错过了一个框架”。如果我能以某种方式得到这些帧,我想我会得到 60hz。

这可以通过日志进一步澄清:

I0608 10:40:16.964375  4196 window_capturer_dd.cc:438] 206 - Frame 6 start acquire
I0608 10:40:16.973867  4196 window_capturer_dd.cc:451] 216 - Frame 6 acquired
I0608 10:40:16.981364  4196 window_capturer_dd.cc:438] 223 - Frame 7 start acquire
I0608 10:40:16.990864  4196 window_capturer_dd.cc:451] 233 - Frame 7 acquired
I0608 10:40:16.998364  4196 window_capturer_dd.cc:438] 240 - Frame 8 start acquire
I0608 10:40:17.007876  4196 window_capturer_dd.cc:451] 250 - Frame 8 acquired
I0608 10:40:17.015393  4196 window_capturer_dd.cc:438] 257 - Frame 9 start acquire
I0608 10:40:17.023905  4196 window_capturer_dd.cc:451] 266 - Frame 9 acquired
I0608 10:40:17.032411  4196 window_capturer_dd.cc:438] 274 - Frame 10 start acquire
I0608 10:40:17.039912  4196 window_capturer_dd.cc:451] 282 - Frame 10 acquired
I0608 10:40:17.048925  4196 window_capturer_dd.cc:438] 291 - Frame 11 start acquire
I0608 10:40:17.058428  4196 window_capturer_dd.cc:451] 300 - Frame 11 acquired
I0608 10:40:17.065943  4196 window_capturer_dd.cc:438] 308 - Frame 12 start acquire
I0608 10:40:17.096945  4196 window_capturer_dd.cc:451] 336 - Frame 12 acquired
I0608 10:40:17.098947  4196 window_capturer_dd.cc:464] 1 FRAMES MISSED on frame: 12
I0608 10:40:17.101444  4196 window_capturer_dd.cc:438] 343 - Frame 13 start acquire
I0608 10:40:17.128958  4196 window_capturer_dd.cc:451] 368 - Frame 13 acquired
I0608 10:40:17.130957  4196 window_capturer_dd.cc:464] 1 FRAMES MISSED on frame: 13
I0608 10:40:17.135459  4196 window_capturer_dd.cc:438] 377 - Frame 14 start acquire
I0608 10:40:17.160959  4196 window_capturer_dd.cc:451] 399 - Frame 14 acquired
I0608 10:40:17.162958  4196 window_capturer_dd.cc:464] 1 FRAMES MISSED on frame: 14

第 6-11 帧看起来不错,采集间隔大约 17 毫秒。第 12 帧应在 (300+17=317ms) 获取。第 12 帧在 308 开始等待,但直到 336 毫秒才得到任何东西。在 (300+17+17~=336ms) 之后的帧之前,Windows 对我没有任何帮助。好吧,也许 windows 只是错过了一个帧,但是当我终于得到它时,我可以检查 AccumulatedFrames 并且它的值为 2(这意味着我错过了一个帧,因为我在调用 AcquireNextFrame 之前等待了太长时间)。据我了解,如果 AcquireNextFrame 立即返回,则 AccumulatedFrames 大于 1 才有意义。

此外,我可以在我的捕获软件运行时使用 PresentMon。日志显示每一帧的 MsBetweenDisplayChange,相当稳定在 16.666 毫秒(有几个异常值,但比我的捕获软件看到的要少得多)。

这些人(12)似乎已经能够获得 60fps,所以我想知道我做错了什么。

我的代码基于

int main() {
    int FPS = 60;
    int video_length_sec = 5;

    int total_frames = FPS * video_length_sec;
    for (int i = 0; i < total_frames; i++) {
        if(!CaptureSingleFrame()){
            i--;
        }
    }
}

ComPtr<ID3D11Device> lDevice;
ComPtr<ID3D11DeviceContext> lImmediateContext;
ComPtr<IDXGIOutputDuplication> lDeskDupl;
ComPtr<ID3D11Texture2D> lAcquiredDesktopImage;
ComPtr<ID3D11Texture2D> lGDIImage;
ComPtr<ID3D11Texture2D> lDestImage;
DXGI_OUTPUT_DESC lOutputDesc;
DXGI_OUTDUPL_DESC lOutputDuplDesc;
D3D11_TEXTURE2D_DESC desc;

// Driver types supported
D3D_DRIVER_TYPE gDriverTypes[] = {
    D3D_DRIVER_TYPE_HARDWARE
};
UINT gNumDriverTypes = ARRAYSIZE(gDriverTypes);

// Feature levels supported
D3D_FEATURE_LEVEL gFeatureLevels[] = {
    D3D_FEATURE_LEVEL_11_0,
    D3D_FEATURE_LEVEL_10_1,
    D3D_FEATURE_LEVEL_10_0,
    D3D_FEATURE_LEVEL_9_1
};
UINT gNumFeatureLevels = ARRAYSIZE(gFeatureLevels);


bool Init() {
    int lresult(-1);

    D3D_FEATURE_LEVEL lFeatureLevel;

    HRESULT hr(E_FAIL);

    // Create device
    for (UINT DriverTypeIndex = 0; DriverTypeIndex < gNumDriverTypes; ++DriverTypeIndex)
    {
        hr = D3D11CreateDevice(
            nullptr,
            gDriverTypes[DriverTypeIndex],
            nullptr,
            0,
            gFeatureLevels,
            gNumFeatureLevels,
            D3D11_SDK_VERSION,
            &lDevice,
            &lFeatureLevel,
            &lImmediateContext);

        if (SUCCEEDED(hr))
        {
            // Device creation success, no need to loop anymore
            break;
        }

        lDevice.Reset();

        lImmediateContext.Reset();
    }

    if (FAILED(hr))
        return false;

    if (lDevice == nullptr)
        return false;

    // Get DXGI device
    ComPtr<IDXGIDevice> lDxgiDevice;
    hr = lDevice.As(&lDxgiDevice);

    if (FAILED(hr))
        return false;

    // Get DXGI adapter
    ComPtr<IDXGIAdapter> lDxgiAdapter;
    hr = lDxgiDevice->GetParent(
        __uuidof(IDXGIAdapter), &lDxgiAdapter);

    if (FAILED(hr))
        return false;

    lDxgiDevice.Reset();

    UINT Output = 0;

    // Get output
    ComPtr<IDXGIOutput> lDxgiOutput;
    hr = lDxgiAdapter->EnumOutputs(
        Output,
        &lDxgiOutput);

    if (FAILED(hr))
        return false;

    lDxgiAdapter.Reset();

    hr = lDxgiOutput->GetDesc(
        &lOutputDesc);

    if (FAILED(hr))
        return false;

    // QI for Output 1
    ComPtr<IDXGIOutput1> lDxgiOutput1;
    hr = lDxgiOutput.As(&lDxgiOutput1);

    if (FAILED(hr))
        return false;

    lDxgiOutput.Reset();

    // Create desktop duplication
    hr = lDxgiOutput1->DuplicateOutput(
        lDevice.Get(), //TODO what im i doing here
        &lDeskDupl);

    if (FAILED(hr))
        return false;

    lDxgiOutput1.Reset();

    // Create GUI drawing texture
    lDeskDupl->GetDesc(&lOutputDuplDesc);
    desc.Width = lOutputDuplDesc.ModeDesc.Width;
    desc.Height = lOutputDuplDesc.ModeDesc.Height;
    desc.Format = lOutputDuplDesc.ModeDesc.Format;
    desc.ArraySize = 1;
    desc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET;
    desc.MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.MipLevels = 1;
    desc.CPUAccessFlags = 0;
    desc.Usage = D3D11_USAGE_DEFAULT;


    hr = lDevice->CreateTexture2D(&desc, NULL, &lGDIImage);

    if (FAILED(hr))
        return false;

    if (lGDIImage == nullptr)
        return false;

    // Create CPU access texture
    desc.Width = lOutputDuplDesc.ModeDesc.Width;
    desc.Height = lOutputDuplDesc.ModeDesc.Height;
    desc.Format = lOutputDuplDesc.ModeDesc.Format;
    std::cout << desc.Width << "x" << desc.Height << "\n\n\n";
    desc.ArraySize = 1;
    desc.BindFlags = 0;
    desc.MiscFlags = 0;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.MipLevels = 1;
    desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
    desc.Usage = D3D11_USAGE_STAGING;

    return true;
}

void WriteFrameToCaptureFile(ID3D11Texture2D* texture) {

    D3D11_MAPPED_SUBRESOURCE* pRes = new D3D11_MAPPED_SUBRESOURCE;
    UINT subresource = D3D11CalcSubresource(0, 0, 0);

    lImmediateContext->Map(texture, subresource, D3D11_MAP_READ_WRITE, 0, pRes);

    void* d = pRes->pData;
    char* data = reinterpret_cast<char*>(d);

    // writes data to file
    WriteFrameToCaptureFile(data, 0);
}

bool CaptureSingleFrame()
{
    HRESULT hr(E_FAIL);
    ComPtr<IDXGIResource> lDesktopResource = nullptr;
    DXGI_OUTDUPL_FRAME_INFO lFrameInfo;
    ID3D11Texture2D* currTexture;

    hr = lDeskDupl->AcquireNextFrame(
        999,
        &lFrameInfo,
        &lDesktopResource);

    if (FAILED(hr)) {
        LOG(INFO) << "Failed to acquire new frame";
        return false;
    }

    if (lFrameInfo.LastPresentTime.HighPart == 0) {
        // not interested in just mouse updates, which can happen much faster than 60fps if you really shake the mouse
        hr = lDeskDupl->ReleaseFrame();
        return false;
    }

    int accum_frames = lFrameInfo.AccumulatedFrames;
    if (accum_frames > 1 && current_frame != 1) {
        // TOO MANY OF THESE is the problem
        // especially after having to wait >17ms in AcquireNextFrame()
    }

    // QI for ID3D11Texture2D
    hr = lDesktopResource.As(&lAcquiredDesktopImage);

    // Copy image into a newly created CPU access texture
    hr = lDevice->CreateTexture2D(&desc, NULL, &currTexture);
    if (FAILED(hr))
        return false;
    if (currTexture == nullptr)
        return false;

    lImmediateContext->CopyResource(currTexture, lAcquiredDesktopImage.Get());


    writer_thread->Schedule(
        FROM_HERE, [this, currTexture]() {
        WriteFrameToCaptureFile(currTexture);
    });
    pending_write_counts_++;

    hr = lDeskDupl->ReleaseFrame();

    return true;
}

**编辑 - 根据我的测量,您必须在帧实际出现约 10 毫秒之前调用 AcquireNextFrame(),否则 Windows 将无法获取它并让您获得下一个。每次我的录制程序需要超过 7 毫秒的时间来回绕(在获取第 i 帧之后直到在 i+1 上调用 AcquireNextFrame()),都会错过第 i+1 帧。

***编辑 -这是GPU视图的屏幕截图,显示了我在说什么。前 6 帧很快处理,然后第 7 帧需要 119 毫秒。“capture_to_argb.exe”旁边的长矩形对应于我被困在 AcquireNextFrame() 中。如果您查看硬件队列,您可以看到它以 60fps 清晰地呈现,即使我被困在 AcquireNextFrame() 中。至少这是我的解释(我不知道我在做什么)。

4

3 回答 3

2

“当前显示模式:3840 x 2160(32 位)(60hz)”是指显示刷新率,即每秒可以通过多少帧来显示。然而,新帧的渲染速率通常要低得多。您可以使用PresentMon或类似实用程序检查此速率。当我不移动鼠标时,它会向我报告如下内容:

本报告

正如您所看到的,当什么都没有发生时,Windows 每秒只显示两次新帧,甚至更慢。但是,这通常对视频编码非常有用,因为即使您以 60 fps 录制视频并AcquireNextFrame报告没有可用的新帧,这也意味着当前帧与之前的帧完全相同。

于 2017-06-07T12:14:17.040 回答
1

在下次呼叫AcquireNextFrame您之前进行阻塞等待会丢失实际帧。桌面复制 API 逻辑建议您尝试立即获取下一帧,如果您期望一个不错的帧速率。您的睡眠调用有效地放弃了可用的剩余执行超时时间,而不是硬保证您会在预定的时间间隔内获得一个新切片。

您必须以最大帧速率进行轮询。不要睡眠(即使睡眠时间为零)并立即请求下一帧。您可以选择丢弃过早出现的帧。桌面复制 API 的设计方式是获取额外的帧可能不会太昂贵,因为您可以尽早识别它们并停止它们的处理。

如果您仍然喜欢在帧之间睡觉,您可能需要阅读准确度备注

要提高睡眠间隔的准确性,请调用该timeGetDevCaps函数来确定支持的最小计时器分辨率,并调用 timeBeginPeriod 函数将计时器分辨率设置为最小值。调用 timeBeginPeriod 时要小心,因为频繁调用会显着影响系统时钟、系统电源使用和调度程序。如果您调用 timeBeginPeriod,请在应用程序的早期调用一次,并确保在应用程序的最后调用 timeEndPeriod 函数。

于 2017-06-08T20:17:33.837 回答
0

正如其他人所提到的,60Hz 刷新率仅表示显示可能更改的频率。这实际上并不意味着它经常改变。AcquireNextFrame 只会在重复输出上显示的内容发生更改时返回一个帧。

我的建议是...

  1. 使用所需的视频帧间隔创建Timer Queue 计时器
  2. 创建用于缓冲桌面位图的兼容资源
  3. 当计时器关闭时,以零超时调用 AcquireNextFrame
  4. 如果有更改,请将返回的资源复制到缓冲区并释放它
  5. 将缓冲的帧发送到编码器或任何进一步处理

这将以所需的速率产生一系列帧。如果显示没有改变,您将获得前一帧的副本以用于维持帧速率。

于 2017-08-31T15:39:20.060 回答