[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

@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.10)
project(audiodev_WASAPI LANGUAGES C)
add_library(audiodev_WASAPI STATIC
capture_queue.c
format.c
guids.c
wasapi.c
)
target_link_libraries(audiodev_WASAPI
avrt
lg_common
m
ole32
uuid
)
target_include_directories(audiodev_WASAPI
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}"
)

View File

@@ -0,0 +1,145 @@
/**
* 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 <stdlib.h>
#include <string.h>
bool wasapi_captureQueueInit(WasapiCaptureQueue * queue, size_t capacity,
size_t slotSize)
{
if (!queue || !capacity || !slotSize || capacity == SIZE_MAX)
return false;
const size_t slotCount = capacity + 1;
if (slotCount > SIZE_MAX / sizeof(*queue->packets) ||
slotCount > SIZE_MAX / slotSize)
return false;
WasapiCapturePacket * packets = calloc(slotCount, sizeof(*packets));
uint8_t * data = malloc(slotCount * slotSize);
if (!packets || !data)
{
free(data);
free(packets);
return false;
}
for (size_t i = 0; i < slotCount; ++i)
packets[i].data = data + i * slotSize;
queue->packets = packets;
queue->data = data;
queue->slotSize = slotSize;
queue->slotCount = slotCount;
atomic_init(&queue->head, 0);
atomic_init(&queue->tail, 0);
return true;
}
void wasapi_captureQueueFree(WasapiCaptureQueue * queue)
{
if (!queue)
return;
free(queue->data);
free(queue->packets);
*queue = (WasapiCaptureQueue) { 0 };
}
void wasapi_captureQueueReset(WasapiCaptureQueue * queue)
{
if (!queue)
return;
atomic_store_explicit(&queue->head, 0, memory_order_relaxed);
atomic_store_explicit(&queue->tail, 0, memory_order_relaxed);
}
bool wasapi_captureQueuePush(WasapiCaptureQueue * queue,
const uint8_t * data, size_t bytes, int frames,
const LG_AudioClock * clock)
{
if (!data || frames < 0)
return false;
uint8_t * destination = wasapi_captureQueueBegin(queue, bytes);
if (!destination)
return false;
memcpy(destination, data, bytes);
wasapi_captureQueueCommit(queue, bytes, frames, clock);
return true;
}
uint8_t * wasapi_captureQueueBegin(WasapiCaptureQueue * queue, size_t bytes)
{
if (!queue || !queue->packets || bytes > queue->slotSize)
return NULL;
const size_t head = atomic_load_explicit(
&queue->head, memory_order_relaxed);
const size_t next = (head + 1) % queue->slotCount;
if (next == atomic_load_explicit(&queue->tail, memory_order_acquire))
return NULL;
return queue->packets[head].data;
}
void wasapi_captureQueueCommit(WasapiCaptureQueue * queue, size_t bytes,
int frames, const LG_AudioClock * clock)
{
const size_t head = atomic_load_explicit(
&queue->head, memory_order_relaxed);
WasapiCapturePacket * packet = &queue->packets[head];
packet->bytes = bytes;
packet->frames = frames;
packet->clockValid = clock != NULL;
packet->clock = clock ? *clock : (LG_AudioClock) { 0 };
atomic_store_explicit(&queue->head,
(head + 1) % queue->slotCount, memory_order_release);
}
const WasapiCapturePacket * wasapi_captureQueueFront(
WasapiCaptureQueue * queue)
{
if (!queue || !queue->packets)
return NULL;
const size_t tail = atomic_load_explicit(
&queue->tail, memory_order_relaxed);
if (tail == atomic_load_explicit(&queue->head, memory_order_acquire))
return NULL;
return &queue->packets[tail];
}
void wasapi_captureQueuePop(WasapiCaptureQueue * queue)
{
if (!queue || !queue->packets)
return;
const size_t tail = atomic_load_explicit(
&queue->tail, memory_order_relaxed);
atomic_store_explicit(
&queue->tail, (tail + 1) % queue->slotCount, memory_order_release);
}

View File

@@ -0,0 +1,73 @@
/**
* 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
*/
#ifndef _H_LG_WASAPI_CAPTURE_QUEUE_
#define _H_LG_WASAPI_CAPTURE_QUEUE_
#include "interface/audio.h"
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef struct WasapiCapturePacket
{
uint8_t * data;
size_t bytes;
int frames;
bool clockValid;
LG_AudioClock clock;
}
WasapiCapturePacket;
typedef struct WasapiCaptureQueue
{
WasapiCapturePacket * packets;
uint8_t * data;
size_t slotSize;
size_t slotCount;
_Atomic(size_t) head;
_Atomic(size_t) tail;
}
WasapiCaptureQueue;
bool wasapi_captureQueueInit (
WasapiCaptureQueue * queue, size_t capacity,
size_t slotSize);
void wasapi_captureQueueFree (
WasapiCaptureQueue * queue);
void wasapi_captureQueueReset (
WasapiCaptureQueue * queue);
uint8_t * wasapi_captureQueueBegin (
WasapiCaptureQueue * queue, size_t bytes);
void wasapi_captureQueueCommit (
WasapiCaptureQueue * queue, size_t bytes, int frames,
const LG_AudioClock * clock);
bool wasapi_captureQueuePush (
WasapiCaptureQueue * queue,
const uint8_t * data, size_t bytes, int frames,
const LG_AudioClock * clock);
const WasapiCapturePacket * wasapi_captureQueueFront (
WasapiCaptureQueue * queue);
void wasapi_captureQueuePop (
WasapiCaptureQueue * queue);
#endif

View File

@@ -0,0 +1,210 @@
/**
* 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 <limits.h>
#define WASAPI_TICKS_PER_SECOND UINT64_C(10000000)
#define WASAPI_SPEAKER_FRONT_LEFT UINT32_C(0x00000001)
#define WASAPI_SPEAKER_FRONT_RIGHT UINT32_C(0x00000002)
#define WASAPI_SPEAKER_FRONT_CENTER UINT32_C(0x00000004)
#define WASAPI_SPEAKER_LOW_FREQUENCY UINT32_C(0x00000008)
#define WASAPI_SPEAKER_BACK_LEFT UINT32_C(0x00000010)
#define WASAPI_SPEAKER_BACK_RIGHT UINT32_C(0x00000020)
#define WASAPI_SPEAKER_FRONT_LEFT_OF_CENTER UINT32_C(0x00000040)
#define WASAPI_SPEAKER_FRONT_RIGHT_OF_CENTER UINT32_C(0x00000080)
#define WASAPI_SPEAKER_BACK_CENTER UINT32_C(0x00000100)
#define WASAPI_SPEAKER_SIDE_LEFT UINT32_C(0x00000200)
#define WASAPI_SPEAKER_SIDE_RIGHT UINT32_C(0x00000400)
#define WASAPI_SPEAKER_TOP_CENTER UINT32_C(0x00000800)
#define WASAPI_SPEAKER_TOP_FRONT_LEFT UINT32_C(0x00001000)
#define WASAPI_SPEAKER_TOP_FRONT_CENTER UINT32_C(0x00002000)
#define WASAPI_SPEAKER_TOP_FRONT_RIGHT UINT32_C(0x00004000)
#define WASAPI_SPEAKER_TOP_BACK_LEFT UINT32_C(0x00008000)
#define WASAPI_SPEAKER_TOP_BACK_CENTER UINT32_C(0x00010000)
#define WASAPI_SPEAKER_TOP_BACK_RIGHT UINT32_C(0x00020000)
void wasapi_controlPendingReset(WasapiControlPending * state)
{
*state = (WasapiControlPending) { 0 };
}
WasapiControlPending wasapi_controlPendingTake(WasapiControlPending * state)
{
const WasapiControlPending pending = *state;
wasapi_controlPendingReset(state);
return pending;
}
int wasapi_sampleSize(LG_AudioSampleFormat format)
{
switch (format)
{
case LG_AUDIO_FMT_U8 : return 1;
case LG_AUDIO_FMT_S16_LE : return 2;
case LG_AUDIO_FMT_S24_LE : return 3;
case LG_AUDIO_FMT_S32_LE :
case LG_AUDIO_FMT_F32_LE :
case LG_AUDIO_FMT_F32_NE : return 4;
case LG_AUDIO_FMT_F64_LE : return 8;
}
return 0;
}
static uint32_t wasapi_channelBit(LG_AudioChannel channel)
{
switch (channel)
{
case LG_AUDIO_CH_UNKNOWN : return 0;
case LG_AUDIO_CH_MONO :
case LG_AUDIO_CH_FRONT_CENTER :
return WASAPI_SPEAKER_FRONT_CENTER;
case LG_AUDIO_CH_FRONT_LEFT :
return WASAPI_SPEAKER_FRONT_LEFT;
case LG_AUDIO_CH_FRONT_RIGHT :
return WASAPI_SPEAKER_FRONT_RIGHT;
case LG_AUDIO_CH_LFE :
return WASAPI_SPEAKER_LOW_FREQUENCY;
case LG_AUDIO_CH_REAR_LEFT :
return WASAPI_SPEAKER_BACK_LEFT;
case LG_AUDIO_CH_REAR_RIGHT :
return WASAPI_SPEAKER_BACK_RIGHT;
case LG_AUDIO_CH_FRONT_LEFT_CENTER :
return WASAPI_SPEAKER_FRONT_LEFT_OF_CENTER;
case LG_AUDIO_CH_FRONT_RIGHT_CENTER :
return WASAPI_SPEAKER_FRONT_RIGHT_OF_CENTER;
case LG_AUDIO_CH_REAR_CENTER :
return WASAPI_SPEAKER_BACK_CENTER;
case LG_AUDIO_CH_SIDE_LEFT :
return WASAPI_SPEAKER_SIDE_LEFT;
case LG_AUDIO_CH_SIDE_RIGHT :
return WASAPI_SPEAKER_SIDE_RIGHT;
case LG_AUDIO_CH_TOP_CENTER :
return WASAPI_SPEAKER_TOP_CENTER;
case LG_AUDIO_CH_TOP_FRONT_LEFT :
return WASAPI_SPEAKER_TOP_FRONT_LEFT;
case LG_AUDIO_CH_TOP_FRONT_CENTER :
return WASAPI_SPEAKER_TOP_FRONT_CENTER;
case LG_AUDIO_CH_TOP_FRONT_RIGHT :
return WASAPI_SPEAKER_TOP_FRONT_RIGHT;
case LG_AUDIO_CH_TOP_REAR_LEFT :
return WASAPI_SPEAKER_TOP_BACK_LEFT;
case LG_AUDIO_CH_TOP_REAR_CENTER :
return WASAPI_SPEAKER_TOP_BACK_CENTER;
case LG_AUDIO_CH_TOP_REAR_RIGHT :
return WASAPI_SPEAKER_TOP_BACK_RIGHT;
}
return 0;
}
uint32_t wasapi_channelMask(const LG_AudioFormat * format)
{
if (!format || !format->channelCount ||
format->channelCount > LG_AUDIO_MAX_CHANNELS)
return 0;
if (format->channelCount == 1 &&
format->channels[0] == LG_AUDIO_CH_MONO)
return WASAPI_SPEAKER_FRONT_CENTER;
uint32_t mask = 0;
uint32_t lastBit = 0;
for (uint8_t i = 0; i < format->channelCount; ++i)
{
if (format->channels[i] == LG_AUDIO_CH_MONO)
return 0;
const uint32_t bit = wasapi_channelBit(format->channels[i]);
if (!bit || bit <= lastBit)
return 0;
mask |= bit;
lastBit = bit;
}
return mask;
}
bool wasapi_formatInfo(const LG_AudioFormat * format,
WasapiFormatInfo * info)
{
if (!format || !info || !format->sampleRate || !format->channelCount ||
format->channelCount > LG_AUDIO_MAX_CHANNELS)
return false;
const int sampleSize = wasapi_sampleSize(format->sampleFormat);
const uint32_t channelMask = wasapi_channelMask(format);
const uint32_t blockAlign = (uint32_t)sampleSize * format->channelCount;
const uint64_t bytesPerSecond = (uint64_t)blockAlign * format->sampleRate;
if (!sampleSize || !channelMask || blockAlign > UINT16_MAX ||
bytesPerSecond > UINT32_MAX)
return false;
WasapiSampleType sampleType;
switch (format->sampleFormat)
{
case LG_AUDIO_FMT_U8:
case LG_AUDIO_FMT_S16_LE:
case LG_AUDIO_FMT_S24_LE:
case LG_AUDIO_FMT_S32_LE:
sampleType = WASAPI_SAMPLE_TYPE_PCM;
break;
case LG_AUDIO_FMT_F32_LE:
case LG_AUDIO_FMT_F64_LE:
case LG_AUDIO_FMT_F32_NE:
sampleType = WASAPI_SAMPLE_TYPE_FLOAT;
break;
default:
return false;
}
info->blockAlign = (uint16_t)blockAlign;
info->bytesPerSecond = (uint32_t)bytesPerSecond;
info->bitsPerSample = (uint16_t)(sampleSize * CHAR_BIT);
info->channelMask = channelMask;
info->sampleType = sampleType;
return true;
}
uint64_t wasapi_framesToDuration(uint32_t frames, uint32_t sampleRate)
{
if (!sampleRate)
return 0;
return ((uint64_t)frames * WASAPI_TICKS_PER_SECOND + sampleRate - 1) /
sampleRate;
}
uint32_t wasapi_availableFrames(uint32_t bufferFrames,
uint32_t paddingFrames)
{
return paddingFrames < bufferFrames ? bufferFrames - paddingFrames : 0;
}
uint8_t wasapi_silenceByte(LG_AudioSampleFormat format)
{
return format == LG_AUDIO_FMT_U8 ? UINT8_C(0x80) : UINT8_C(0);
}

View File

@@ -0,0 +1,65 @@
/**
* 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
*/
#ifndef _H_LG_WASAPI_FORMAT_
#define _H_LG_WASAPI_FORMAT_
#include "interface/audio.h"
#include <stdint.h>
typedef enum WasapiSampleType
{
WASAPI_SAMPLE_TYPE_PCM,
WASAPI_SAMPLE_TYPE_FLOAT
}
WasapiSampleType;
typedef struct WasapiFormatInfo
{
uint16_t blockAlign;
uint32_t bytesPerSecond;
uint16_t bitsPerSample;
uint32_t channelMask;
WasapiSampleType sampleType;
}
WasapiFormatInfo;
typedef struct WasapiControlPending
{
bool volume;
bool mute;
}
WasapiControlPending;
void wasapi_controlPendingReset (WasapiControlPending * state);
WasapiControlPending wasapi_controlPendingTake (WasapiControlPending * state);
int wasapi_sampleSize (LG_AudioSampleFormat format);
uint32_t wasapi_channelMask (const LG_AudioFormat * format);
bool wasapi_formatInfo (const LG_AudioFormat * format,
WasapiFormatInfo * info);
uint64_t wasapi_framesToDuration (uint32_t frames,
uint32_t sampleRate);
uint32_t wasapi_availableFrames (uint32_t bufferFrames,
uint32_t paddingFrames);
uint8_t wasapi_silenceByte (LG_AudioSampleFormat format);
#endif

View File

@@ -0,0 +1,24 @@
/**
* 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 <initguid.h>
#include <audioclient.h>
#include <mmdeviceapi.h>

File diff suppressed because it is too large Load Diff

View File

@@ -34,6 +34,7 @@
#include "dynamic/audiodev.h"
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <samplerate.h>
@@ -2307,7 +2308,8 @@ static PlaybackDataResult playbackData(const void * data, size_t frameCount,
PlaybackSourceData * sourceData = &audio.playback.sourceData;
if (!data || frameCount > (size_t)sourceData->maxSourcePacketFrames)
{
DEBUG_ERROR("Invalid playback packet length: %zu frames", frameCount);
DEBUG_ERROR("Invalid playback packet length: %" PRIu64 " frames",
(uint64_t)frameCount);
return PLAYBACK_DATA_DROP;
}
const int frames = frameCount;

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;
}