Repository: xbmc/xbmc
File: xbmc/cores/AudioEngine/Sinks/AESinkWASAPI.cpp (C++)

Code before:
    m_isDirty = true;
    return INT_MAX;
  }

  // fill buffer
  memcpy(buf, m_bufferPtr == 0 ? buffer : m_buffer.data(),
         m_format.m_frames * m_format.m_frameSize);
  m_bufferPtr = 0;

  // pass back to the audio driver
  hr = m_pRenderClient->ReleaseBuffer(m_format.m_frames, 0);
  if (FAILED(hr))
  {
    CLog::LogF(LOGERROR, "ReleaseBuffer failed due to {}.", CWIN32Util::FormatHRESULT(hr));
    m_isDirty = true;
    return INT_MAX;
  }

  m_sinkFrames += m_format.m_frames;

  // if not running start the audio driver
  if (!m_running)
  {
#ifdef TARGET_WINDOWS_DESKTOP
    DWORD taskIndex = 0;
    m_hTask = AvSetMmThreadCharacteristics(TEXT("Audio"), &taskIndex);
    if (m_hTask == NULL)
    {
      CLog::LogF(LOGERROR, "unable to register thread with the multimedia scheduler ({})",
                 GetLastError());
    }
#endif

    hr = m_pAudioClient->Start();
    if (FAILED(hr))
    {
      CLog::LogF(LOGERROR, "AudioClient Start Failed");
      m_isDirty = true;
      return INT_MAX;
    }
    m_running = true;
  }

  // if not all received frames have been written, save the pending ones in the aux buffer
  if (framesToCopy != frames)
  {
    m_bufferPtr = frames - framesToCopy;
    memcpy(m_buffer.data(), buffer + framesToCopy * m_format.m_frameSize,
           m_bufferPtr * m_format.m_frameSize);
  }


Review comment (suggestion):
While I think it could be a good idea, I don't think this is the right place (AddPackets). I believe this should be done when initializing the sink, and possibly investing more time and moving the code to:

https://github.com/xbmc/xbmc/blob/master/xbmc/platform/win32/threads/ThreadImplWin.cpp

Maybe create new method here "SetThreadCharacteristics" or similar. In main class is sufficient a empty method and override only for Windows.

Then Call it from:
https://github.com/xbmc/xbmc/blob/b08ff8dda24b0d05e7e078300428702d1fe8820d/xbmc/cores/AudioEngine/Engines/ActiveAE/ActiveAESink.cpp#L48

Add new "SetCharacteristics" or `SetType("Audio")` here.

This way it would also be valid for XAudio.



Code after:
  m_sinkFrames += m_format.m_frames;

  // if not running start the audio driver
  if (!m_running)
  {
    hr = m_pAudioClient->Start();
    if (FAILED(hr))
    {
      CLog::LogF(LOGERROR, "AudioClient Start Failed");
      m_isDirty = true;
      return INT_MAX;
    }
    m_running = true;
  }

  // if not all received frames have been written, save the pending ones in the aux buffer
  if (framesToCopy != frames)
  {
    m_bufferPtr = frames - framesToCopy;
    memcpy(m_buffer.data(), buffer + framesToCopy * m_format.m_frameSize,
           m_bufferPtr * m_format.m_frameSize);
  }

  return frames;
}

void CAESinkWASAPI::EnumerateDevicesEx(AEDeviceInfoList &deviceInfoList, bool force)
{
  CAEDeviceInfo        deviceInfo;
  CAEChannelInfo       deviceChannels;
  bool                 add192 = false;
  bool add48 = false;

  WAVEFORMATEXTENSIBLE wfxex = {};
  HRESULT              hr;

  const bool onlyPT = (CSysInfo::GetWindowsDeviceFamily() == CSysInfo::WindowsDeviceFamily::Xbox);

  for(RendererDetail& details : CAESinkFactoryWin::GetRendererDetails())
  {
    deviceInfo.m_channels.Reset();
    deviceInfo.m_dataFormats.clear();
    deviceInfo.m_sampleRates.clear();
    deviceInfo.m_streamTypes.clear();
    deviceChannels.Reset();
    add192 = false;
    add48 = false;

    for (unsigned int c = 0; c < WASAPI_SPEAKER_COUNT; c++)
    {
      if (details.uiChannelMask & WASAPIChannelOrder[c])