forked from ozsun88/SynchronousAudioRouter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSarCtl.cpp
More file actions
218 lines (176 loc) · 6.25 KB
/
SarCtl.cpp
File metadata and controls
218 lines (176 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// SynchronousAudioRouter
// Copyright (C) 2015 Mackenzie Straight
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SynchronousAudioRouter. If not, see <http://www.gnu.org/licenses/>.
#include <iostream>
#include <windows.h>
#include <SetupAPI.h>
#include <initguid.h>
#include <sar.h>
BOOL createEndpoint(
HANDLE device, DWORD endpointType, DWORD index, DWORD channelCount,
LPWSTR name)
{
SarCreateEndpointRequest request = {};
DWORD dummy;
request.type = endpointType;
request.index = index;
request.channelCount = channelCount;
wcscpy_s(request.name, name);
if (!DeviceIoControl(device, SAR_CREATE_ENDPOINT,
(LPVOID)&request, sizeof(request), nullptr, 0, &dummy, nullptr)) {
return FALSE;
}
return TRUE;
}
BOOL setBufferLayout(
HANDLE device, DWORD bufferSize,
DWORD sampleRate, DWORD sampleSize,
SarEndpointRegisters **regsOut,
LPVOID *bufferOut,
DWORD *bufferSizeOut)
{
SarSetBufferLayoutRequest request = {};
SarSetBufferLayoutResponse response = {};
DWORD dummy;
request.bufferSize = bufferSize;
request.frameSize = 512;
request.sampleRate = sampleRate;
request.sampleSize = sampleSize;
if (!DeviceIoControl(device, SAR_SET_BUFFER_LAYOUT,
(LPVOID)&request, sizeof(request), (LPVOID)&response, sizeof(response),
&dummy, nullptr)) {
return FALSE;
}
if (regsOut) {
*regsOut = (SarEndpointRegisters *)
((PCH)(ULONG64)response.virtualAddress + response.registerBase);
}
if (bufferOut) {
*bufferOut = (PVOID)(ULONG64)response.virtualAddress;
}
if (bufferSizeOut) {
*bufferSizeOut = response.actualSize;
}
return TRUE;
}
BOOL startRegistryFilter(HANDLE device)
{
DWORD dummy;
return DeviceIoControl(device, SAR_START_REGISTRY_FILTER,
nullptr, 0, nullptr, 0, &dummy, nullptr);
}
BOOL sendFormatChangeEvent(HANDLE device)
{
DWORD dummy;
return DeviceIoControl(device, SAR_SEND_FORMAT_CHANGE_EVENT,
nullptr, 0, nullptr, 0, &dummy, nullptr);
}
int main(int argc, char *argv[])
{
HDEVINFO devinfo;
HANDLE device;
SP_DEVICE_INTERFACE_DATA interfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetail;
DWORD requiredSize;
interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
devinfo = SetupDiGetClassDevs(
&GUID_DEVINTERFACE_SYNCHRONOUSAUDIOROUTER, nullptr, nullptr,
DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (devinfo == INVALID_HANDLE_VALUE) {
std::cerr << "Couldn't enumerate SAR devices: "
<< GetLastError() << std::endl;
return 1;
}
if (!SetupDiEnumDeviceInterfaces(devinfo, NULL,
&GUID_DEVINTERFACE_SYNCHRONOUSAUDIOROUTER, 0, &interfaceData)) {
std::cerr << "Couldn't get interface data: "
<< GetLastError() << std::endl;
return 1;
}
SetLastError(0);
SetupDiGetDeviceInterfaceDetail(
devinfo, &interfaceData, nullptr, 0, &requiredSize, nullptr);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
std::cerr << "Weird SetupDiGetDeviceInterfaceDetail failure: "
<< GetLastError() << std::endl;
}
interfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
interfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(
devinfo, &interfaceData, interfaceDetail,
requiredSize, nullptr, nullptr)) {
std::cerr << "Couldn't get device interface details: "
<< GetLastError() << std::endl;
return 1;
}
device = CreateFile(interfaceDetail->DevicePath,
GENERIC_ALL, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (device == INVALID_HANDLE_VALUE) {
std::cerr << "Couldn't open SAR device: "
<< GetLastError() << std::endl;
return 1;
}
std::cout << "Opened SAR device." << std::endl;
sendFormatChangeEvent(device);
while (true) {
Sleep(1000);
}
//SarEndpointRegisters *regs = nullptr;
//PCH buffer = nullptr;
//DWORD bufferSize = 0;
//if (!setBufferLayout(
// device, 1024 * 1024, 96000, 3, ®s, (LPVOID *)&buffer, &bufferSize)) {
// std::cerr << "Couldn't set buffer layout: "
// << GetLastError() << std::endl;
// return 1;
//}
//if (!createEndpoint(
// device, SAR_ENDPOINT_TYPE_PLAYBACK, 0, 2, L"Music Out (Stereo)")) {
// std::cerr << "Couldn't create endpoint: "
// << GetLastError() << std::endl;
// return 1;
//}
//while (true) {
// Sleep(100);
// SarEndpointRegisters snap;
// PCH first = nullptr, last = nullptr;
// DWORD sum = 0;
// RtlCopyMemory(&snap, regs, sizeof(SarEndpointRegisters));
// std::cout << "Active: " << GENERATION_IS_ACTIVE(snap.generation)
// << " Offset: " << snap.bufferOffset << " Size: "
// << snap.bufferSize << " Clock: " << snap.clockRegister
// << " Position: " << snap.positionRegister << std::endl;
// for (PCH p = buffer; p < buffer + bufferSize - SAR_BUFFER_CELL_SIZE; ++p) {
// if (*p) {
// if (!first) {
// first = p;
// }
// last = p;
// sum += *p;
// }
// }
// if (first) {
// std::cout << "First: " << (first - buffer)
// << " Last: " << (last - buffer)
// << " Sum: " << sum << std::endl;
// }
// if (snap.bufferSize) {
// regs->positionRegister =
// (regs->positionRegister + 1024) % snap.bufferSize;
// }
//}
CloseHandle(device);
return 0;
}