[client] windows: add WASAPI audio support

This commit is contained in:
Geoffrey McRae
2026-09-12 12:51:49 +10:00
parent eabc56a6de
commit ca0738ec0d
11 changed files with 2296 additions and 1 deletions

View File

@@ -114,6 +114,34 @@ if(LG_CLIENT_PLATFORM_LINUX)
)
endif()
add_executable(wasapi-format-tests
wasapi_format_test.c
../audiodevs/WASAPI/format.c
)
target_include_directories(wasapi-format-tests PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../audiodevs/WASAPI"
)
add_test(NAME wasapi-format-tests
COMMAND wasapi-format-tests
)
set_tests_properties(wasapi-format-tests PROPERTIES
TIMEOUT 10
)
add_executable(wasapi-capture-queue-tests
wasapi_capture_queue_test.c
../audiodevs/WASAPI/capture_queue.c
)
target_include_directories(wasapi-capture-queue-tests PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../audiodevs/WASAPI"
)
add_test(NAME wasapi-capture-queue-tests
COMMAND wasapi-capture-queue-tests
)
set_tests_properties(wasapi-capture-queue-tests PROPERTIES
TIMEOUT 10
)
if(LG_CLIENT_PLATFORM_WINDOWS)
add_executable(common-windows-tests
common_windows_test.c

View File

@@ -0,0 +1,121 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* 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 2 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 this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "capture_queue.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define TEST_ASSERT(expr) \
do \
{ \
if (!(expr)) \
{ \
fprintf(stderr, "%s:%d: assertion failed: %s\n", \
__FILE__, __LINE__, #expr); \
return 1; \
} \
} \
while (0)
static int testQueue(void)
{
WasapiCaptureQueue queue = { 0 };
TEST_ASSERT(!wasapi_captureQueueInit(NULL, 2, 4));
TEST_ASSERT(!wasapi_captureQueueInit(&queue, 0, 4));
TEST_ASSERT(!wasapi_captureQueueInit(&queue, 2, 0));
TEST_ASSERT(wasapi_captureQueueInit(&queue, 2, 4));
const uint8_t first[] = { 1, 2, 3, 4 };
const uint8_t second[] = { 5, 6, 7, 8 };
const uint8_t third[] = { 9, 10, 11, 12 };
const LG_AudioClock clock =
{
.position = 42,
.time = 1000,
.rate = 0.0,
.stable = true,
.discontinuity = false
};
TEST_ASSERT(!wasapi_captureQueueBegin(&queue, sizeof(first) + 1));
TEST_ASSERT(!wasapi_captureQueuePush(
&queue, first, sizeof(first) + 1, 2, &clock));
TEST_ASSERT(!wasapi_captureQueuePush(
&queue, NULL, sizeof(first), 2, &clock));
uint8_t * destination = wasapi_captureQueueBegin(&queue, sizeof(first));
TEST_ASSERT(destination);
memcpy(destination, first, sizeof(first));
TEST_ASSERT(!wasapi_captureQueueFront(&queue));
wasapi_captureQueueCommit(&queue, sizeof(first), 2, &clock);
TEST_ASSERT(wasapi_captureQueuePush(
&queue, second, sizeof(second), 2, NULL));
TEST_ASSERT(!wasapi_captureQueuePush(
&queue, third, sizeof(third), 2, &clock));
const WasapiCapturePacket * packet = wasapi_captureQueueFront(&queue);
TEST_ASSERT(packet);
TEST_ASSERT(packet->bytes == sizeof(first));
TEST_ASSERT(packet->frames == 2);
TEST_ASSERT(packet->clockValid);
TEST_ASSERT(packet->clock.position == clock.position);
TEST_ASSERT(packet->clock.time == clock.time);
TEST_ASSERT(packet->clock.rate == 0.0);
TEST_ASSERT(packet->clock.stable);
TEST_ASSERT(!packet->clock.discontinuity);
TEST_ASSERT(memcmp(packet->data, first, sizeof(first)) == 0);
wasapi_captureQueuePop(&queue);
TEST_ASSERT(wasapi_captureQueuePush(
&queue, third, sizeof(third), 2, &clock));
packet = wasapi_captureQueueFront(&queue);
TEST_ASSERT(packet);
TEST_ASSERT(!packet->clockValid);
TEST_ASSERT(memcmp(packet->data, second, sizeof(second)) == 0);
wasapi_captureQueuePop(&queue);
packet = wasapi_captureQueueFront(&queue);
TEST_ASSERT(packet);
TEST_ASSERT(packet->clockValid);
TEST_ASSERT(memcmp(packet->data, third, sizeof(third)) == 0);
wasapi_captureQueuePop(&queue);
TEST_ASSERT(!wasapi_captureQueueFront(&queue));
TEST_ASSERT(wasapi_captureQueuePush(
&queue, first, sizeof(first), 2, &clock));
wasapi_captureQueueReset(&queue);
TEST_ASSERT(!wasapi_captureQueueFront(&queue));
TEST_ASSERT(wasapi_captureQueuePush(
&queue, second, sizeof(second), 2, NULL));
wasapi_captureQueueFree(&queue);
TEST_ASSERT(!queue.packets);
TEST_ASSERT(!queue.data);
wasapi_captureQueueFree(&queue);
return 0;
}
int main(void)
{
return testQueue();
}

View File

@@ -0,0 +1,207 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* 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 2 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 this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "format.h"
#include <inttypes.h>
#include <stdio.h>
#define TEST_ASSERT(expr) \
do \
{ \
if (!(expr)) \
{ \
fprintf(stderr, "%s:%d: assertion failed: %s\n", \
__FILE__, __LINE__, #expr); \
return 1; \
} \
} \
while (0)
static int testSampleSizes(void)
{
TEST_ASSERT(wasapi_sampleSize(LG_AUDIO_FMT_U8 ) == 1);
TEST_ASSERT(wasapi_sampleSize(LG_AUDIO_FMT_S16_LE) == 2);
TEST_ASSERT(wasapi_sampleSize(LG_AUDIO_FMT_S24_LE) == 3);
TEST_ASSERT(wasapi_sampleSize(LG_AUDIO_FMT_S32_LE) == 4);
TEST_ASSERT(wasapi_sampleSize(LG_AUDIO_FMT_F32_LE) == 4);
TEST_ASSERT(wasapi_sampleSize(LG_AUDIO_FMT_F32_NE) == 4);
TEST_ASSERT(wasapi_sampleSize(LG_AUDIO_FMT_F64_LE) == 8);
return 0;
}
static int testChannelMasks(void)
{
LG_AudioFormat stereo =
{
.channelCount = 2,
.channels =
{
LG_AUDIO_CH_FRONT_LEFT,
LG_AUDIO_CH_FRONT_RIGHT
}
};
TEST_ASSERT(wasapi_channelMask(&stereo) == UINT32_C(0x3));
LG_AudioFormat surround =
{
.channelCount = 6,
.channels =
{
LG_AUDIO_CH_FRONT_LEFT,
LG_AUDIO_CH_FRONT_RIGHT,
LG_AUDIO_CH_FRONT_CENTER,
LG_AUDIO_CH_LFE,
LG_AUDIO_CH_REAR_LEFT,
LG_AUDIO_CH_REAR_RIGHT
}
};
TEST_ASSERT(wasapi_channelMask(&surround) == UINT32_C(0x3f));
LG_AudioFormat mono =
{
.channelCount = 1,
.channels = { LG_AUDIO_CH_MONO }
};
TEST_ASSERT(wasapi_channelMask(&mono) == UINT32_C(0x4));
stereo.channels[0] = LG_AUDIO_CH_UNKNOWN;
TEST_ASSERT(wasapi_channelMask(&stereo) == 0);
stereo.channels[0] = LG_AUDIO_CH_FRONT_RIGHT;
stereo.channels[1] = LG_AUDIO_CH_FRONT_LEFT;
TEST_ASSERT(wasapi_channelMask(&stereo) == 0);
stereo.channels[0] = LG_AUDIO_CH_FRONT_LEFT;
stereo.channels[1] = LG_AUDIO_CH_FRONT_LEFT;
TEST_ASSERT(wasapi_channelMask(&stereo) == 0);
stereo.channels[1] = LG_AUDIO_CH_MONO;
TEST_ASSERT(wasapi_channelMask(&stereo) == 0);
return 0;
}
static int testFormatInfo(void)
{
LG_AudioFormat format =
{
.sampleRate = 48000,
.sampleFormat = LG_AUDIO_FMT_S16_LE,
.channelCount = 2,
.channels =
{
LG_AUDIO_CH_FRONT_LEFT,
LG_AUDIO_CH_FRONT_RIGHT
}
};
WasapiFormatInfo info;
TEST_ASSERT(wasapi_formatInfo(&format, &info));
TEST_ASSERT(info.blockAlign == 4);
TEST_ASSERT(info.bytesPerSecond == 192000);
TEST_ASSERT(info.bitsPerSample == 16);
TEST_ASSERT(info.channelMask == UINT32_C(0x3));
TEST_ASSERT(info.sampleType == WASAPI_SAMPLE_TYPE_PCM);
format.sampleFormat = LG_AUDIO_FMT_F32_LE;
TEST_ASSERT(wasapi_formatInfo(&format, &info));
TEST_ASSERT(info.blockAlign == 8);
TEST_ASSERT(info.bytesPerSecond == 384000);
TEST_ASSERT(info.bitsPerSample == 32);
TEST_ASSERT(info.sampleType == WASAPI_SAMPLE_TYPE_FLOAT);
TEST_ASSERT(!wasapi_formatInfo(NULL, &info));
TEST_ASSERT(!wasapi_formatInfo(&format, NULL));
format.sampleRate = 0;
TEST_ASSERT(!wasapi_formatInfo(&format, &info));
format.sampleRate = 48000;
format.channelCount = 0;
TEST_ASSERT(!wasapi_formatInfo(&format, &info));
format.channelCount = LG_AUDIO_MAX_CHANNELS + 1;
TEST_ASSERT(!wasapi_formatInfo(&format, &info));
format.channelCount = 2;
format.channels[0] = LG_AUDIO_CH_UNKNOWN;
TEST_ASSERT(!wasapi_formatInfo(&format, &info));
format.channels[0] = LG_AUDIO_CH_FRONT_RIGHT;
format.channels[1] = LG_AUDIO_CH_FRONT_LEFT;
TEST_ASSERT(!wasapi_formatInfo(&format, &info));
format.channels[0] = LG_AUDIO_CH_FRONT_LEFT;
format.channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
format.sampleFormat = (LG_AudioSampleFormat)-1;
TEST_ASSERT(!wasapi_formatInfo(&format, &info));
format.sampleFormat = LG_AUDIO_FMT_F64_LE;
format.sampleRate = UINT32_MAX;
TEST_ASSERT(!wasapi_formatInfo(&format, &info));
return 0;
}
static int testControlPending(void)
{
WasapiControlPending state =
{
.volume = true,
.mute = true,
};
WasapiControlPending pending = wasapi_controlPendingTake(&state);
TEST_ASSERT(pending.volume);
TEST_ASSERT(pending.mute);
TEST_ASSERT(!state.volume);
TEST_ASSERT(!state.mute);
pending = wasapi_controlPendingTake(&state);
TEST_ASSERT(!pending.volume);
TEST_ASSERT(!pending.mute);
state.volume = true;
state.mute = true;
wasapi_controlPendingReset(&state);
TEST_ASSERT(!state.volume);
TEST_ASSERT(!state.mute);
return 0;
}
static int testPeriods(void)
{
TEST_ASSERT(wasapi_framesToDuration(480, 48000) == UINT64_C(100000));
TEST_ASSERT(wasapi_framesToDuration(1, 48000) == UINT64_C(209));
TEST_ASSERT(wasapi_framesToDuration(1, 0) == 0);
return 0;
}
static int testBufferMath(void)
{
TEST_ASSERT(wasapi_availableFrames(1024, 256) == 768);
TEST_ASSERT(wasapi_availableFrames(1024, 1024) == 0);
TEST_ASSERT(wasapi_availableFrames(1024, 2048) == 0);
TEST_ASSERT(wasapi_silenceByte(LG_AUDIO_FMT_U8) == UINT8_C(0x80));
TEST_ASSERT(wasapi_silenceByte(LG_AUDIO_FMT_S16_LE) == 0);
return 0;
}
int main(void)
{
if (testSampleSizes() || testChannelMasks() || testFormatInfo() ||
testControlPending() || testPeriods() || testBufferMath())
return 1;
return 0;
}