桌面复制(DirectX)屏幕捕获无法提供屏幕更新。

18
我正在开发一个应用程序,可以通过桌面复制API(使用DirectX 11)捕获屏幕的内容(仅捕获前一次屏幕更新后的差异),并在另一个窗口上呈现它(查看器可能在通过局域网连接的另一台计算机上运行)。该代码是MSDN提供的示例的改进版。除了设备有时不提供屏幕更新之外,一切都运行良好,这种情况大约会在某些机器(主要是Windows 8 / 8.1机器,很少在Windows 10机器上)中10%的时间发生。我尝试了解决此问题的所有可能方法。减少设备重置的次数,这提供了比较可靠的输出,但并非总是100%正常工作。

设备有时无法提供初始屏幕(全屏),这在所有支持桌面复制的Windows操作系统中发生的频率为60%。我想出了一个解决办法,它会重试从设备获取初始更新,直到它提供一个,但这也导致多个问题,设备甚至可能永远都不提供初始屏幕。

我已经花费了几周的时间来解决这个问题,但没有找到合适的解决方案,也没有我知道讨论这些问题的论坛。任何帮助都将不胜感激。

以下是我的代码,用于获取屏幕差异,初始化设备并填充适配器和监视器。

请耐心看完下面的非常长的代码片段,提前致谢。

获取屏幕更新的代码如下:

INT getChangedRegions(int timeout, rectangles &dirtyRects, std::vector <MOVE_RECT> &moveRects, UINT &rect_count, RECT ScreenRect)
{
UINT diffArea           = 0;
FRAME_DATA currentFrameData;

bool isTimeOut          = false;

TRY
{
    
    m_LastErrorCode = m_DuplicationManager.GetFrame(&currentFrameData, timeout, &isTimeOut);

    if(SUCCEEDED(m_LastErrorCode) && (!isTimeOut))
    {
        if(currentFrameData.FrameInfo.TotalMetadataBufferSize)
        {

            m_CurrentFrameTexture = currentFrameData.Frame;

            if(currentFrameData.MoveCount)
            {
                DXGI_OUTDUPL_MOVE_RECT* moveRectArray = reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*> (currentFrameData.MetaData);

                if (moveRectArray)
                {
                    for(UINT index = 0; index < currentFrameData.MoveCount; index++)
                    {
                        //WebRTC
                        // DirectX capturer API may randomly return unmoved move_rects, which should
                        // be skipped to avoid unnecessary wasting of differing and encoding
                        // resources.
                        // By using testing application it2me_standalone_host_main, this check
                        // reduces average capture time by 0.375% (4.07 -> 4.055), and average
                        // encode time by 0.313% (8.042 -> 8.016) without other impacts.

                        if (moveRectArray[index].SourcePoint.x != moveRectArray[index].DestinationRect.left || moveRectArray[index].SourcePoint.y != moveRectArray[index].DestinationRect.top) 
                        {

                            if(m_UseD3D11BitmapConversion)
                            {
                                MOVE_RECT moveRect;

                                moveRect.SourcePoint.x =  moveRectArray[index].SourcePoint.x * m_ImageScalingFactor;
                                moveRect.SourcePoint.y =  moveRectArray[index].SourcePoint.y * m_ImageScalingFactor;

                                moveRect.DestinationRect.left = moveRectArray[index].DestinationRect.left * m_ImageScalingFactor;
                                moveRect.DestinationRect.top = moveRectArray[index].DestinationRect.top * m_ImageScalingFactor;
                                moveRect.DestinationRect.bottom = moveRectArray[index].DestinationRect.bottom * m_ImageScalingFactor;
                                moveRect.DestinationRect.right = moveRectArray[index].DestinationRect.right * m_ImageScalingFactor;

                                moveRects.push_back(moveRect);
                                diffArea += abs((moveRect.DestinationRect.right - moveRect.DestinationRect.left) * 
                                        (moveRect.DestinationRect.bottom - moveRect.DestinationRect.top));
                            }
                            else
                            {
                                moveRects.push_back(moveRectArray[index]);
                                diffArea += abs((moveRectArray[index].DestinationRect.right - moveRectArray[index].DestinationRect.left) * 
                                        (moveRectArray[index].DestinationRect.bottom - moveRectArray[index].DestinationRect.top));
                            }
                        }
                    }
                }
                else
                {
                    return -1;
                }
            }

            if(currentFrameData.DirtyCount)
            {
                RECT* dirtyRectArray = reinterpret_cast<RECT*> (currentFrameData.MetaData + (currentFrameData.MoveCount * sizeof(DXGI_OUTDUPL_MOVE_RECT)));

                if (!dirtyRectArray)
                {
                    return -1;
                }

                rect_count = currentFrameData.DirtyCount;

                for(UINT index = 0; index < rect_count; index ++)
                {

                    if(m_UseD3D11BitmapConversion)
                    {
                        RECT dirtyRect;

                        dirtyRect.bottom = dirtyRectArray[index].bottom * m_ImageScalingFactor;
                        dirtyRect.top = dirtyRectArray[index].top * m_ImageScalingFactor;
                        dirtyRect.left = dirtyRectArray[index].left * m_ImageScalingFactor;
                        dirtyRect.right = dirtyRectArray[index].right * m_ImageScalingFactor;

                        diffArea += abs((dirtyRect.right - dirtyRect.left) * 
                        (dirtyRect.bottom - dirtyRect.top));

                        dirtyRects.push_back(dirtyRect);
                    }
                    else
                    {
                        diffArea += abs((dirtyRectArray[index].right - dirtyRectArray[index].left) * 
                        (dirtyRectArray[index].bottom - dirtyRectArray[index].top));

                        dirtyRects.push_back(dirtyRectArray[index]);
                    }
                }

            }

        }

    return diffArea;

}

CATCH_ALL(e)
{ 
    LOG(CRITICAL) << _T("Exception in getChangedRegions");
}
END_CATCH_ALL

return -1;
}

以下是初始化设备的代码

       //
    // Initialize duplication interfaces
    //
    HRESULT cDuplicationManager::InitDupl(_In_ ID3D11Device* Device, _In_ IDXGIAdapter *_pAdapter, _In_ IDXGIOutput *_pOutput, _In_ UINT Output)
    {
    HRESULT hr = E_FAIL;

    if(!_pOutput || !_pAdapter || !Device)
    {
        return hr;
    }

    m_OutputNumber = Output;
 
    // Take a reference on the device
    m_Device = Device;
    m_Device->AddRef();

    /*
    // Get DXGI device
    IDXGIDevice* DxgiDevice = nullptr;
    HRESULT hr = m_Device->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&DxgiDevice));
    if (FAILED(hr))
    {
        return ProcessFailure(nullptr, _T("Failed to QI for DXGI Device"), _T("Error"), hr);
    }
 
    // Get DXGI adapter
    IDXGIAdapter* DxgiAdapter = nullptr;
    hr = DxgiDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&DxgiAdapter));
    DxgiDevice->Release();
    DxgiDevice = nullptr;
    if (FAILED(hr))
    {
        return ProcessFailure(m_Device, _T("Failed to get parent DXGI Adapter"), _T("Error"), hr);//, SystemTransitionsExpectedErrors);
    }
 
    // Get output
    IDXGIOutput* DxgiOutput = nullptr;
    hr = DxgiAdapter->EnumOutputs(Output, &DxgiOutput);
    DxgiAdapter->Release();
    DxgiAdapter = nullptr;
    if (FAILED(hr))
    {
        return ProcessFailure(m_Device, _T("Failed to get specified output in DUPLICATIONMANAGER"), _T("Error"), hr);//, EnumOutputsExpectedErrors);
    }

    DxgiOutput->GetDesc(&m_OutputDesc);

     IDXGIOutput1* DxgiOutput1 = nullptr;
    hr = DxgiOutput->QueryInterface(__uuidof(DxgiOutput1), reinterpret_cast<void**>(&DxgiOutput1));

    */

    _pOutput->GetDesc(&m_OutputDesc);
     // QI for Output 1
    IDXGIOutput1* DxgiOutput1 = nullptr;
    hr = _pOutput->QueryInterface(__uuidof(DxgiOutput1), reinterpret_cast<void**>(&DxgiOutput1));

    if (FAILED(hr))
    {
        return ProcessFailure(nullptr, _T("Failed to QI for DxgiOutput1 in DUPLICATIONMANAGER"), _T("Error"), hr);
    }
 
    // Create desktop duplication
    hr = DxgiOutput1->DuplicateOutput(m_Device, &m_DeskDupl);

    DxgiOutput1->Release();
    DxgiOutput1 = nullptr;


    if (FAILED(hr) || !m_DeskDupl)
    {
        if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE)
        {
            return ProcessFailure(nullptr, _T("Maximum number of applications using Desktop Duplication API"), _T("Error"), hr);
        }
        return ProcessFailure(m_Device, _T("Failed to get duplicate output in DUPLICATIONMANAGER"), _T("Error"), hr);//, CreateDuplicationExpectedErrors);
    }
 
    return S_OK;
}

最后,获取当前帧并将其与上一帧进行比较:

   //
// Get next frame and write it into Data
//
_Success_(*Timeout == false && return == DUPL_RETURN_SUCCESS)
HRESULT cDuplicationManager::GetFrame(_Out_ FRAME_DATA* Data, int timeout, _Out_ bool* Timeout)
{
    IDXGIResource* DesktopResource = nullptr;
    DXGI_OUTDUPL_FRAME_INFO FrameInfo;
    
    try
    {
         // Get new frame
        HRESULT hr = m_DeskDupl->AcquireNextFrame(timeout, &FrameInfo, &DesktopResource);

        if (hr == DXGI_ERROR_WAIT_TIMEOUT)
        {
            *Timeout = true;
            return S_OK;
        }

        *Timeout = false;
 
        if (FAILED(hr))
        {
            return ProcessFailure(m_Device, _T("Failed to acquire next frame in DUPLICATIONMANAGER"), _T("Error"), hr);//, FrameInfoExpectedErrors);
        }
 
        // If still holding old frame, destroy it
        if (m_AcquiredDesktopImage)
        {
            m_AcquiredDesktopImage->Release();
            m_AcquiredDesktopImage = nullptr;
        }
 
        if (DesktopResource)
        {
            // QI for IDXGIResource
            hr = DesktopResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void **>(&m_AcquiredDesktopImage));
            DesktopResource->Release();
            DesktopResource = nullptr;
        }

        if (FAILED(hr))
        {
            return ProcessFailure(nullptr, _T("Failed to QI for ID3D11Texture2D from acquired IDXGIResource in DUPLICATIONMANAGER"), _T("Error"), hr);
        }
 
        // Get metadata
        if (FrameInfo.TotalMetadataBufferSize)
        {
            // Old buffer too small
            if (FrameInfo.TotalMetadataBufferSize > m_MetaDataSize)
            {
                if (m_MetaDataBuffer)
                {
                    delete [] m_MetaDataBuffer;
                    m_MetaDataBuffer = nullptr;
                }

                m_MetaDataBuffer = new (std::nothrow) BYTE[FrameInfo.TotalMetadataBufferSize];

                if (!m_MetaDataBuffer)
                {
                    m_MetaDataSize = 0;
                    Data->MoveCount = 0;
                    Data->DirtyCount = 0;
                    return ProcessFailure(nullptr, _T("Failed to allocate memory for metadata in DUPLICATIONMANAGER"), _T("Error"), E_OUTOFMEMORY);
                }

                m_MetaDataSize = FrameInfo.TotalMetadataBufferSize;
            }
 
            
            UINT BufSize = FrameInfo.TotalMetadataBufferSize;
 
            // Get move rectangles

        
            hr = m_DeskDupl->GetFrameMoveRects(BufSize, reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(m_MetaDataBuffer), &BufSize);

            if (FAILED(hr))
            {
                Data->MoveCount = 0;
                Data->DirtyCount = 0;
                return ProcessFailure(nullptr, L"Failed to get frame move rects in DUPLICATIONMANAGER", L"Error", hr);//, FrameInfoExpectedErrors);
            
            }
        
            Data->MoveCount = BufSize / sizeof(DXGI_OUTDUPL_MOVE_RECT);
 
            BYTE* DirtyRects = m_MetaDataBuffer + BufSize;
            BufSize = FrameInfo.TotalMetadataBufferSize - BufSize;
 
            // Get dirty rectangles
            hr = m_DeskDupl->GetFrameDirtyRects(BufSize, reinterpret_cast<RECT*>(DirtyRects), &BufSize);

            if (FAILED(hr))
            {
                Data->MoveCount = 0;
                Data->DirtyCount = 0;
                return ProcessFailure(nullptr, _T("Failed to get frame dirty rects in DUPLICATIONMANAGER"), _T("Error"), hr);//, FrameInfoExpectedErrors);
            }

            Data->DirtyCount = BufSize / sizeof(RECT);
 
            Data->MetaData = m_MetaDataBuffer;
        }
 
        Data->Frame = m_AcquiredDesktopImage;
        Data->FrameInfo = FrameInfo;

    }
    catch (...)
    {
        return S_FALSE;
    }

    return S_OK;
}

更新:

如果设备挂起(即在流式传输屏幕的过程中,例如连续捕获视频并将其发送到另一端),则会打印出“在DUPLICATIONMANAGER中获取下一帧失败”的错误信息。

// Get new frame
    HRESULT hr = m_DeskDupl->AcquireNextFrame(timeout, &FrameInfo, &DesktopResource);

    if (hr == DXGI_ERROR_WAIT_TIMEOUT)
    {
        *Timeout = true;
        return S_OK;
    }

    *Timeout = false;

    if (FAILED(hr))
    {
        return ProcessFailure(m_Device, _T("Failed to acquire next frame in DUPLICATIONMANAGER"), _T("Error"), hr);//, FrameInfoExpectedErrors);
    }

以下是详细的错误信息:

Id3d11DuplicationManager::ProcessFailure - 错误:在DUPLICATIONMANAGER中获取下一帧失败,详情:关键互斥锁已被放弃。

更新2: 每当设备无法永久提供屏幕更新时,我都会收到错误代码,以下是相同的错误:

Id3d11DuplicationManager::ProcessFailure - 错误:在DUPLICATIONMANAGER中获取重复输出失败,详情:拒绝访问。

错误代码为E_ACCESSDENIED。

我不明白为什么会出现这个错误,因为我已经处于SYSTEM模式,并且SetThreadDesktop已执行两次(一次在初始化后,另一次在检测到失败后)。

根据MSDN上的解释,如果应用程序没有对当前桌面图像的访问权限,则会出现E_ACCESSDENIED错误。例如,只有在LOCAL_SYSTEM运行的应用程序才能访问安全桌面。

是否还有其他原因会导致此类问题?


4
代码片段不太容易阅读,但有一个问题——而且是个严重的问题——立刻浮现出来:在获取新帧之后释放旧帧。这是不正确的:应用程序必须在获取下一帧之前释放当前帧。在帧被释放后,包含桌面位图的表面将变得无效;您将无法在DirectX图形操作中使用该表面。 - Roman R.
1
是的,@RomanR。这看起来是一个严重的问题,而且这是一个多余的检查。我会处理好它的。对不起我的代码很差,但我尽力只包含与初始化、捕获和释放图像帧相关的部分。 - iamrameshkumar
1
@RomanR。我有关于独立显卡和集成显卡的几个问题。我知道有一些论坛(https://dev59.com/Apffa4cB1Zd3GeqP5UIq)讨论了独立显卡上DirectX失败的问题。这可能是我的情况之一吗?但如果是这种情况,设备初始化本身不会返回失败吗? - iamrameshkumar
1个回答

1

如果出现不可恢复的错误,检查返回代码并立即回退到GDI或任何其他可用的屏幕捕获方法总是很好的选择。对于某些硬件错误(如达到最大限制、内存不足、设备已移除等),重试通常无效,我曾经吃过亏。此外,在极少数情况下,DirectX设备需要几次迭代才能产生初始帧。重试超过10次是没有用的,你可以安全地回退或尝试重新初始化设备以再次检查后再回退。

以下是一些基本检查:

处理DXGI_ERROR_NOT_CURRENTLY_AVAILABLE错误:

_pOutput->GetDesc(&m_OutputDesc);
// QI for Output 1
IDXGIOutput1* DxgiOutput1 = nullptr;
hr = _pOutput->QueryInterface(__uuidof(DxgiOutput1), reinterpret_cast<void**>(&DxgiOutput1));

if (FAILED(hr))
{
    return ProcessFailure(nullptr, _T("Failed to QI for DxgiOutput1 in DUPLICATIONMANAGER"), _T("Error"), hr);
}

// Create desktop duplication
hr = DxgiOutput1->DuplicateOutput(m_Device, &m_DeskDupl);

DxgiOutput1->Release();
DxgiOutput1 = nullptr;

if (FAILED(hr) || !m_DeskDupl)
{
    if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE)
    {
        return ProcessFailure(nullptr, _T("Maximum number of applications using Desktop Duplication API"), _T("Error"), hr);
    }

    return ProcessFailure(m_Device, _T("Failed to get duplicate output in DUPLICATIONMANAGER"), _T("Error"), hr);//, CreateDuplicationExpectedErrors);
}

检查设备移除(DXGI_ERROR_DEVICE_REMOVED)或设备重置(DXGI_ERROR_DEVICE_RESET)和内存不足(E_OUTOFMEMORY)错误代码(我有时会收到E_OUTOFMEMORY,尽管这很少见):
 HRESULT ProcessFailure(_In_opt_ ID3D11Device* Device, _In_ LPCWSTR Str, _In_ LPCWSTR Title, HRESULT hr)//, _In_opt_z_ HRESULT* ExpectedErrors = NULL)
 {
    HRESULT TranslatedHr;

// On an error check if the DX device is lost
  if (Device)
  {
    HRESULT DeviceRemovedReason = Device->GetDeviceRemovedReason();

    switch (DeviceRemovedReason)
    {
    case DXGI_ERROR_DEVICE_REMOVED:
    case DXGI_ERROR_DEVICE_RESET:
    case static_cast<HRESULT>(E_OUTOFMEMORY) :
    {
        // Our device has been stopped due to an external event on the GPU so map them all to
        // device removed and continue processing the condition
        TranslatedHr = DXGI_ERROR_DEVICE_REMOVED;
        break;
    }

    case S_OK:
    {
        // Device is not removed so use original error
        TranslatedHr = hr;
        break;
    }

    default:
    {
        // Device is removed but not a error we want to remap
        TranslatedHr = DeviceRemovedReason;
    }
    }
  }
  else
  {
    TranslatedHr = hr;
  }

_com_error err(TranslatedHr);
LPCTSTR errMsg = err.ErrorMessage();

return TranslatedHr;
}

此外,桌面复制需要真实的图形设备才能正常工作。否则,您可能会遇到E_ACCESSDENIED错误。
还有其他情况可能会出现此错误,例如桌面切换情况、键控互斥量被弃用等。在这种情况下,您可以尝试重新初始化设备。
我也上传了我的示例项目here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接