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

Code before:
    /* In shared mode Windows tells us what format the audio must be in. */

    /* Beginning of suspicous code
     *
     * Is this seemingly unnecessary activation actually needed in some cases to initialize
     * PKEY_AudioEngine_DeviceFormat? Or was it forgotten when GetMixFormat() was commented?
     * Found a blog that indicates that GetMixFormat() can force the population. Maybe only for
     * older Windows versions?
     * Doesn't seem needed for Windows 8.1 and above. No mention in MS documentation.
     * This code came from AESinkDirectSound which was created with this as is.
     * No prior history found, the DS code prior to 2012 didn't use the MMDevice API.
     * Kept to avoid breaking something by accident.
     */
    ComPtr<IAudioClient> pClient;
    hr = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr,
                           reinterpret_cast<void**>(pClient.GetAddressOf()));
    EXIT_ON_FAILURE(hr, "Activate device failed.")

    //hr = pClient->GetMixFormat(&pwfxex);
    /* End of suspicious code */

    hr = pProperty->GetValue(PKEY_AudioEngine_DeviceFormat, &varName);
    if (SUCCEEDED(hr) && varName.blob.cbSize >= sizeof(WAVEFORMATEX))
    {
      // This may be a WAVEFORMATEXTENSIBLE but the extra data is not needed.
      WAVEFORMATEX* pwfx = (WAVEFORMATEX*)varName.blob.pBlobData;
      details.wfx = *pwfx;
    }
    else
    {
      CLog::LogF(LOGERROR, "Getting DeviceFormat failed ({})", CWIN32Util::FormatHRESULT(hr));
    }
    PropVariantClear(&varName);

    hr = pProperty->GetValue(PKEY_AudioEndpoint_PhysicalSpeakers, &varName);
    EXIT_ON_FAILURE(hr, "Retrieval of endpoint speaker layout failed.")

    details.uiChannelMask = std::max(varName.uintVal, (unsigned int)KSAUDIO_SPEAKER_STEREO);
    PropVariantClear(&varName);

    hr = pProperty->GetValue(PKEY_Device_EnumeratorName, &varName);
    if (SUCCEEDED(hr) && varName.vt != VT_EMPTY)
    {
      details.strDeviceEnumerator = KODI::PLATFORM::WINDOWS::FromW(varName.pwszVal);
      StringUtils::ToUpper(details.strDeviceEnumerator);
    }
    else
    {
      CLog::LogF(LOGDEBUG, "Retrieval of endpoint enumerator name failed: {}.",
                 (FAILED(hr)) ? "'GetValue' has failed" : "'varName.pwszVal' is NULL");
    }

Review comment (suggestion):
```suggestion
      WAVEFORMATEX* pwfx = reinterpret_cast<WAVEFORMATEX*>(varName.blob.pBlobData);
```

Code after:
    /* In shared mode Windows tells us what format the audio must be in. */
    hr = pProperty->GetValue(PKEY_AudioEngine_DeviceFormat, &varName);
    if (SUCCEEDED(hr) && varName.blob.cbSize >= sizeof(WAVEFORMATEX))
    {
      // This may be a WAVEFORMATEXTENSIBLE but the extra data is not needed.
      WAVEFORMATEX* pwfx = reinterpret_cast<WAVEFORMATEX*>(varName.blob.pBlobData);
      details.nChannels = pwfx->nChannels;
      details.m_samplesPerSec = pwfx->nSamplesPerSec;
    }
    else
    {
      CLog::LogF(LOGDEBUG, "Getting DeviceFormat failed ({})", CWIN32Util::FormatHRESULT(hr));
    }
    PropVariantClear(&varName);

    hr = pProperty->GetValue(PKEY_AudioEndpoint_PhysicalSpeakers, &varName);
    EXIT_ON_FAILURE(hr, "Retrieval of endpoint speaker layout failed.")

    details.uiChannelMask = std::max(varName.uintVal, (unsigned int)KSAUDIO_SPEAKER_STEREO);
    PropVariantClear(&varName);

    hr = pProperty->GetValue(PKEY_Device_EnumeratorName, &varName);
    if (SUCCEEDED(hr) && varName.vt != VT_EMPTY)
    {
      details.strDeviceEnumerator = KODI::PLATFORM::WINDOWS::FromW(varName.pwszVal);
      StringUtils::ToUpper(details.strDeviceEnumerator);
    }
    else
    {
      CLog::LogF(LOGDEBUG, "Retrieval of endpoint enumerator name failed: {}.",
                 (FAILED(hr)) ? "'GetValue' has failed" : "'varName.pwszVal' is NULL");
    }
    PropVariantClear(&varName);

    if (S_OK == (hr = pDevice->GetId(&pwszID)))
    {
      if (wstrDDID.compare(pwszID) == 0)
        details.bDefault = true;

      CoTaskMemFree(pwszID);
    }
    else
    {
      CLog::LogF(LOGERROR, "Unable to retrieve device id ({})", CWIN32Util::FormatHRESULT(hr));
    }

    list.push_back(details);
  }

  return list;