title: Windows Desktop Duplication Api - Trying to get desired fps with fixed time step - Problem with the Timeout from the AcquireNextFrame function
body: <p>I'm currently trying to create a &quot;screenshare&quot; program that has low latency and good performance. For this I use DirectX to capture the screen and FFmpeg to encode it. And all in C++. But now I have several problems that I just can't solve.</p>
  <ol>
  <li><p>My biggest problem right now is getting frames at a fixed rate. I've already tried some things, like the sleep function, but this is way too inaccurate. Another thing I tried was some approaches from <a href="https://blat-blatnik.github.io/computerBear/making-accurate-sleep-function/" rel="nofollow noreferrer">this</a> github page (It contains code aswell) and I actually got good results for accuracy but every approach got me too high CPU or too low accuracy either way. The reason for the high CPU usage was spin locking. I'm pretty stuck at this point. I have no idea how to get a frame every ~16.67ms for example.</p>
  </li>
  <li><p>My second problem has to do with the <code>AcquireNextFrame</code> function timing out. Let's say I want 120 FPS, then I have a delta time of <code>1000 / 120 = 8.33333333333 ms</code>, but it won't actually time out when it reaches 8 ms, but actually a bit later (about 7ms later), and then I can't feed the encoder with the last frame because the timeout was longer than 8ms so the video gets slowed down. And that happens quite often because there's a timeout when nothing changes on the screen or it's just not fast enough.</p>
  </li>
  </ol>
  <p>Here is the code on how I'm getting the Frame</p>
  <pre><code>FRAME_DATA DuplicationManager::getFrame(UINT timeout) {
  FRAME_DATA result;
  
  IDXGIResource* DesktopResource = nullptr;
  DXGI_OUTDUPL_FRAME_INFO FrameInfo;
  
  HRESULT hr = m_DesktopDup-&gt;AcquireNextFrame(timeout, &amp;FrameInfo, &amp;DesktopResource);
  
  if (hr == DXGI_ERROR_ACCESS_LOST) {
      result.result = RESULT_ACCESSLOST;
      m_DesktopDup-&gt;ReleaseFrame();
      initialize();
      return result;
  }
  
  if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
      result.result = RESULT_TIMEOUT;
      m_DesktopDup-&gt;ReleaseFrame();
      return result;
  }
  
  if (FAILED(hr)) {
      result.result = RESULT_ERROR;
      result.error = &quot;Failed to aquire next frame: &quot; + std::system_category().message(hr);
      m_DesktopDup-&gt;ReleaseFrame();
      initialize();
      return result;
  }
  
  if (m_LastImage) {
      m_LastImage-&gt;Release();
      m_LastImage = nullptr;
  }
  
  hr = DesktopResource-&gt;QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast&lt;void**&gt;(&amp;m_LastImage));
  DesktopResource-&gt;Release();
  DesktopResource = nullptr;
  
  if (FAILED(hr)) {
      result.result = RESULT_ERROR;
      result.error = &quot;Failed to QI for ID3D11Texture2D from acquired IDXGIResource: &quot; + std::system_category().message(hr);
      m_DesktopDup-&gt;ReleaseFrame();
      return result;
  }
  
  D3D11_TEXTURE2D_DESC frameDesc;
  m_LastImage-&gt;GetDesc(&amp;frameDesc);
  
  D3D11_TEXTURE2D_DESC stagingTextureDesc;
  stagingTextureDesc.Width = frameDesc.Width;
  stagingTextureDesc.Height = frameDesc.Height;
  stagingTextureDesc.MipLevels = frameDesc.MipLevels;
  stagingTextureDesc.ArraySize = 1;
  stagingTextureDesc.Format = frameDesc.Format;
  stagingTextureDesc.SampleDesc = frameDesc.SampleDesc;
  stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
  stagingTextureDesc.BindFlags = 0;
  stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  stagingTextureDesc.MiscFlags = 0;
  
  ID3D11Texture2D* stagingTexture = nullptr;
  
  hr = m_Device-&gt;CreateTexture2D(&amp;stagingTextureDesc, nullptr, &amp;stagingTexture);
  if (FAILED(hr)) {
      result.result = RESULT_ERROR;
      result.error = &quot;Failed to create shared surface: &quot; + std::system_category().message(hr);
      m_DesktopDup-&gt;ReleaseFrame();
      return result;
  }
  
  m_Context-&gt;CopyResource(stagingTexture, m_LastImage);
  
  result = getFrameData(stagingTexture, stagingTextureDesc);
  
  stagingTexture-&gt;Release();
  m_DesktopDup-&gt;ReleaseFrame();
  
  return result;
  }
  </code></pre>
  <p>And here is the code to turn it into a Bitmap</p>
  <pre><code>FRAME_DATA DuplicationManager::getFrameData(ID3D11Texture2D* texture, D3D11_TEXTURE2D_DESC&amp; textureDesc) {
  FRAME_DATA result;
  
  D3D11_MAPPED_SUBRESOURCE resourceAccess;
  
  HRESULT hr = m_Context-&gt;Map(texture, 0, D3D11_MAP_READ, 0, &amp;resourceAccess);
  
  if (FAILED(hr)) {
      result.result = RESULT_ERROR;
      result.error = &quot;Failed to get pointer to the data contained in the shared texture: &quot; + std::system_category().message(hr);
      return result;
  }
  
  void* imgData = malloc(textureDesc.Width * textureDesc.Height * 4);
  
  if (imgData == NULL) {
      m_Context-&gt;Unmap(texture, 0);
      result.result = RESULT_ERROR;
      result.error = &quot;Failed to allocate memory for the frame&quot;;
      return result;
  }
  
  char* p = (char*)resourceAccess.pData;
  
  for (uint32_t y = 0; y &lt; textureDesc.Height; y++) {
      memcpy((char*)imgData + y * textureDesc.Width * 4, p, textureDesc.Width * 4);
      p += resourceAccess.RowPitch;
  }
  
  char* data = reinterpret_cast&lt;char*&gt;(imgData);
  char temp;
  for (uint32_t i = 0; i &lt; textureDesc.Width * textureDesc.Height * 4; i += 4) {
      temp = data[i + 2];
      data[i + 2] = data[i];
      data[i] = temp;
  }
  
  result.result = RESULT_SUCCESS;
  result.data = data;
  result.width = textureDesc.Width;
  result.height = textureDesc.Height;
  
  m_Context-&gt;Unmap(texture, 0);
  
  return result;
  }
  </code></pre>
  <p>Any help or suggestion is appreciated!</p>