HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
AudioRenderClientHook.cpp
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2018-2026 Benjamin Höglinger-Stelzer
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25
27#include "Exceptions.hpp"
28
29#include <wrl/client.h>
30
31using namespace HydraHook::Core::Exceptions;
32using Microsoft::WRL::ComPtr;
33
35{
36 const HRESULT coInit = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
37 comInitialized_ = (coInit == S_OK || coInit == S_FALSE);
38
39 ComPtr<IMMDeviceEnumerator> enumeratorPtr;
40 HRESULT hr = CoCreateInstance(
41 __uuidof(MMDeviceEnumerator),
42 nullptr,
43 CLSCTX_ALL,
44 __uuidof(IMMDeviceEnumerator),
45 reinterpret_cast<void**>(enumeratorPtr.GetAddressOf())
46 );
47 if (FAILED(hr))
48 {
49 throw ARCException("CoCreateInstance failed", hr);
50 }
51
52 ComPtr<IMMDevice> devicePtr;
53 hr = enumeratorPtr->GetDefaultAudioEndpoint(eRender, eConsole, devicePtr.GetAddressOf());
54 if (FAILED(hr))
55 {
56 throw ARCException("Failed to get default audio endpoint", hr);
57 }
58
59 ComPtr<IAudioClient> clientPtr;
60 hr = devicePtr->Activate(
61 __uuidof(IAudioClient),
62 CLSCTX_ALL,
63 nullptr,
64 reinterpret_cast<void**>(clientPtr.GetAddressOf())
65 );
66 if (FAILED(hr))
67 {
68 throw ARCException("Failed to activate IAudioClient instance", hr);
69 }
70
71 hr = clientPtr->GetMixFormat(&pwfx);
72 if (FAILED(hr) || !pwfx)
73 {
74 throw ARCException("Failed to query the mix format", FAILED(hr) ? hr : E_POINTER);
75 }
76
77 auto initializeAt = [&](DWORD sampleRate) -> HRESULT
78 {
79 pwfx->nSamplesPerSec = sampleRate;
80 pwfx->nAvgBytesPerSec = sampleRate * pwfx->nBlockAlign;
81 return clientPtr->Initialize(
82 AUDCLNT_SHAREMODE_SHARED,
83 0,
84 10 * 1000 * 1000,
85 0,
86 pwfx,
87 nullptr
88 );
89 };
90
91 hr = clientPtr->Initialize(
92 AUDCLNT_SHAREMODE_SHARED,
93 0,
94 10 * 1000 * 1000,
95 0,
96 pwfx,
97 nullptr
98 );
99 if (hr != S_OK)
100 {
101 hr = initializeAt(48000);
102 if (hr != S_OK)
103 hr = initializeAt(44100);
104 }
105 if (FAILED(hr))
106 {
107 throw ARCException("Failed to initialize IAudioClient instance", hr);
108 }
109
110 ComPtr<IAudioRenderClient> arcPtr;
111 hr = clientPtr->GetService(
112 __uuidof(IAudioRenderClient),
113 reinterpret_cast<void**>(arcPtr.GetAddressOf())
114 );
115 if (FAILED(hr))
116 {
117 throw ARCException("Failed to request IAudioRenderClient service", hr);
118 }
119
120 enumerator = enumeratorPtr.Detach();
121 device = devicePtr.Detach();
122 client = clientPtr.Detach();
123 arc = arcPtr.Detach();
124}
125
127{
128 if (arc)
129 arc->Release();
130 if (client)
131 client->Release();
132 if (device)
133 device->Release();
134 if (enumerator)
135 enumerator->Release();
136 if (pwfx)
137 CoTaskMemFree(pwfx);
138 if (comInitialized_)
139 CoUninitialize();
140}
141
143{
144 return std::vector<size_t>(*reinterpret_cast<size_t**>(arc), *reinterpret_cast<size_t**>(arc) + VTableElements);
145}
IAudioRenderClient vtable hook for Core Audio interception.
HydraHook exception types (Detours, Win32, generic).
ARCException(const std::string &msg, HRESULT result)