title: Desktop duplication (DirectX) screen capture fails to deliver screen updates
body: <p>I'm working on an application that would capture the screen through Desktop duplication APIs (using DirectX 11) (only the diff to the previous screen update) and render it on another window (The viewer might be running on another machine connected via LAN). The code is an improved version of the <a href="https://code.msdn.microsoft.com/windowsdesktop/Desktop-Duplication-Sample-da4c696a" rel="noreferrer">sample</a> provided in MSDN. Everything works fine except the device did not give any screen update though there is one some times in the mid, that happens around 10% of the time on some machines (mostly on windows 8/8.1 machines and rarely on windows 10 machines). I tried all the possible ways to sort out this problem. Reduced the number of device resets, that provided me some what reliable output but not always work fine for 100%.</p>
  <p>The device fails to provide an initial screen (a full screen) some times (This happens 60% of the time on all windows operating systems where Desktop duplication is supported), I came up with a work around that retried for an initial update from the device until it provides one but that too resulted in multiple issues, the device might not even give the initial screen ever.</p>
  <p>I have already invested weeks of my efforts to fix the problem but did not figure out a proper solution and there are no forums I know that discusses these kind of issues. Any help would be appreciated.</p>
  <p>Below is my code to get the screen diff to the previous one, init the device, populating the adapters and monitors.</p>
  <p>Please bear with me for a very long code snippet, Thanks in advance.</p>
  <p><strong>To Get the screen update:</strong></p>
  <pre><code>INT getChangedRegions(int timeout, rectangles &amp;dirtyRects, std::vector &lt;MOVE_RECT&gt; &amp;moveRects, UINT &amp;rect_count, RECT ScreenRect)
  {
  UINT diffArea           = 0;
  FRAME_DATA currentFrameData;
  
  bool isTimeOut          = false;
  
  TRY
  {
      
      m_LastErrorCode = m_DuplicationManager.GetFrame(&amp;currentFrameData, timeout, &amp;isTimeOut);
  
      if(SUCCEEDED(m_LastErrorCode) &amp;&amp; (!isTimeOut))
      {
          if(currentFrameData.FrameInfo.TotalMetadataBufferSize)
          {
  
              m_CurrentFrameTexture = currentFrameData.Frame;
  
              if(currentFrameData.MoveCount)
              {
                  DXGI_OUTDUPL_MOVE_RECT* moveRectArray = reinterpret_cast&lt;DXGI_OUTDUPL_MOVE_RECT*&gt; (currentFrameData.MetaData);
  
                  if (moveRectArray)
                  {
                      for(UINT index = 0; index &lt; 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 -&gt; 4.055), and average
                          // encode time by 0.313% (8.042 -&gt; 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&lt;RECT*&gt; (currentFrameData.MetaData + (currentFrameData.MoveCount * sizeof(DXGI_OUTDUPL_MOVE_RECT)));
  
                  if (!dirtyRectArray)
                  {
                      return -1;
                  }
  
                  rect_count = currentFrameData.DirtyCount;
  
                  for(UINT index = 0; index &lt; 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) &lt;&lt; _T(&quot;Exception in getChangedRegions&quot;);
  }
  END_CATCH_ALL
  
  return -1;
  }
  </code></pre>
  <p><strong>Here is the code to init the device</strong></p>
  <pre><code>       //
      // 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-&gt;AddRef();
  
      /*
      // Get DXGI device
      IDXGIDevice* DxgiDevice = nullptr;
      HRESULT hr = m_Device-&gt;QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast&lt;void**&gt;(&amp;DxgiDevice));
      if (FAILED(hr))
      {
          return ProcessFailure(nullptr, _T(&quot;Failed to QI for DXGI Device&quot;), _T(&quot;Error&quot;), hr);
      }
   
      // Get DXGI adapter
      IDXGIAdapter* DxgiAdapter = nullptr;
      hr = DxgiDevice-&gt;GetParent(__uuidof(IDXGIAdapter), reinterpret_cast&lt;void**&gt;(&amp;DxgiAdapter));
      DxgiDevice-&gt;Release();
      DxgiDevice = nullptr;
      if (FAILED(hr))
      {
          return ProcessFailure(m_Device, _T(&quot;Failed to get parent DXGI Adapter&quot;), _T(&quot;Error&quot;), hr);//, SystemTransitionsExpectedErrors);
      }
   
      // Get output
      IDXGIOutput* DxgiOutput = nullptr;
      hr = DxgiAdapter-&gt;EnumOutputs(Output, &amp;DxgiOutput);
      DxgiAdapter-&gt;Release();
      DxgiAdapter = nullptr;
      if (FAILED(hr))
      {
          return ProcessFailure(m_Device, _T(&quot;Failed to get specified output in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), hr);//, EnumOutputsExpectedErrors);
      }
  
      DxgiOutput-&gt;GetDesc(&amp;m_OutputDesc);
  
       IDXGIOutput1* DxgiOutput1 = nullptr;
      hr = DxgiOutput-&gt;QueryInterface(__uuidof(DxgiOutput1), reinterpret_cast&lt;void**&gt;(&amp;DxgiOutput1));
  
      */
  
      _pOutput-&gt;GetDesc(&amp;m_OutputDesc);
       // QI for Output 1
      IDXGIOutput1* DxgiOutput1 = nullptr;
      hr = _pOutput-&gt;QueryInterface(__uuidof(DxgiOutput1), reinterpret_cast&lt;void**&gt;(&amp;DxgiOutput1));
  
      if (FAILED(hr))
      {
          return ProcessFailure(nullptr, _T(&quot;Failed to QI for DxgiOutput1 in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), hr);
      }
   
      // Create desktop duplication
      hr = DxgiOutput1-&gt;DuplicateOutput(m_Device, &amp;m_DeskDupl);
  
      DxgiOutput1-&gt;Release();
      DxgiOutput1 = nullptr;
  
  
      if (FAILED(hr) || !m_DeskDupl)
      {
          if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE)
          {
              return ProcessFailure(nullptr, _T(&quot;Maximum number of applications using Desktop Duplication API&quot;), _T(&quot;Error&quot;), hr);
          }
          return ProcessFailure(m_Device, _T(&quot;Failed to get duplicate output in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), hr);//, CreateDuplicationExpectedErrors);
      }
   
      return S_OK;
  }
  </code></pre>
  <p><strong>Finally to get the current frame and difference to the previous one:</strong></p>
  <pre><code>   //
  // Get next frame and write it into Data
  //
  _Success_(*Timeout == false &amp;&amp; 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-&gt;AcquireNextFrame(timeout, &amp;FrameInfo, &amp;DesktopResource);
  
          if (hr == DXGI_ERROR_WAIT_TIMEOUT)
          {
              *Timeout = true;
              return S_OK;
          }
  
          *Timeout = false;
   
          if (FAILED(hr))
          {
              return ProcessFailure(m_Device, _T(&quot;Failed to acquire next frame in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), hr);//, FrameInfoExpectedErrors);
          }
   
          // If still holding old frame, destroy it
          if (m_AcquiredDesktopImage)
          {
              m_AcquiredDesktopImage-&gt;Release();
              m_AcquiredDesktopImage = nullptr;
          }
   
          if (DesktopResource)
          {
              // QI for IDXGIResource
              hr = DesktopResource-&gt;QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast&lt;void **&gt;(&amp;m_AcquiredDesktopImage));
              DesktopResource-&gt;Release();
              DesktopResource = nullptr;
          }
  
          if (FAILED(hr))
          {
              return ProcessFailure(nullptr, _T(&quot;Failed to QI for ID3D11Texture2D from acquired IDXGIResource in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), hr);
          }
   
          // Get metadata
          if (FrameInfo.TotalMetadataBufferSize)
          {
              // Old buffer too small
              if (FrameInfo.TotalMetadataBufferSize &gt; 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-&gt;MoveCount = 0;
                      Data-&gt;DirtyCount = 0;
                      return ProcessFailure(nullptr, _T(&quot;Failed to allocate memory for metadata in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), E_OUTOFMEMORY);
                  }
  
                  m_MetaDataSize = FrameInfo.TotalMetadataBufferSize;
              }
   
              
              UINT BufSize = FrameInfo.TotalMetadataBufferSize;
   
              // Get move rectangles
  
          
              hr = m_DeskDupl-&gt;GetFrameMoveRects(BufSize, reinterpret_cast&lt;DXGI_OUTDUPL_MOVE_RECT*&gt;(m_MetaDataBuffer), &amp;BufSize);
  
              if (FAILED(hr))
              {
                  Data-&gt;MoveCount = 0;
                  Data-&gt;DirtyCount = 0;
                  return ProcessFailure(nullptr, L&quot;Failed to get frame move rects in DUPLICATIONMANAGER&quot;, L&quot;Error&quot;, hr);//, FrameInfoExpectedErrors);
              
              }
          
              Data-&gt;MoveCount = BufSize / sizeof(DXGI_OUTDUPL_MOVE_RECT);
   
              BYTE* DirtyRects = m_MetaDataBuffer + BufSize;
              BufSize = FrameInfo.TotalMetadataBufferSize - BufSize;
   
              // Get dirty rectangles
              hr = m_DeskDupl-&gt;GetFrameDirtyRects(BufSize, reinterpret_cast&lt;RECT*&gt;(DirtyRects), &amp;BufSize);
  
              if (FAILED(hr))
              {
                  Data-&gt;MoveCount = 0;
                  Data-&gt;DirtyCount = 0;
                  return ProcessFailure(nullptr, _T(&quot;Failed to get frame dirty rects in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), hr);//, FrameInfoExpectedErrors);
              }
  
              Data-&gt;DirtyCount = BufSize / sizeof(RECT);
   
              Data-&gt;MetaData = m_MetaDataBuffer;
          }
   
          Data-&gt;Frame = m_AcquiredDesktopImage;
          Data-&gt;FrameInfo = FrameInfo;
  
      }
      catch (...)
      {
          return S_FALSE;
      }
  
      return S_OK;
  }
  </code></pre>
  <p><strong>Update :</strong></p>
  <blockquote>
  <p>Failed to acquire next frame in DUPLICATIONMANAGER is getting printed whenever the device has hung (That is in the mid of streaming the screens, Ex: Continuously capturing a video and sending it to the other end)</p>
  </blockquote>
  <pre><code>// Get new frame
      HRESULT hr = m_DeskDupl-&gt;AcquireNextFrame(timeout, &amp;FrameInfo, &amp;DesktopResource);
  
      if (hr == DXGI_ERROR_WAIT_TIMEOUT)
      {
          *Timeout = true;
          return S_OK;
      }
  
      *Timeout = false;
  
      if (FAILED(hr))
      {
          return ProcessFailure(m_Device, _T(&quot;Failed to acquire next frame in DUPLICATIONMANAGER&quot;), _T(&quot;Error&quot;), hr);//, FrameInfoExpectedErrors);
      }
  </code></pre>
  <p>here is the detailed error info :</p>
  <blockquote>
  <p>Id3d11DuplicationManager::ProcessFailure - Error: Failed to acquire next frame in DUPLICATIONMANAGER, Detail: The keyed mutex was abandoned.</p>
  </blockquote>
  <p><strong>Update 2 :</strong>
  I have got the error code whenever the device failed to give screen updates forever, And here is the same</p>
  <p>Id3d11DuplicationManager::ProcessFailure - Error: Failed to get duplicate output in DUPLICATIONMANAGER, Detail: Access is denied.</p>
  <p>The error code is E_ACCESSDENIED.</p>
  <p>I do not understand why I am getting this error as I am running in SYSTEM mode already and the SetThreadDesktop had been executed twice (One during the init and another after detecting a failure)</p>
  <blockquote>
  <p>This is what the explanation of the error on MSDN : E_ACCESSDENIED if the application does not have access privilege to the current desktop image. For example, only an application that runs at LOCAL_SYSTEM can access the secure desktop.</p>
  </blockquote>
  <p>Is there anything else that would result in this kind of issue?</p>