From 85e9c5b1d4232994f64a0a42e543680e596736c1 Mon Sep 17 00:00:00 2001 From: Alex Marsev Date: Tue, 26 Jan 2016 15:22:30 +0300 Subject: Use MMCSS for event mode audio device --- src/AudioDeviceEvent.cpp | 23 ++++++++++++++++++++++- src/Utils.h | 18 ++++++++++++++++++ src/pch.h | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/AudioDeviceEvent.cpp b/src/AudioDeviceEvent.cpp index b4fd323..774b74b 100644 --- a/src/AudioDeviceEvent.cpp +++ b/src/AudioDeviceEvent.cpp @@ -3,6 +3,15 @@ namespace SaneAudioRenderer { + namespace + { + WinapiFunc + AvSetMmThreadCharacteristicsFunction(L"avrt.dll", "AvSetMmThreadCharacteristicsW"); + + WinapiFunc + AvRevertMmThreadCharacteristicsFunction(L"avrt.dll", "AvRevertMmThreadCharacteristics"); + } + AudioDeviceEvent::AudioDeviceEvent(std::shared_ptr backend) { assert(backend); @@ -143,7 +152,16 @@ namespace SaneAudioRenderer void AudioDeviceEvent::EventFeed() { - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + HANDLE taskHandle = NULL; + if (AvSetMmThreadCharacteristicsFunction && AvRevertMmThreadCharacteristicsFunction) + { + DWORD taskIndex = 0; + taskHandle = AvSetMmThreadCharacteristicsFunction(L"Pro Audio", &taskIndex); + assert(taskHandle != NULL); + } + + if (taskHandle == NULL) + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); while (!m_exit) { @@ -172,6 +190,9 @@ namespace SaneAudioRenderer m_wake.Wait(); } + + if (taskHandle != NULL) + AvRevertMmThreadCharacteristicsFunction(taskHandle); } void AudioDeviceEvent::PushBufferToDevice() diff --git a/src/Utils.h b/src/Utils.h index 1a8d9a9..8b996aa 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -118,4 +118,22 @@ namespace SaneAudioRenderer } #endif } + + template + class WinapiFunc; + template + class WinapiFunc final + { + public: + typedef ReturnType(WINAPI* Func)(Args...); + WinapiFunc(LPCWSTR dll, LPCSTR func) { m_lib = LoadLibrary(dll); m_func = (Func)GetProcAddress(m_lib, func); } + WinapiFunc(const WinapiFunc&) = delete; + WinapiFunc& operator=(const WinapiFunc&) = delete; + ~WinapiFunc() { FreeLibrary(m_lib); } + explicit operator bool() const { return !!m_func; } + ReturnType operator()(Args...args) const { return m_func(args...); } + private: + HMODULE m_lib = NULL; + Func m_func = nullptr; + }; } diff --git a/src/pch.h b/src/pch.h index 530d9ac..7490c9a 100644 --- a/src/pch.h +++ b/src/pch.h @@ -8,6 +8,7 @@ #include +#include #include #include #include -- cgit v1.2.3