[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