Merge pull request #20535 from hrydgard/new-wasapi-implementation

Add the new low-latency WASAPI backend, add audio device selection on Windows
This commit is contained in:
Henrik Rydgård
2025-06-17 08:00:40 +02:00
committed by GitHub
11 changed files with 504 additions and 695 deletions
+21 -6
View File
@@ -4,16 +4,31 @@
#include <string_view> #include <string_view>
#include <vector> #include <vector>
// Always 2 channels, 16-bit audio. // For absolute minimal latency, we do not use std::function. Might be overthinking, but...
typedef int (*StreamCallback)(short *buffer, int numSamples, int rate, void *userdata); typedef void (*RenderCallback)(float *dest, int framesToWrite, int channels, int sampleRateHz, void *userdata);
enum class LatencyMode {
Safe,
Aggressive
};
struct AudioDeviceDesc {
std::string name; // User-friendly name
std::string uniqueId; // store-able ID for settings.
};
inline float FramesToMs(int frames, int sampleRate) {
return 1000.0f * (float)frames / (float)sampleRate;
}
// Note that the backend may override the passed in sample rate. The actual sample rate
// should be returned by SampleRate though.
class AudioBackend { class AudioBackend {
public: public:
virtual ~AudioBackend() {} virtual ~AudioBackend() {}
virtual bool Init(StreamCallback _callback, void *userdata = nullptr) = 0; virtual void EnumerateDevices(std::vector<AudioDeviceDesc> *outputDevices, bool captureDevices = false) = 0;
virtual void SetRenderCallback(RenderCallback callback, void *userdata) = 0;
virtual bool InitOutputDevice(std::string_view uniqueId, LatencyMode latencyMode, bool *revertedToDefault) = 0;
virtual int SampleRate() const = 0; virtual int SampleRate() const = 0;
virtual int BufferSize() const = 0;
virtual int PeriodFrames() const = 0; virtual int PeriodFrames() const = 0;
virtual void FrameUpdate() {} virtual void FrameUpdate(bool allowAutoChange) {}
}; };
+1 -1
View File
@@ -184,7 +184,7 @@ uint32_t TextDrawerUWP::SetFont(const char *fontName, int size, int flags) {
fname = L"Tahoma"; fname = L"Tahoma";
TextDrawerFontContext *font = new TextDrawerFontContext(); TextDrawerFontContext *font = new TextDrawerFontContext();
font->weight = DWRITE_FONT_WEIGHT_LIGHT; font->weight = DWRITE_FONT_WEIGHT_NORMAL;
font->height = size; font->height = size;
font->fname = fname; font->fname = fname;
font->dpiScale = dpiScale_; font->dpiScale = dpiScale_;
+21 -4
View File
@@ -261,19 +261,30 @@ private:
// Allows passing in a dynamic vector of strings. Saves the string. // Allows passing in a dynamic vector of strings. Saves the string.
class PopupMultiChoiceDynamic : public PopupMultiChoice { class PopupMultiChoiceDynamic : public PopupMultiChoice {
public: public:
PopupMultiChoiceDynamic(std::string *value, std::string_view text, std::vector<std::string> choices, // TODO: This all is absolutely terrible, just done this way to be conformant with the internals of PopupMultiChoice.
I18NCat category, ScreenManager *screenManager, UI::LayoutParams *layoutParams = nullptr) PopupMultiChoiceDynamic(std::string *value, std::string_view text, const std::vector<std::string> &choices,
I18NCat category, ScreenManager *screenManager, std::vector<std::string> *values = nullptr, UI::LayoutParams *layoutParams = nullptr)
: UI::PopupMultiChoice(&valueInt_, text, nullptr, 0, (int)choices.size(), category, screenManager, layoutParams), : UI::PopupMultiChoice(&valueInt_, text, nullptr, 0, (int)choices.size(), category, screenManager, layoutParams),
valueStr_(value) { valueStr_(value) {
if (values) {
_dbg_assert_(choices.size() == values->size());
}
choices_ = new const char *[numChoices_]; choices_ = new const char *[numChoices_];
valueInt_ = 0; valueInt_ = 0;
for (int i = 0; i < numChoices_; i++) { for (int i = 0; i < numChoices_; i++) {
choices_[i] = new char[choices[i].size() + 1]; choices_[i] = new char[choices[i].size() + 1];
memcpy((char *)choices_[i], choices[i].c_str(), choices[i].size() + 1); memcpy((char *)choices_[i], choices[i].c_str(), choices[i].size() + 1);
if (values) {
if (*value == (*values)[i])
valueInt_ = i;
}
if (*value == choices_[i]) if (*value == choices_[i])
valueInt_ = i; valueInt_ = i;
} }
value_ = &valueInt_; value_ = &valueInt_;
if (values) {
choiceValues_ = *values;
}
UpdateText(); UpdateText();
} }
~PopupMultiChoiceDynamic() { ~PopupMultiChoiceDynamic() {
@@ -288,8 +299,13 @@ protected:
if (!valueStr_) { if (!valueStr_) {
return true; return true;
} }
if (*valueStr_ != choices_[num]) { const char *value = choices_[num];
*valueStr_ = choices_[num]; if (choiceValues_.size() == numChoices_) {
value = choiceValues_[num].c_str();
}
if (*valueStr_ != value) {
*valueStr_ = value;
return true; return true;
} else { } else {
return false; return false;
@@ -299,6 +315,7 @@ protected:
private: private:
int valueInt_; int valueInt_;
std::string *valueStr_; std::string *valueStr_;
std::vector<std::string> choiceValues_;
}; };
class PopupSliderChoice : public AbstractChoiceWithValueDisplay { class PopupSliderChoice : public AbstractChoiceWithValueDisplay {
+42 -4
View File
@@ -21,6 +21,7 @@
#include <set> #include <set>
#include "Common/Net/Resolve.h" #include "Common/Net/Resolve.h"
#include "Common/Audio/AudioBackend.h"
#include "Common/GPU/OpenGL/GLFeatures.h" #include "Common/GPU/OpenGL/GLFeatures.h"
#include "Common/Render/DrawBuffer.h" #include "Common/Render/DrawBuffer.h"
#include "Common/UI/Root.h" #include "Common/UI/Root.h"
@@ -674,10 +675,11 @@ void GameSettingsScreen::CreateAudioSettings(UI::ViewGroup *audioSettings) {
PlayUISound(UI::UISound::CONFIRM); PlayUISound(UI::UISound::CONFIRM);
return UI::EVENT_DONE; return UI::EVENT_DONE;
}); });
audioSettings->Add(new ItemHeader(a->T("Audio backend")));
bool sdlAudio = false; bool sdlAudio = false;
#if defined(SDL) #if defined(SDL)
audioSettings->Add(new ItemHeader(a->T("Audio backend")));
std::vector<std::string> audioDeviceList; std::vector<std::string> audioDeviceList;
SplitString(System_GetProperty(SYSPROP_AUDIO_DEVICE_LIST), '\0', audioDeviceList); SplitString(System_GetProperty(SYSPROP_AUDIO_DEVICE_LIST), '\0', audioDeviceList);
audioDeviceList.insert(audioDeviceList.begin(), a->T_cstr("Auto")); audioDeviceList.insert(audioDeviceList.begin(), a->T_cstr("Auto"));
@@ -694,13 +696,48 @@ void GameSettingsScreen::CreateAudioSettings(UI::ViewGroup *audioSettings) {
}); });
#endif #endif
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP) #if PPSSPP_PLATFORM(WINDOWS)
extern AudioBackend *g_audioBackend;
std::vector<std::string> audioDeviceNames;
std::vector<std::string> audioDeviceIds;
std::vector<AudioDeviceDesc> deviceDescs;
g_audioBackend->EnumerateDevices(&deviceDescs);
if (!deviceDescs.empty()) {
audioSettings->Add(new ItemHeader(a->T("Audio backend")));
for (auto &desc : deviceDescs) {
audioDeviceNames.push_back(desc.name);
audioDeviceIds.push_back(desc.uniqueId);
}
audioDeviceNames.insert(audioDeviceNames.begin(), std::string(a->T("Auto")));
audioDeviceIds.insert(audioDeviceIds.begin(), "");
PopupMultiChoiceDynamic *audioDevice = audioSettings->Add(new PopupMultiChoiceDynamic(&g_Config.sAudioDevice, a->T("Device"), audioDeviceNames, I18NCat::NONE, screenManager(), &audioDeviceIds));
audioDevice->OnChoice.Add([this](UI::EventParams &) {
bool reverted;
if (g_audioBackend->InitOutputDevice(g_Config.sAudioDevice, LatencyMode::Aggressive, &reverted)) {
if (reverted) {
WARN_LOG(Log::Audio, "Unexpected: After a direct choice, audio device reverted to default. '%s'", g_Config.sAudioDevice.c_str());
}
} else {
WARN_LOG(Log::Audio, "InitOutputDevice failed");
}
return UI::EVENT_DONE;
});
CheckBox *autoAudio = audioSettings->Add(new CheckBox(&g_Config.bAutoAudioDevice, a->T("Use new audio devices automatically")));
autoAudio->SetEnabledFunc([]()->bool {
return g_Config.sAudioDevice.empty();
});
}
const bool isWindows = true; const bool isWindows = true;
#else #else
const bool isWindows = false; const bool isWindows = false;
#endif audioSettings->Add(new ItemHeader(a->T("Audio backend")));
if (sdlAudio || isWindows) { if (sdlAudio) {
audioSettings->Add(new CheckBox(&g_Config.bAutoAudioDevice, a->T("Use new audio devices automatically"))); audioSettings->Add(new CheckBox(&g_Config.bAutoAudioDevice, a->T("Use new audio devices automatically")));
} }
@@ -712,6 +749,7 @@ void GameSettingsScreen::CreateAudioSettings(UI::ViewGroup *audioSettings) {
if (!audioErrorStr.empty()) { if (!audioErrorStr.empty()) {
audioSettings->Add(new InfoItem(a->T("Audio Error"), audioErrorStr)); audioSettings->Add(new InfoItem(a->T("Audio Error"), audioErrorStr));
} }
#endif
#endif #endif
std::vector<std::string> micList = Microphone::getDeviceList(); std::vector<std::string> micList = Microphone::getDeviceList();
+23 -2
View File
@@ -803,6 +803,22 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch
void CallbackPostRender(UIContext *dc, void *userdata); void CallbackPostRender(UIContext *dc, void *userdata);
bool CreateGlobalPipelines(); bool CreateGlobalPipelines();
void NativeMixWrapper(float *dest, int framesToWrite, int channels, int sampleRateHz, void *userdata) {
static int16_t *buffer;
static int bufSize;
if (bufSize < framesToWrite * channels) {
buffer = new int16_t[framesToWrite * channels];
bufSize = framesToWrite * channels;
}
NativeMix(buffer, framesToWrite, sampleRateHz, userdata);
for (int i = 0; i < framesToWrite * channels; i++) {
int16_t value = buffer[i];
dest[i] = (float)value * (float)(1.0f / 32767.0f);
}
}
bool NativeInitGraphics(GraphicsContext *graphicsContext) { bool NativeInitGraphics(GraphicsContext *graphicsContext) {
INFO_LOG(Log::System, "NativeInitGraphics"); INFO_LOG(Log::System, "NativeInitGraphics");
@@ -840,7 +856,12 @@ bool NativeInitGraphics(GraphicsContext *graphicsContext) {
g_audioBackend = System_CreateAudioBackend(); g_audioBackend = System_CreateAudioBackend();
if (g_audioBackend) { if (g_audioBackend) {
g_audioBackend->Init(&NativeMix); g_audioBackend->SetRenderCallback(&NativeMixWrapper, nullptr);
bool reverted = false;
g_audioBackend->InitOutputDevice(g_Config.sAudioDevice, LatencyMode::Aggressive, &reverted);
if (reverted) {
g_Config.sAudioDevice.clear();
}
} }
#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP) #if defined(_WIN32) && !PPSSPP_PLATFORM(UWP)
@@ -1093,7 +1114,7 @@ void NativeFrame(GraphicsContext *graphicsContext) {
g_screenManager->update(); g_screenManager->update();
if (g_audioBackend) { if (g_audioBackend) {
g_audioBackend->FrameUpdate(); g_audioBackend->FrameUpdate(g_Config.bAutoAudioDevice);
} }
// Do this after g_screenManager.update() so we can receive setting changes before rendering. // Do this after g_screenManager.update() so we can receive setting changes before rendering.
+1 -2
View File
@@ -17,7 +17,6 @@
#include "Common/Thread/ThreadUtil.h" #include "Common/Thread/ThreadUtil.h"
#include "Common/Data/Encoding/Utf8.h" #include "Common/Data/Encoding/Utf8.h"
#include "Common/DirectXHelper.h" #include "Common/DirectXHelper.h"
#include "Common/File/FileUtil.h"
#include "Common/Log.h" #include "Common/Log.h"
#include "Common/Log/LogManager.h" #include "Common/Log/LogManager.h"
#include "Common/TimeUtil.h" #include "Common/TimeUtil.h"
@@ -173,7 +172,7 @@ void PPSSPP_UWPMain::UpdateScreenState() {
bool PPSSPP_UWPMain::Render() { bool PPSSPP_UWPMain::Render() {
static bool hasSetThreadName = false; static bool hasSetThreadName = false;
if (!hasSetThreadName) { if (!hasSetThreadName) {
SetCurrentThreadName("UWPRenderThread"); SetCurrentThreadName("EmuThread");
hasSetThreadName = true; hasSetThreadName = true;
} }
+82 -112
View File
@@ -4,12 +4,12 @@
#include <algorithm> #include <algorithm>
#include <cstdint> #include <cstdint>
#include <thread>
#include "Common/Log.h" #include "Common/Log.h"
#include "Common/Thread/ThreadUtil.h" #include "Common/Thread/ThreadUtil.h"
#include "Common/Audio/AudioBackend.h" #include "Common/Audio/AudioBackend.h"
#include "XAudioSoundStream.h" #include "XAudioSoundStream.h"
#include <process.h>
const size_t BUFSIZE = 32 * 1024; const size_t BUFSIZE = 32 * 1024;
@@ -18,69 +18,85 @@ public:
XAudioBackend(); XAudioBackend();
~XAudioBackend() override; ~XAudioBackend() override;
bool Init(StreamCallback callback, void *userdata = nullptr) override; // If fails, can safely delete the object void EnumerateDevices(std::vector<AudioDeviceDesc> *outputDevices, bool captureDevices = false) {
// Do nothing! Auto is the only option.
}
void SetRenderCallback(RenderCallback callback, void *userdata) override {
callback_ = callback;
userdata_ = userdata;
}
bool InitOutputDevice(std::string_view uniqueId, LatencyMode latencyMode, bool *reverted) override;
int SampleRate() const override { return sampleRate_; } int SampleRate() const override { return sampleRate_; }
int PeriodFrames() const override { return 0; } int PeriodFrames() const override { return samplesPerBuffer; }
int BufferSize() const override { return samplesPerBuffer * bufferCount; }
private: private:
bool RunSound(); void Start();
bool CreateBuffer(); void Stop();
void PollLoop();
StreamCallback callback_ = nullptr; RenderCallback callback_ = nullptr;
IXAudio2 *xaudioDevice = nullptr; IXAudio2 *xaudioDevice = nullptr;
IXAudio2MasteringVoice *xaudioMaster = nullptr; IXAudio2MasteringVoice *masterVoice_ = nullptr;
IXAudio2SourceVoice *xaudioVoice = nullptr; IXAudio2SourceVoice *sourceVoice_ = nullptr;
void *userdata_ = nullptr; void *userdata_ = nullptr;
WAVEFORMATEX format_;
int sampleRate_ = 44100; int sampleRate_ = 44100;
int periodFrames_ = 0; int periodFrames_ = 0;
char realtimeBuffer_[BUFSIZE]{}; enum {
samplesPerBuffer = 480, // 10 ms @ 48kHz. maybe we can tweak this using latency mode.
bufferCount = 3,
channels = 2,
};
float audioBuffer_[samplesPerBuffer * channels];
uint32_t cursor_ = 0; uint32_t cursor_ = 0;
HANDLE thread_ = 0; std::thread thread_;
HANDLE exitEvent_ = 0; std::atomic<bool> running_{};
bool exit = false;
}; };
// TODO: Get rid of this // TODO: Get rid of this
static XAudioBackend *g_dsound; static XAudioBackend *g_dsound;
XAudioBackend::XAudioBackend() { XAudioBackend::XAudioBackend() {
exitEvent_ = CreateEvent(nullptr, true, true, L""); format_.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
format_.nChannels = channels;
format_.nSamplesPerSec = 48000;
format_.wBitsPerSample = 32;
format_.nBlockAlign = format_.nChannels * format_.wBitsPerSample / 8;
format_.nAvgBytesPerSec = format_.nSamplesPerSec * format_.nBlockAlign;
format_.cbSize = 0;
} }
inline int RoundDown128(int x) { XAudioBackend::~XAudioBackend() {
return x & (~127); Stop();
} }
bool XAudioBackend::CreateBuffer() { void XAudioBackend::Stop() {
if FAILED(xaudioDevice->CreateMasteringVoice(&xaudioMaster, 2, sampleRate_, 0, 0, NULL)) running_ = false;
return false; if (thread_.joinable()) {
thread_.join();
}
WAVEFORMATEX waveFormat; if (xaudioDevice) {
waveFormat.cbSize = sizeof(waveFormat); xaudioDevice->Release();
waveFormat.nAvgBytesPerSec = sampleRate_ * 4; xaudioDevice = nullptr;
waveFormat.nBlockAlign = 4; sourceVoice_ = nullptr;
waveFormat.nChannels = 2; }
waveFormat.nSamplesPerSec = sampleRate_;
waveFormat.wBitsPerSample = 16;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
if FAILED(xaudioDevice->CreateSourceVoice(&xaudioVoice, &waveFormat, 0, 1.0, nullptr, nullptr, nullptr))
return false;
xaudioVoice->SetFrequencyRatio(1.0);
return true;
} }
bool XAudioBackend::RunSound() { bool XAudioBackend::InitOutputDevice(std::string_view uniqueId, LatencyMode latencyMode, bool *reverted) {
Stop();
*reverted = false;
if FAILED(XAudio2Create(&xaudioDevice, 0, XAUDIO2_DEFAULT_PROCESSOR)) { if FAILED(XAudio2Create(&xaudioDevice, 0, XAUDIO2_DEFAULT_PROCESSOR)) {
xaudioDevice = NULL; xaudioDevice = nullptr;
return false; return false;
} }
@@ -90,96 +106,50 @@ bool XAudioBackend::RunSound() {
//dbgCfg.BreakMask = XAUDIO2_LOG_ERRORS; //dbgCfg.BreakMask = XAUDIO2_LOG_ERRORS;
xaudioDevice->SetDebugConfiguration(&dbgCfg); xaudioDevice->SetDebugConfiguration(&dbgCfg);
if (!CreateBuffer()) { if FAILED(xaudioDevice->CreateMasteringVoice(&masterVoice_, 2, sampleRate_, 0, 0, nullptr)) {
xaudioDevice->Release(); xaudioDevice->Release();
xaudioDevice = NULL; xaudioDevice = nullptr;
return false; return false;
} }
if FAILED(xaudioDevice->CreateSourceVoice(&sourceVoice_, &format_, 0, 1.0, nullptr, nullptr, nullptr)) {
xaudioDevice->Release();
xaudioDevice = nullptr;
return false;
}
sourceVoice_->SetFrequencyRatio(1.0);
cursor_ = 0; cursor_ = 0;
if FAILED(xaudioVoice->Start(0, XAUDIO2_COMMIT_NOW)) { if FAILED(sourceVoice_->Start(0, XAUDIO2_COMMIT_NOW)) {
xaudioDevice->Release(); xaudioDevice->Release();
xaudioDevice = NULL; xaudioDevice = nullptr;
return false; return false;
} }
thread_ = (HANDLE)_beginthreadex(0, 0, [](void* param) running_ = true;
{ thread_ = std::thread([this]() {
SetCurrentThreadName("XAudio2"); while (running_) {
XAudioBackend *backend = (XAudioBackend *)param; XAUDIO2_VOICE_STATE state = {};
backend->PollLoop(); sourceVoice_->GetState(&state);
return 0U; if (state.BuffersQueued < bufferCount) {
}, (void *)this, 0, 0); // Fill buffer with audio
SetThreadPriority(thread_, THREAD_PRIORITY_ABOVE_NORMAL); callback_(audioBuffer_, samplesPerBuffer, channels, format_.nSamplesPerSec, userdata_);
XAUDIO2_BUFFER buf = {};
buf.AudioBytes = samplesPerBuffer * channels * sizeof(float);
buf.pAudioData = reinterpret_cast<BYTE*>(audioBuffer_);
buf.Flags = 0;
sourceVoice_->SubmitSourceBuffer(&buf);
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
});
return true; return true;
} }
XAudioBackend::~XAudioBackend() {
if (!xaudioDevice)
return;
if (!xaudioVoice)
return;
exit = true;
WaitForSingleObject(exitEvent_, INFINITE);
CloseHandle(exitEvent_);
xaudioDevice->Release();
}
bool XAudioBackend::Init(StreamCallback _callback, void *userdata) {
callback_ = _callback;
userdata_ = userdata;
return RunSound();
}
void XAudioBackend::PollLoop() {
ResetEvent(exitEvent_);
while (!exit) {
XAUDIO2_VOICE_STATE state;
xaudioVoice->GetState(&state);
// TODO: Still plenty of tuning to do here.
// 4 seems to work fine.
if (state.BuffersQueued > 4) {
Sleep(1);
continue;
}
uint32_t bytesRequired = (sampleRate_ * 4) / 100;
uint32_t bytesLeftInBuffer = BUFSIZE - cursor_;
uint32_t readCount = std::min(bytesRequired, bytesLeftInBuffer);
// realtimeBuffer_ is just used as a ring of scratch space to be submitted, since SubmitSourceBuffer doesn't
// take ownership of the data. It needs to be big enough to fit the max number of buffers we check for
// above, which it is, easily.
int stereoSamplesRendered = (*callback_)((short*)&realtimeBuffer_[cursor_], readCount / 4, sampleRate_, userdata_);
int numBytesRendered = 2 * sizeof(short) * stereoSamplesRendered;
XAUDIO2_BUFFER xaudioBuffer{};
xaudioBuffer.pAudioData = (const BYTE*)&realtimeBuffer_[cursor_];
xaudioBuffer.AudioBytes = numBytesRendered;
if FAILED(xaudioVoice->SubmitSourceBuffer(&xaudioBuffer, NULL)) {
WARN_LOG(Log::Audio, "XAudioBackend: Failed writing bytes");
}
cursor_ += numBytesRendered;
if (cursor_ >= BUFSIZE) {
cursor_ = 0;
bytesLeftInBuffer = BUFSIZE;
}
periodFrames_ = stereoSamplesRendered;
}
SetEvent(exitEvent_);
}
AudioBackend *System_CreateAudioBackend() { AudioBackend *System_CreateAudioBackend() {
// Only one type available on UWP. // Only one type available on UWP.
return new XAudioBackend(); return new XAudioBackend();
+6 -6
View File
@@ -195,7 +195,7 @@
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>avrt.lib;mmdevapi.lib;wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>../ffmpeg/Windows/x86/lib</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>../ffmpeg/Windows/x86/lib</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -235,7 +235,7 @@
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>avrt.lib;mmdevapi.lib;wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>../ffmpeg/Windows/x86_64/lib</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>../ffmpeg/Windows/x86_64/lib</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile> <ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
@@ -271,7 +271,7 @@
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;oleaut32.lib;comdlg32.lib;shell32.lib;user32.lib;gdi32.lib;advapi32.lib;ole32.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>avrt.lib;mmdevapi.lib;wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;oleaut32.lib;comdlg32.lib;shell32.lib;user32.lib;gdi32.lib;advapi32.lib;ole32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>../ffmpeg/Windows/aarch64/lib</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>../ffmpeg/Windows/aarch64/lib</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile> <ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
@@ -309,7 +309,7 @@
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>avrt.lib;mmdevapi.lib;wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>../ffmpeg/Windows/x86/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>../ffmpeg/Windows/x86/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile> <OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@@ -358,7 +358,7 @@
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>avrt.lib;mmdevapi.lib;wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>../ffmpeg/Windows/x86_64/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>../ffmpeg/Windows/x86_64/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -401,7 +401,7 @@
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;oleaut32.lib;comdlg32.lib;shell32.lib;user32.lib;gdi32.lib;advapi32.lib;ole32.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>avrt.lib;mmdevapi.lib;wbemuuid.lib;dwmapi.lib;winhttp.lib;uxtheme.lib;mf.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;shlwapi.lib;Winmm.lib;Ws2_32.lib;comctl32.lib;dxguid.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;oleaut32.lib;comdlg32.lib;shell32.lib;user32.lib;gdi32.lib;advapi32.lib;ole32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>../ffmpeg/Windows/aarch64/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>../ffmpeg/Windows/aarch64/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
+227 -541
View File
@@ -1,583 +1,269 @@
#include "stdafx.h" #include <windows.h>
#include <mmdeviceapi.h>
#include <functiondiscoverykeys_devpkey.h>
#include <audioclient.h>
#include <avrt.h>
#include <comdef.h>
#include <atomic>
#include <thread>
#include <iostream>
#include <vector>
#include <string_view>
#include <initguid.h>
#include "WindowsAudio.h"
#include "WASAPIContext.h" #include "WASAPIContext.h"
#include "Common/Log.h"
#include "Common/LogReporting.h"
#include "Core/Config.h"
#include "Core/Util/AudioFormat.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Thread/ThreadUtil.h" static std::string ConvertWStringToUTF8(const std::wstring &wstr) {
int len = (int)wstr.size();
#include <memory> int size = (int)WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), len, 0, 0, NULL, NULL);
#include <mutex> std::string s;
#include <Objbase.h> s.resize(size);
#include <Mmreg.h> if (size > 0) {
#include <MMDeviceAPI.h> WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), len, &s[0], size, NULL, NULL);
#include <AudioClient.h>
#include <AudioPolicy.h>
#include <wrl/client.h>
#include "Functiondiscoverykeys_devpkey.h"
// Includes some code from https://learn.microsoft.com/en-us/windows/win32/coreaudio/device-events
#ifdef _MSC_VER
#pragma comment(lib, "ole32.lib")
#endif
// Adapted from a MSDN sample.
using Microsoft::WRL::ComPtr;
class CMMNotificationClient final : public IMMNotificationClient {
public:
CMMNotificationClient() {
}
virtual ~CMMNotificationClient() {
CoTaskMemFree(currentDevice_);
currentDevice_ = nullptr;
}
void SetCurrentDevice(IMMDevice *device) {
std::lock_guard<std::mutex> guard(lock_);
CoTaskMemFree(currentDevice_);
currentDevice_ = nullptr;
if (!device || FAILED(device->GetId(&currentDevice_))) {
currentDevice_ = nullptr;
}
if (currentDevice_) {
INFO_LOG(Log::sceAudio, "Switching to WASAPI audio device: '%s'", GetDeviceName(currentDevice_).c_str());
}
deviceChanged_ = false;
}
bool HasDefaultDeviceChanged() const {
return deviceChanged_;
}
// IUnknown methods -- AddRef, Release, and QueryInterface
ULONG STDMETHODCALLTYPE AddRef() override {
return InterlockedIncrement(&_cRef);
}
ULONG STDMETHODCALLTYPE Release() override {
ULONG ulRef = InterlockedDecrement(&_cRef);
if (0 == ulRef) {
delete this;
}
return ulRef;
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) override {
if (IID_IUnknown == riid) {
AddRef();
*ppvInterface = (IUnknown*)this;
} else if (__uuidof(IMMNotificationClient) == riid) {
AddRef();
*ppvInterface = (IMMNotificationClient*)this;
} else {
*ppvInterface = nullptr;
return E_NOINTERFACE;
}
return S_OK;
}
// Callback methods for device-event notifications.
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) override {
std::lock_guard<std::mutex> guard(lock_);
if (flow != eRender || role != eConsole) {
// Not relevant to us.
return S_OK;
}
// pwstrDeviceId can be null. We consider that a new device, I think?
bool same = currentDevice_ == pwstrDeviceId;
if (!same && currentDevice_ && pwstrDeviceId) {
same = !wcscmp(currentDevice_, pwstrDeviceId);
}
if (same) {
// Already the current device, nothing to do.
return S_OK;
}
deviceChanged_ = true;
INFO_LOG(Log::sceAudio, "New default eRender/eConsole WASAPI audio device detected: '%s'", GetDeviceName(pwstrDeviceId).c_str());
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) override {
// Ignore.
return S_OK;
};
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) override {
// Ignore.
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) override {
// Ignore.
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) override {
INFO_LOG(Log::sceAudio, "Changed audio device property "
"{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}#%d",
(uint32_t)key.fmtid.Data1, key.fmtid.Data2, key.fmtid.Data3,
key.fmtid.Data4[0], key.fmtid.Data4[1],
key.fmtid.Data4[2], key.fmtid.Data4[3],
key.fmtid.Data4[4], key.fmtid.Data4[5],
key.fmtid.Data4[6], key.fmtid.Data4[7],
(int)key.pid);
return S_OK;
}
std::string GetDeviceName(LPCWSTR pwstrId)
{
HRESULT hr = S_OK;
ComPtr<IMMDevice> pDevice;
ComPtr<IPropertyStore> pProps;
PROPVARIANT varString;
PropVariantInit(&varString);
if (_pEnumerator == NULL)
{
// Get enumerator for audio endpoint devices.
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&_pEnumerator));
}
if (hr == S_OK && _pEnumerator) {
hr = _pEnumerator->GetDevice(pwstrId, &pDevice);
}
if (hr == S_OK && pDevice) {
hr = pDevice->OpenPropertyStore(STGM_READ, &pProps);
}
if (hr == S_OK && pProps) {
// Get the endpoint device's friendly-name property.
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varString);
}
std::string name = ConvertWStringToUTF8((hr == S_OK) ? varString.pwszVal : L"null device");
PropVariantClear(&varString);
return name;
}
private:
std::mutex lock_;
LONG _cRef = 1;
ComPtr<IMMDeviceEnumerator> _pEnumerator;
wchar_t *currentDevice_ = nullptr;
bool deviceChanged_ = false;
};
// TODO: Make these adjustable. This is from the example in MSDN.
// 200 times/sec = 5ms, pretty good :) Wonder if all computers can handle it though.
#define REFTIMES_PER_SEC (10000000/200)
#define REFTIMES_PER_MILLISEC (REFTIMES_PER_SEC / 1000)
WASAPIAudioBackend::WASAPIAudioBackend() : threadData_(0) { }
WASAPIAudioBackend::~WASAPIAudioBackend() {
if (threadData_ == 0) {
threadData_ = 1;
}
if (hThread_) {
WaitForSingleObject(hThread_, 1000);
CloseHandle(hThread_);
hThread_ = nullptr;
}
if (threadData_ == 2) {
// blah.
} }
return s;
} }
unsigned int WINAPI WASAPIAudioBackend::soundThread(void *param) { static std::wstring ConvertUTF8ToWString(const std::string_view source) {
WASAPIAudioBackend *backend = (WASAPIAudioBackend *)param; int len = (int)source.size();
return backend->RunThread(); int size = (int)MultiByteToWideChar(CP_UTF8, 0, source.data(), len, NULL, 0);
} std::wstring str;
str.resize(size);
bool WASAPIAudioBackend::Init(StreamCallback callback, void *userdata) { if (size > 0) {
threadData_ = 0; MultiByteToWideChar(CP_UTF8, 0, source.data(), (int)source.size(), &str[0], size);
userdata_ = userdata;
callback_ = callback;
hThread_ = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);
if (!hThread_)
return false;
SetThreadPriority(hThread_, THREAD_PRIORITY_ABOVE_NORMAL);
return true;
}
// This to be run only on the thread.
class WASAPIAudioThread {
public:
WASAPIAudioThread(std::atomic<int> &threadData, int &sampleRate, StreamCallback &callback)
: threadData_(threadData), sampleRate_(sampleRate), callback_(callback) {
} }
~WASAPIAudioThread(); return str;
void Run();
private:
bool ActivateDefaultDevice();
bool InitAudioDevice();
void ShutdownAudioDevice();
bool DetectFormat();
bool ValidateFormat(const WAVEFORMATEXTENSIBLE *fmt);
bool PrepareFormat();
std::atomic<int> &threadData_;
int &sampleRate_;
StreamCallback &callback_;
ComPtr<IMMDeviceEnumerator> deviceEnumerator_;
ComPtr<IMMDevice> device_;
ComPtr<IAudioClient> audioInterface_;
ComPtr<CMMNotificationClient> notificationClient_;
WAVEFORMATEXTENSIBLE *deviceFormat_ = nullptr;
ComPtr<IAudioRenderClient> renderClient_;
int16_t *shortBuf_ = nullptr;
enum class Format {
UNKNOWN = 0,
IEEE_FLOAT = 1,
PCM16 = 2,
};
uint32_t numBufferFrames = 0;
Format format_ = Format::UNKNOWN;
REFERENCE_TIME actualDuration_{};
};
WASAPIAudioThread::~WASAPIAudioThread() {
delete [] shortBuf_;
shortBuf_ = nullptr;
ShutdownAudioDevice();
if (notificationClient_ && deviceEnumerator_)
deviceEnumerator_->UnregisterEndpointNotificationCallback(notificationClient_.Get());
notificationClient_ = nullptr;
deviceEnumerator_ = nullptr;
} }
bool WASAPIAudioThread::ActivateDefaultDevice() { WASAPIContext::WASAPIContext() : notificationClient_(this) {
_assert_(device_ == nullptr); HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&enumerator_);
HRESULT hresult = deviceEnumerator_->GetDefaultAudioEndpoint(eRender, eConsole, &device_); if (FAILED(hr)) {
if (FAILED(hresult) || device_ == nullptr) // Bad!
return false; enumerator_ = nullptr;
return;
_assert_(audioInterface_ == nullptr); }
hresult = device_->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, &audioInterface_); enumerator_->RegisterEndpointNotificationCallback(&notificationClient_);
if (FAILED(hresult) || audioInterface_ == nullptr)
return false;
return true;
} }
bool WASAPIAudioThread::InitAudioDevice() { WASAPIContext::~WASAPIContext() {
REFERENCE_TIME hnsBufferDuration = REFTIMES_PER_SEC; if (!enumerator_) {
_assert_(deviceFormat_ == nullptr); // Nothing can have been happening.
HRESULT hresult = audioInterface_->GetMixFormat((WAVEFORMATEX **)&deviceFormat_); return;
if (FAILED(hresult) || !deviceFormat_) }
return false; Stop();
enumerator_->UnregisterEndpointNotificationCallback(&notificationClient_);
enumerator_->Release();
}
if (!DetectFormat()) { void WASAPIContext::EnumerateDevices(std::vector<AudioDeviceDesc> *output, bool captureDevices) {
// Format unsupported - let's not even try to initialize. IMMDeviceCollection *collection = nullptr;
return false; enumerator_->EnumAudioEndpoints(captureDevices ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection);
UINT count = 0;
collection->GetCount(&count);
for (UINT i = 0; i < count; ++i) {
IMMDevice *device = nullptr;
collection->Item(i, &device);
IPropertyStore *props = nullptr;
device->OpenPropertyStore(STGM_READ, &props);
PROPVARIANT nameProp;
PropVariantInit(&nameProp);
props->GetValue(PKEY_Device_FriendlyName, &nameProp);
LPWSTR id_str = 0;
if (SUCCEEDED(device->GetId(&id_str))) {
AudioDeviceDesc desc;
desc.name = ConvertWStringToUTF8(nameProp.pwszVal);
desc.uniqueId = ConvertWStringToUTF8(id_str);
output->push_back(desc);
CoTaskMemFree(id_str);
}
PropVariantClear(&nameProp);
props->Release();
device->Release();
} }
hresult = audioInterface_->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, hnsBufferDuration, 0, &deviceFormat_->Format, nullptr); collection->Release();
if (FAILED(hresult))
return false;
_assert_(renderClient_ == nullptr);
hresult = audioInterface_->GetService(IID_PPV_ARGS(&renderClient_));
if (FAILED(hresult) || !renderClient_)
return false;
numBufferFrames = 0;
hresult = audioInterface_->GetBufferSize(&numBufferFrames);
if (FAILED(hresult) || numBufferFrames == 0)
return false;
sampleRate_ = deviceFormat_->Format.nSamplesPerSec;
return true;
} }
void WASAPIAudioThread::ShutdownAudioDevice() { bool WASAPIContext::InitOutputDevice(std::string_view uniqueId, LatencyMode latencyMode, bool *revertedToDefault) {
renderClient_ = nullptr; Stop();
CoTaskMemFree(deviceFormat_);
deviceFormat_ = nullptr;
audioInterface_ = nullptr;
device_ = nullptr;
}
bool WASAPIAudioThread::DetectFormat() { *revertedToDefault = false;
if (deviceFormat_ && !ValidateFormat(deviceFormat_)) {
// Last chance, let's try to ask for one we support instead.
WAVEFORMATEXTENSIBLE fmt{};
fmt.Format.cbSize = sizeof(fmt);
fmt.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
fmt.Format.nChannels = 2;
fmt.Format.nSamplesPerSec = 44100;
if (deviceFormat_->Format.nSamplesPerSec >= 22050 && deviceFormat_->Format.nSamplesPerSec <= 192000)
fmt.Format.nSamplesPerSec = deviceFormat_->Format.nSamplesPerSec;
fmt.Format.nBlockAlign = 2 * sizeof(float);
fmt.Format.nAvgBytesPerSec = fmt.Format.nSamplesPerSec * fmt.Format.nBlockAlign;
fmt.Format.wBitsPerSample = sizeof(float) * 8;
fmt.Samples.wReserved = 0;
fmt.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
fmt.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
WAVEFORMATEXTENSIBLE *closest = nullptr; IMMDevice *device = nullptr;
HRESULT hr = audioInterface_->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, &fmt.Format, (WAVEFORMATEX **)&closest); if (uniqueId.empty()) {
if (hr == S_OK) { // Use the default device.
// Okay, great. Let's just use ours. if (FAILED(enumerator_->GetDefaultAudioEndpoint(eRender, eConsole, &device))) {
CoTaskMemFree(closest);
CoTaskMemFree(deviceFormat_);
deviceFormat_ = (WAVEFORMATEXTENSIBLE *)CoTaskMemAlloc(sizeof(fmt));
if (deviceFormat_)
memcpy(deviceFormat_, &fmt, sizeof(fmt));
// In case something above gets out of date.
return ValidateFormat(deviceFormat_);
} else if (hr == S_FALSE && closest != nullptr) {
// This means check closest. We'll allow it only if it's less specific on channels.
if (ValidateFormat(closest)) {
CoTaskMemFree(deviceFormat_);
deviceFormat_ = closest;
} else {
wchar_t guid[256]{};
StringFromGUID2(closest->SubFormat, guid, 256);
ERROR_LOG_REPORT_ONCE(badfallbackclosest, Log::sceAudio, "WASAPI fallback and closest unsupported (fmt=%04x/%s)", closest->Format.wFormatTag, ConvertWStringToUTF8(guid).c_str());
CoTaskMemFree(closest);
return false;
}
} else {
CoTaskMemFree(closest);
if (hr != AUDCLNT_E_DEVICE_INVALIDATED && hr != AUDCLNT_E_SERVICE_NOT_RUNNING)
ERROR_LOG_REPORT_ONCE(badfallback, Log::sceAudio, "WASAPI fallback format was unsupported (%08lx)", hr);
return false; return false;
} }
}
return true;
}
bool WASAPIAudioThread::ValidateFormat(const WAVEFORMATEXTENSIBLE *fmt) {
// Don't know if PCM16 ever shows up here, the documentation only talks about float... but let's blindly
// try to support it :P
format_ = Format::UNKNOWN;
if (!fmt)
return false;
if (fmt->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
if (!memcmp(&fmt->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(fmt->SubFormat))) {
if (fmt->Format.nChannels >= 1)
format_ = Format::IEEE_FLOAT;
} else {
wchar_t guid[256]{};
StringFromGUID2(fmt->SubFormat, guid, 256);
ERROR_LOG_REPORT_ONCE(unexpectedformat, Log::sceAudio, "Got unexpected WASAPI 0xFFFE stream format (%S), expected float!", guid);
if (fmt->Format.wBitsPerSample == 16 && fmt->Format.nChannels == 2) {
format_ = Format::PCM16;
}
}
} else if (fmt->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
if (fmt->Format.nChannels >= 1)
format_ = Format::IEEE_FLOAT;
} else { } else {
ERROR_LOG_REPORT_ONCE(unexpectedformat2, Log::sceAudio, "Got unexpected non-extensible WASAPI stream format, expected extensible float!"); // Use whatever device.
if (fmt->Format.wBitsPerSample == 16 && fmt->Format.nChannels == 2) { std::wstring wId = ConvertUTF8ToWString(uniqueId);
format_ = Format::PCM16; if (FAILED(enumerator_->GetDevice(wId.c_str(), &device))) {
// Fallback to default device
printf("Falling back to default device...\n");
*revertedToDefault = true;
if (FAILED(enumerator_->GetDefaultAudioEndpoint(eRender, eConsole, &device))) {
return false;
}
} }
} }
return format_ != Format::UNKNOWN; deviceId_ = uniqueId;
}
bool WASAPIAudioThread::PrepareFormat() { HRESULT hr = E_FAIL;
delete [] shortBuf_; // Try IAudioClient3 first if not in "safe" mode. It's probably safe anyway, but still, let's use the legacy client as a safe fallback option.
shortBuf_ = nullptr; if (latencyMode != LatencyMode::Safe) {
hr = device->Activate(__uuidof(IAudioClient3), CLSCTX_ALL, nullptr, (void**)&audioClient3_);
}
if (SUCCEEDED(hr)) {
audioClient3_->GetMixFormat(&format_);
audioClient3_->GetSharedModeEnginePeriod(format_, &defaultPeriodFrames, &fundamentalPeriodFrames, &minPeriodFrames, &maxPeriodFrames);
BYTE *pData = nullptr; printf("default: %d fundamental: %d min: %d max: %d\n", defaultPeriodFrames, fundamentalPeriodFrames, minPeriodFrames, maxPeriodFrames);
HRESULT hresult = renderClient_->GetBuffer(numBufferFrames, &pData); printf("initializing with %d frame period at %d Hz, meaning %0.1fms\n", (int)minPeriodFrames, (int)format_->nSamplesPerSec, FramesToMs(minPeriodFrames, format_->nSamplesPerSec));
if (FAILED(hresult) || !pData)
return false;
const int numSamples = numBufferFrames * deviceFormat_->Format.nChannels; audioEvent_ = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (format_ == Format::IEEE_FLOAT) { HRESULT result = audioClient3_->InitializeSharedAudioStream(
memset(pData, 0, sizeof(float) * numSamples); AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
// This buffer is always stereo - PPSSPP writes to it. minPeriodFrames,
shortBuf_ = new short[numBufferFrames * 2]; format_,
} else if (format_ == Format::PCM16) { nullptr
memset(pData, 0, sizeof(short) * numSamples); );
if (FAILED(result)) {
printf("Error initializing shared audio stream: %08x", result);
audioClient3_->Release();
device->Release();
audioClient3_ = nullptr;
device = nullptr;
return false;
}
actualPeriodFrames_ = minPeriodFrames;
audioClient3_->GetBufferSize(&reportedBufferSize_);
audioClient3_->SetEventHandle(audioEvent_);
audioClient3_->GetService(__uuidof(IAudioRenderClient), (void**)&renderClient_);
} else {
// Fallback to IAudioClient (older OS)
device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void**)&audioClient_);
audioClient_->GetMixFormat(&format_);
// Get engine period info
REFERENCE_TIME defaultPeriod = 0, minPeriod = 0;
audioClient_->GetDevicePeriod(&defaultPeriod, &minPeriod);
audioEvent_ = CreateEvent(nullptr, FALSE, FALSE, nullptr);
const REFERENCE_TIME duration = minPeriod;
HRESULT hr = audioClient_->Initialize(
AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
duration, // This is a minimum, the result might be larger. We use GetBufferSize to check.
0, // ref duration, always 0 in shared mode.
format_,
nullptr
);
if (FAILED(hr)) {
printf("ERROR: Failed to initialize audio with all attempted buffer sizes\n");
audioClient_->Release();
device->Release();
audioClient_ = nullptr;
device = nullptr;
return false;
}
audioClient_->GetBufferSize(&reportedBufferSize_);
actualPeriodFrames_ = reportedBufferSize_; // we don't have a better estimate.
audioClient_->SetEventHandle(audioEvent_);
audioClient_->GetService(__uuidof(IAudioRenderClient), (void**)&renderClient_);
} }
hresult = renderClient_->ReleaseBuffer(numBufferFrames, 0); latencyMode_ = latencyMode;
if (FAILED(hresult))
return false;
actualDuration_ = (REFERENCE_TIME)((double)REFTIMES_PER_SEC * numBufferFrames / deviceFormat_->Format.nSamplesPerSec); Start();
device->Release();
return true; return true;
} }
void WASAPIAudioThread::Run() { void WASAPIContext::Start() {
// Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/dd316756(v=vs.85).aspx running_ = true;
audioThread_ = std::thread([this]() { AudioLoop(); });
}
_assert_(deviceEnumerator_ == nullptr); void WASAPIContext::Stop() {
HRESULT hresult = CoCreateInstance(__uuidof(MMDeviceEnumerator), running_ = false;
nullptr, /* Object is not created as the part of the aggregate */ if (audioClient_) audioClient_->Stop();
CLSCTX_ALL, IID_PPV_ARGS(&deviceEnumerator_)); if (audioEvent_) SetEvent(audioEvent_);
if (audioThread_.joinable()) audioThread_.join();
if (FAILED(hresult) || deviceEnumerator_ == nullptr) if (renderClient_) { renderClient_->Release(); renderClient_ = nullptr; }
return; if (audioClient_) { audioClient_->Release(); audioClient_ = nullptr; }
if (audioEvent_) { CloseHandle(audioEvent_); audioEvent_ = nullptr; }
if (format_) { CoTaskMemFree(format_); format_ = nullptr; }
}
if (!ActivateDefaultDevice()) { void WASAPIContext::FrameUpdate(bool allowAutoChange) {
ERROR_LOG(Log::sceAudio, "WASAPI: Could not activate default device"); if (deviceId_.empty() && defaultDeviceChanged_ && allowAutoChange) {
return; defaultDeviceChanged_ = false;
} Stop();
Start();
notificationClient_ = new CMMNotificationClient();
notificationClient_->SetCurrentDevice(device_.Get());
hresult = deviceEnumerator_->RegisterEndpointNotificationCallback(notificationClient_.Get());
if (FAILED(hresult)) {
// Let's just keep going, but release the client since it doesn't work.
notificationClient_ = nullptr;
}
if (!InitAudioDevice()) {
ERROR_LOG(Log::sceAudio, "WASAPI: Could not init audio device");
return;
}
if (!PrepareFormat()) {
ERROR_LOG(Log::sceAudio, "WASAPI: Could not find a suitable audio output format");
return;
}
hresult = audioInterface_->Start();
if (FAILED(hresult)) {
ERROR_LOG(Log::sceAudio, "WASAPI: Failed to start audio stream");
return;
}
DWORD flags = 0;
while (flags != AUDCLNT_BUFFERFLAGS_SILENT) {
Sleep((DWORD)(actualDuration_ / REFTIMES_PER_MILLISEC / 2));
uint32_t pNumPaddingFrames = 0;
hresult = audioInterface_->GetCurrentPadding(&pNumPaddingFrames);
if (FAILED(hresult)) {
// What to do?
pNumPaddingFrames = 0;
}
uint32_t pNumAvFrames = numBufferFrames - pNumPaddingFrames;
BYTE *pData = nullptr;
hresult = renderClient_->GetBuffer(pNumAvFrames, &pData);
if (FAILED(hresult) || pData == nullptr) {
// What to do?
} else if (pNumAvFrames) {
int chans = deviceFormat_->Format.nChannels;
switch (format_) {
case Format::IEEE_FLOAT:
callback_(shortBuf_, pNumAvFrames, sampleRate_, nullptr);
if (chans == 1) {
float *ptr = (float *)pData;
memset(ptr, 0, pNumAvFrames * chans * sizeof(float));
for (uint32_t i = 0; i < pNumAvFrames; i++) {
ptr[i * chans + 0] = 0.5f * ((float)shortBuf_[i * 2] + (float)shortBuf_[i * 2 + 1]) * (1.0f / 32768.0f);
}
} else if (chans == 2) {
ConvertS16ToF32((float *)pData, shortBuf_, pNumAvFrames * chans);
} else if (chans > 2) {
float *ptr = (float *)pData;
memset(ptr, 0, pNumAvFrames * chans * sizeof(float));
for (uint32_t i = 0; i < pNumAvFrames; i++) {
ptr[i * chans + 0] = (float)shortBuf_[i * 2] * (1.0f / 32768.0f);
ptr[i * chans + 1] = (float)shortBuf_[i * 2 + 1] * (1.0f / 32768.0f);
}
}
break;
case Format::PCM16:
callback_((short *)pData, pNumAvFrames, sampleRate_, nullptr);
break;
case Format::UNKNOWN:
break;
}
}
if (threadData_ != 0) {
flags = AUDCLNT_BUFFERFLAGS_SILENT;
}
if (!FAILED(hresult) && pData) {
hresult = renderClient_->ReleaseBuffer(pNumAvFrames, flags);
if (FAILED(hresult)) {
// Not much to do here either...
}
}
// Check if we should use a new device.
if (notificationClient_ && notificationClient_->HasDefaultDeviceChanged() && g_Config.bAutoAudioDevice) {
hresult = audioInterface_->Stop();
ShutdownAudioDevice();
if (!ActivateDefaultDevice()) {
ERROR_LOG(Log::sceAudio, "WASAPI: Could not activate default device");
// TODO: Return to the old device here?
return;
}
notificationClient_->SetCurrentDevice(device_.Get());
if (!InitAudioDevice()) {
ERROR_LOG(Log::sceAudio, "WASAPI: Could not init audio device");
return;
}
if (!PrepareFormat()) {
ERROR_LOG(Log::sceAudio, "WASAPI: Could not find a suitable audio output format");
return;
}
hresult = audioInterface_->Start();
if (FAILED(hresult)) {
ERROR_LOG(Log::sceAudio, "WASAPI: Failed to start audio stream");
return;
}
}
}
// Wait for last data in buffer to play before stopping.
Sleep((DWORD)(actualDuration_ / REFTIMES_PER_MILLISEC / 2));
hresult = audioInterface_->Stop();
if (FAILED(hresult)) {
ERROR_LOG(Log::sceAudio, "WASAPI: Failed to stop audio stream");
} }
} }
int WASAPIAudioBackend::RunThread() { void WASAPIContext::AudioLoop() {
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); DWORD taskID = 0;
_dbg_assert_(SUCCEEDED(hr)); HANDLE mmcssHandle = NULL;
SetCurrentThreadName("WASAPI_audio"); if (latencyMode_ == LatencyMode::Aggressive) {
mmcssHandle = AvSetMmThreadCharacteristics(L"Pro Audio", &taskID);
if (threadData_ == 0) {
// This will free everything once it's done.
WASAPIAudioThread renderer(threadData_, sampleRate_, callback_);
renderer.Run();
} }
threadData_ = 2; if (audioClient3_) {
CoUninitialize(); audioClient3_->Start();
return 0; } else {
audioClient_->Start();
}
double phase = 0.0;
while (running_) {
DWORD result = WaitForSingleObject(audioEvent_, INFINITE);
if (result != WAIT_OBJECT_0) {
// Something bad happened.
break;
}
UINT32 padding = 0, available = 0;
if (audioClient3_)
audioClient3_->GetCurrentPadding(&padding), audioClient3_->GetBufferSize(&available);
else
audioClient_->GetCurrentPadding(&padding), audioClient_->GetBufferSize(&available);
UINT32 framesToWrite = available - padding;
BYTE* buffer = nullptr;
if (framesToWrite > 0 && SUCCEEDED(renderClient_->GetBuffer(framesToWrite, &buffer))) {
callback_((float *)buffer, framesToWrite, 2, format_->nSamplesPerSec, userdata_);
renderClient_->ReleaseBuffer(framesToWrite, 0);
}
// In the old mode, we just estimate the "actualPeriodFrames_" from the framesToWrite.
if (audioClient_ && framesToWrite < actualPeriodFrames_) {
actualPeriodFrames_ = framesToWrite;
}
}
if (audioClient3_) {
audioClient3_->Stop();
} else {
audioClient_->Stop();
}
if (mmcssHandle) {
AvRevertMmThreadCharacteristics(mmcssHandle);
}
} }
+79 -16
View File
@@ -3,26 +3,89 @@
#include <atomic> #include <atomic>
#include "WindowsAudio.h" #include "WindowsAudio.h"
// This should only be included from WindowsAudio.cpp and WASAPIStream.cpp. #include <windows.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <cmath>
#include <thread>
#include <vector>
#include <string>
#include <string_view>
class WASAPIAudioBackend : public AudioBackend { class WASAPIContext : public AudioBackend {
public: public:
WASAPIAudioBackend(); WASAPIContext();
~WASAPIAudioBackend(); ~WASAPIContext();
bool Init(StreamCallback callback, void *userdata) override; // If fails, can safely delete the object void SetRenderCallback(RenderCallback callback, void *userdata) override {
int SampleRate() const override { return sampleRate_; } callback_ = callback;
int PeriodFrames() const override { return periodFrames_; } // amount of frames normally requested userdata_ = userdata;
void FrameUpdate() override {} }
void EnumerateDevices(std::vector<AudioDeviceDesc> *outputDevices, bool captureDevices = false);
bool InitOutputDevice(std::string_view uniqueId, LatencyMode latencyMode, bool *revertedToDefault);
void FrameUpdate(bool allowAutoChange) override;
int PeriodFrames() const override { return actualPeriodFrames_; } // NOTE: This may have the wrong value (too large) until audio has started playing.
int BufferSize() const override { return reportedBufferSize_; }
int SampleRate() const override { return format_->nSamplesPerSec; }
// Implements device change notifications
class DeviceNotificationClient : public IMMNotificationClient {
public:
DeviceNotificationClient(WASAPIContext *engine) : engine_(engine) {}
ULONG STDMETHODCALLTYPE AddRef() override { return 1; }
ULONG STDMETHODCALLTYPE Release() override { return 1; }
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppv) override {
if (iid == __uuidof(IUnknown) || iid == __uuidof(IMMNotificationClient)) {
*ppv = static_cast<IMMNotificationClient*>(this);
return S_OK;
}
*ppv = nullptr;
return E_NOINTERFACE;
}
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR) override {
if (flow == eRender && role == eConsole) {
// PostMessage(hwnd, WM_APP + 1, 0, 0);
engine_->defaultDeviceChanged_ = true;
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR) override { return S_OK; }
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR) override { return S_OK; }
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR, DWORD) override { return S_OK; }
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR, const PROPERTYKEY) override { return S_OK; }
private:
WASAPIContext *engine_;
};
private: private:
int RunThread(); void Start();
static unsigned int WINAPI soundThread(void *param); void Stop();
HANDLE hThread_ = nullptr; void AudioLoop();
StreamCallback callback_ = nullptr;
int sampleRate_ = 0; // Only one of these can be non-null at a time. Check audioClient3 to determine if it's being used.
int periodFrames_ = 0; IAudioClient3 *audioClient3_ = nullptr;
void *userdata_ = 0; IAudioClient *audioClient_ = nullptr;
std::atomic<int> threadData_{};
IAudioRenderClient* renderClient_ = nullptr;
WAVEFORMATEX *format_ = nullptr;
HANDLE audioEvent_ = nullptr;
std::thread audioThread_;
UINT32 defaultPeriodFrames = 0, fundamentalPeriodFrames = 0, minPeriodFrames = 0, maxPeriodFrames = 0;
std::atomic<bool> running_ = true;
UINT32 actualPeriodFrames_ = 0; // may not be the requested.
UINT32 reportedBufferSize_ = 0;
IMMDeviceEnumerator *enumerator_ = nullptr;
DeviceNotificationClient notificationClient_;
RenderCallback callback_{};
void *userdata_ = nullptr;
LatencyMode latencyMode_ = LatencyMode::Aggressive;
std::string deviceId_;
std::atomic<bool> defaultDeviceChanged_{};
}; };
+1 -1
View File
@@ -3,5 +3,5 @@
#include "WASAPIContext.h" #include "WASAPIContext.h"
AudioBackend *System_CreateAudioBackend() { AudioBackend *System_CreateAudioBackend() {
return new WASAPIAudioBackend(); return new WASAPIContext();
} }