Skip to content

Commit 70e4d79

Browse files
committed
Put evil version hack in driver instead of userspace. Re-fixes #2
1 parent fdbae3a commit 70e4d79

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

SarAsio/sarclient.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,13 @@ bool SarClient::createEndpoints()
361361

362362
for (auto& endpoint : _driverConfig.endpoints) {
363363
SarCreateEndpointRequest request = {};
364-
std::wostringstream wos;
365364

366-
wos << UTF8ToWide(endpoint.id) << L"_"
367-
<< endpoint.channelCount << L"_"
368-
<< _bufferConfig.sampleRate << L"_"
369-
<< _bufferConfig.sampleSize;
370-
std::wstring idstr = wos.str();
371365
request.type = endpoint.type == EndpointType::Playback ?
372366
SAR_ENDPOINT_TYPE_PLAYBACK : SAR_ENDPOINT_TYPE_RECORDING;
373367
request.channelCount = endpoint.channelCount;
374368
request.index = i++;
375369
wcscpy_s(request.name, UTF8ToWide(endpoint.description).c_str());
376-
wcscpy_s(request.id, idstr.c_str());
370+
wcscpy_s(request.id, UTF8ToWide(endpoint.id).c_str());
377371

378372
if (!DeviceIoControl(_device, SAR_CREATE_ENDPOINT,
379373
(LPVOID)&request, sizeof(request), nullptr, 0, &dummy, nullptr)) {

SynchronousAudioRouter/control.cpp

+31-5
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ NTSTATUS SarCreateEndpoint(
525525
NTSTATUS status = STATUS_SUCCESS;
526526
PKSDEVICE ksDevice = KsGetDeviceForDeviceObject(device);
527527
BOOLEAN deviceNameAllocated = FALSE, deviceIdAllocated = FALSE;
528+
RTL_OSVERSIONINFOW versionInfo = {};
528529
SarEndpoint *endpoint;
529530

530531
if (request->type != SAR_ENDPOINT_TYPE_RECORDING &&
@@ -679,18 +680,43 @@ NTSTATUS SarCreateEndpoint(
679680
request->name[MAX_ENDPOINT_NAME_LENGTH] = '\0';
680681
request->id[MAX_ENDPOINT_NAME_LENGTH] = '\0';
681682
RtlInitUnicodeString(&endpoint->deviceName, request->name);
682-
RtlInitUnicodeString(&endpoint->deviceId, request->id);
683-
status = SarStringDuplicate(&endpoint->deviceName, &endpoint->deviceName);
683+
status = SarStringDuplicate(
684+
&endpoint->deviceName, &endpoint->deviceName);
684685

685686
if (!NT_SUCCESS(status)) {
686687
goto err_out;
687688
}
688689

689690
deviceNameAllocated = TRUE;
690-
status = SarStringDuplicate(&endpoint->deviceId, &endpoint->deviceId);
691691

692-
if (!NT_SUCCESS(status)) {
693-
goto err_out;
692+
// Windows 10 introduces a 'format cache' which at this time appears to
693+
// not fully invalidate itself when KSEVENT_PINCAPS_FORMATCHANGE is
694+
// sent. Work around this by encoding information about the sample rate,
695+
// channel count and sample resolution into the endpoint ID. This means
696+
// that WASAPI endpoints won't be stable across changes to these
697+
// parameters on Windows 10 and e.g. applications that store them as
698+
// configuration may lose track of them, but that seems better than "it
699+
// fully stops working until you reinstall the driver".
700+
RtlGetVersion(&versionInfo);
701+
702+
if (versionInfo.dwMajorVersion >= 10) {
703+
DECLARE_UNICODE_STRING_SIZE(deviceIdBuffer, 256);
704+
705+
RtlUnicodeStringPrintf(&deviceIdBuffer,
706+
L"%ws_%d_%d_%d", request->id, request->channelCount,
707+
controlContext->sampleRate, controlContext->sampleSize);
708+
status = SarStringDuplicate(&endpoint->deviceId, &deviceIdBuffer);
709+
710+
if (!NT_SUCCESS(status)) {
711+
goto err_out;
712+
}
713+
} else {
714+
RtlInitUnicodeString(&endpoint->deviceId, request->id);
715+
status = SarStringDuplicate(&endpoint->deviceId, &endpoint->deviceId);
716+
717+
if (!NT_SUCCESS(status)) {
718+
goto err_out;
719+
}
694720
}
695721

696722
deviceIdAllocated = TRUE;

0 commit comments

Comments
 (0)