microsoft / Windows.UI.Composition-Win32-Samples

Windows.UI.Composition Win32 Samples
MIT License
471 stars 188 forks source link

GraphicsCapture Session::Close not responding #138

Open PanSnow opened 4 weeks ago

PanSnow commented 4 weeks ago

I am using Windows.Graphics.Capture to capture window, but sometimes I encounter the issue of GraphicsCapture Session::Close not responding. I always start window capture, stop, and call back data in the same thread. Can anyone give me some suggestions to resolve this issue?

Here is my sample code:

winrt::Windows::Graphics::Capture::GraphicsCaptureItem                              m_captureItem = {nullptr};
winrt::Windows::Graphics::SizeInt32                                                 m_contentSize;
winrt::Windows::Graphics::Capture::GraphicsCaptureSession                           m_session = {nullptr};
winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool                       m_framepool = {nullptr};
winrt::Windows::Graphics::Capture::GraphicsCaptureItem::Closed_revoker              m_closedtoken;
winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::FrameArrived_revoker m_frametoken;

BOOL CaptureRT::StartWindowCapture(HWND hWND)
{
    try
    {
        LOG(INFO) << StringPrintf("[CaptureRT] Start Window Capture (0x%08X)", hWND);
        HRESULT hRes = S_OK;
        if (m_captureApp.m_deviceRT != nullptr && hWND != nullptr)
        {
            auto interop = winrt::get_activation_factory<winrt::Windows::Graphics::Capture::GraphicsCaptureItem,
                                                         IGraphicsCaptureItemInterop>();
            hRes = interop->CreateForWindow(hWND, winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(),
                                            reinterpret_cast<void**>(winrt::put_abi(m_captureItem)));
            m_closedtoken = m_captureItem.Closed(winrt::auto_revoke, {this, &CaptureRT::OnClosed});
            m_contentSize = m_captureItem.Size();
            m_framepool = winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::Create(
                m_captureApp.m_deviceRT, winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized, 2,
                m_contentSize);
            m_session = m_framepool.CreateCaptureSession(m_captureItem);
            m_frametoken = m_framepool.FrameArrived(winrt::auto_revoke, {this, &CaptureRT::OnFrameArrived});
            m_session.StartCapture();
            LOG(INFO) << StringPrintf("[CaptureRT] StartCapture success");
            return TRUE;
        }
        return FALSE;
    }
    catch (...)
    {
        LOG(ERROR) << StringPrintf("Failed to Create(Unknown exception)");
    }
    return FALSE;
}

void CaptureRT::Stop()
{
    try
    {
        m_closedtoken.revoke();
        m_frametoken.revoke();
        if (m_framepool != nullptr)
            m_framepool.Close();
        m_framepool = nullptr;
        if (m_session != nullptr)
            m_session.Close();
        m_session = nullptr;
        m_contentSize.Width = m_contentSize.Height = 0;
    }
    catch (...)
    {
        LOG(ERROR) << "Failed to Destroy(Unknown exception)";
    }
}

The following is the dump and related call stack on anr thread issue.dmp

Image

robmikh commented 4 weeks ago

What build of Windows are you seeing this on? Could you also provide a dump of the capture service?

PanSnow commented 4 weeks ago

What build of Windows are you seeing this on? Could you also provide a dump of the capture service?

Windows System Version: 10.0 Build 19045 (revision: 4894; 64-bit)

My customer can reproduce this issue with a high probability with this windows system version.

I have a full dump, but can't upload it successfully due to the website's limited upload file size. Here is the mini dump for this issue: issue.dmp

PanSnow commented 3 weeks ago

@robmikh do you have any suggestions on this?

What build of Windows are you seeing this on? Could you also provide a dump of the capture service?

Windows System Version: 10.0 Build 19045 (revision: 4894; 64-bit)

My customer can reproduce this issue with a high probability with this windows system version.

I have a full dump, but can't upload it successfully due to the website's limited upload file size. Here is the mini dump for this issue: issue.dmp

robmikh commented 3 weeks ago

You'll need to create an issue in the Feedback Hub app, and then attach a dump to that feedback. Once you've done so, please tell me the title of the feedback and any ID it may show you.

However, could you also collect a dump of a service process? While the repro is ongoing:

That will let me determine why Close isn't responding. The stack you've shown says that Close is waiting for the service to respond.

PanSnow commented 3 weeks ago

You'll need to create an issue in the Feedback Hub app, and then attach a dump to that feedback. Once you've done so, please tell me the title of the feedback and any ID it may show you.

However, could you also collect a dump of a service process? While the repro is ongoing:

  • Open Task Manager
  • Got to the Services tab
  • Find a service with that starts with the name 'CaptureService'
  • Right-click the service and select "Go to details"
  • Right-click the selected svchost process and select "Create memory dump file"

That will let me determine why Close isn't responding. The stack you've shown says that Close is waiting for the service to respond.

Got it. I'll follow this approach next time I reproduce this issue from a user.

PanSnow commented 2 days ago

You'll need to create an issue in the Feedback Hub app, and then attach a dump to that feedback. Once you've done so, please tell me the title of the feedback and any ID it may show you.

However, could you also collect a dump of a service process? While the repro is ongoing:

  • Open Task Manager
  • Got to the Services tab
  • Find a service with that starts with the name 'CaptureService'
  • Right-click the service and select "Go to details"
  • Right-click the selected svchost process and select "Create memory dump file"

That will let me determine why Close isn't responding. The stack you've shown says that Close is waiting for the service to respond.

Hi @robmikh I created an issue in Feedback Hub with the title "GraphicsCapture Session::Close api not responding" and here is the share link: https://aka.ms/AAtjd66