forked from eiz/SynchronousAudioRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavert.cpp
More file actions
285 lines (237 loc) · 8.94 KB
/
wavert.cpp
File metadata and controls
285 lines (237 loc) · 8.94 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// 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 "sar.h"
NTSTATUS SarKsPinRtGetBufferCore(
PIRP irp, PVOID baseAddress, ULONG requestedBufferSize,
ULONG notificationCount, PKSRTAUDIO_BUFFER buffer)
{
SarEndpoint *endpoint = SarGetEndpointFromIrp(irp, TRUE);
SarControlContext *controlContext = endpoint->owner;
SarEndpointProcessContext *processContext;
NTSTATUS status;
if (!endpoint) {
SAR_ERROR("No valid endpoint");
return STATUS_NOT_FOUND;
}
if (baseAddress != nullptr) {
SAR_ERROR("It wants a specific address");
SarReleaseEndpointAndContext(endpoint);
return STATUS_NOT_IMPLEMENTED;
}
if (endpoint->activeChannelCount == 0) {
SAR_ERROR("activeChannelCount not set, assuming channelCount");
KdBreakPoint();
endpoint->activeChannelCount = endpoint->channelCount;
}
status = SarGetOrCreateEndpointProcessContext(
endpoint, PsGetCurrentProcess(), &processContext);
if (!NT_SUCCESS(status)) {
SAR_ERROR("Get process context failed: %08X", status);
SarReleaseEndpointAndContext(endpoint);
return status;
}
ULONG actualSize = ROUND_UP(
max(requestedBufferSize,
controlContext->minimumFrameCount *
controlContext->periodSizeBytes *
endpoint->activeChannelCount),
controlContext->sampleSize * endpoint->activeChannelCount);
SIZE_T viewSize = ROUND_UP(actualSize, SAR_BUFFER_CELL_SIZE);
ExAcquireFastMutex(&controlContext->mutex);
if (!controlContext->bufferSection) {
SAR_ERROR("Buffer isn't allocated");
ExReleaseFastMutex(&controlContext->mutex);
SarReleaseEndpointAndContext(endpoint);
return STATUS_INSUFFICIENT_RESOURCES;
}
ULONG cellIndex = RtlFindClearBitsAndSet(
&controlContext->bufferMap,
(ULONG)(viewSize / SAR_BUFFER_CELL_SIZE), 0);
if (cellIndex == 0xFFFFFFFF) {
SAR_ERROR("Cell index full 0xFFFFFFFF");
ExReleaseFastMutex(&controlContext->mutex);
SarReleaseEndpointAndContext(endpoint);
return STATUS_INSUFFICIENT_RESOURCES;
}
endpoint->activeCellIndex = cellIndex;
endpoint->activeViewSize = viewSize;
endpoint->activeBufferSize = actualSize;
ExReleaseFastMutex(&controlContext->mutex);
PVOID mappedAddress = nullptr;
LARGE_INTEGER sectionOffset;
sectionOffset.QuadPart = cellIndex * SAR_BUFFER_CELL_SIZE;
SAR_DEBUG("Mapping %08lX %016llX %lu %lu", (ULONG)viewSize, sectionOffset.QuadPart,
actualSize, requestedBufferSize);
status = ZwMapViewOfSection(
endpoint->owner->bufferSection, ZwCurrentProcess(),
&mappedAddress, 0, 0, §ionOffset, &viewSize, ViewUnmap,
0, PAGE_READWRITE);
if (!NT_SUCCESS(status)) {
SAR_ERROR("Section mapping failed %08X", status);
SarReleaseEndpointAndContext(endpoint);
return status;
}
SarEndpointRegisters regs = {};
processContext->bufferUVA = mappedAddress;
status = SarReadEndpointRegisters(®s, endpoint);
if (!NT_SUCCESS(status)) {
SAR_ERROR("Read endpoint registers failed %08X", status);
SarReleaseEndpointAndContext(endpoint);
return status;
}
regs.bufferOffset = cellIndex * SAR_BUFFER_CELL_SIZE;
regs.bufferSize = actualSize;
regs.notificationCount = notificationCount;
status = SarWriteEndpointRegisters(®s, endpoint);
if (!NT_SUCCESS(status)) {
SAR_ERROR("Couldn't write endpoint registers: %08X %p %p", status,
processContext->process, PsGetCurrentProcess());
SarReleaseEndpointAndContext(endpoint);
return status; // TODO: goto err_out
}
buffer->ActualBufferSize = actualSize;
buffer->BufferAddress = mappedAddress;
buffer->CallMemoryBarrier = FALSE;
SarReleaseEndpointAndContext(endpoint);
return STATUS_SUCCESS;
}
NTSTATUS SarKsPinRtGetBuffer(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
PKSRTAUDIO_BUFFER_PROPERTY prop = (PKSRTAUDIO_BUFFER_PROPERTY)request;
PKSRTAUDIO_BUFFER buffer = (PKSRTAUDIO_BUFFER)data;
return SarKsPinRtGetBufferCore(
irp, prop->BaseAddress, prop->RequestedBufferSize, 0, buffer);
}
NTSTATUS SarKsPinRtGetBufferWithNotification(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
PKSRTAUDIO_BUFFER_PROPERTY_WITH_NOTIFICATION prop =
(PKSRTAUDIO_BUFFER_PROPERTY_WITH_NOTIFICATION)request;
PKSRTAUDIO_BUFFER buffer = (PKSRTAUDIO_BUFFER)data;
return SarKsPinRtGetBufferCore(
irp, prop->BaseAddress, prop->RequestedBufferSize,
prop->NotificationCount, buffer);
}
NTSTATUS SarKsPinRtGetClockRegister(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(irp);
UNREFERENCED_PARAMETER(request);
UNREFERENCED_PARAMETER(data);
// The clock register should be a continuously updated register at a fixed frequency.
// We don't have anything like this to map as a register, report it as not implemented.
return STATUS_NOT_IMPLEMENTED;
}
NTSTATUS SarKsPinRtGetHwLatency(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(irp);
UNREFERENCED_PARAMETER(request);
PKSRTAUDIO_HWLATENCY latency = (PKSRTAUDIO_HWLATENCY)data;
SarEndpoint *endpoint = SarGetEndpointFromIrp(irp, TRUE);
if (!endpoint) {
SAR_ERROR("Get endpoint failed");
return STATUS_UNSUCCESSFUL;
}
latency->CodecDelay = latency->ChipsetDelay = latency->FifoSize = 0;
SarReleaseEndpointAndContext(endpoint);
return STATUS_SUCCESS;
}
NTSTATUS SarKsPinRtGetPacketCount(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(irp);
UNREFERENCED_PARAMETER(request);
UNREFERENCED_PARAMETER(data);
return STATUS_NOT_IMPLEMENTED;
}
NTSTATUS SarKsPinRtGetPositionRegister(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(request);
NTSTATUS status;
PKSRTAUDIO_HWREGISTER reg = (PKSRTAUDIO_HWREGISTER)data;
SarEndpoint *endpoint = SarGetEndpointFromIrp(irp, TRUE);
SarEndpointProcessContext *context;
if (!endpoint) {
SAR_ERROR("Get endpoint failed");
return STATUS_UNSUCCESSFUL;
}
status = SarGetOrCreateEndpointProcessContext(
endpoint, PsGetCurrentProcess(), &context);
if (!NT_SUCCESS(status)) {
SarReleaseEndpointAndContext(endpoint);
return status;
}
reg->Register =
&context->registerFileUVA[endpoint->index].positionRegister;
reg->Width = 32;
reg->Accuracy = endpoint->owner->periodSizeBytes * endpoint->activeChannelCount;
reg->Numerator = 0;
reg->Denominator = 0;
SarReleaseEndpointAndContext(endpoint);
return STATUS_SUCCESS;
}
NTSTATUS SarKsPinRtGetPresentationPosition(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(irp);
UNREFERENCED_PARAMETER(request);
UNREFERENCED_PARAMETER(data);
return STATUS_NOT_IMPLEMENTED;
}
NTSTATUS SarKsPinRtQueryNotificationSupport(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(irp);
UNREFERENCED_PARAMETER(request);
*((BOOL *)data) = TRUE;
return STATUS_SUCCESS;
}
NTSTATUS SarKsPinRtRegisterNotificationEvent(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(data);
SarEndpoint *endpoint = SarGetEndpointFromIrp(irp, TRUE);
SarEndpointRegisters regs;
NTSTATUS status;
PKSRTAUDIO_NOTIFICATION_EVENT_PROPERTY prop =
(PKSRTAUDIO_NOTIFICATION_EVENT_PROPERTY)request;
if (!endpoint) {
SAR_ERROR("Get endpoint failed");
return STATUS_UNSUCCESSFUL;
}
status = SarReadEndpointRegisters(®s, endpoint);
if (!NT_SUCCESS(status)) {
SAR_ERROR("Read endpoint registers failed %08X", status);
SarReleaseEndpointAndContext(endpoint);
return status;
}
ULONG64 associatedData = regs.generation | ((ULONG64)endpoint->index << 32);
status = SarPostHandleQueue(&endpoint->owner->handleQueue,
prop->NotificationEvent, associatedData);
SarReleaseEndpointAndContext(endpoint);
return status;
}
NTSTATUS SarKsPinRtUnregisterNotificationEvent(
PIRP irp, PKSIDENTIFIER request, PVOID data)
{
UNREFERENCED_PARAMETER(irp);
UNREFERENCED_PARAMETER(request);
UNREFERENCED_PARAMETER(data);
return STATUS_NOT_IMPLEMENTED;
}