From e6bd36ec7caea03e8915e8a52f16a2a1769fe7b6 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 6 Jan 2022 22:47:22 +1100 Subject: [PATCH] [client] audio: refactor `audio` to `playback` and add `record` funcs --- client/audiodevs/PipeWire/pipewire.c | 13 ++++-- client/audiodevs/PulseAudio/pulseaudio.c | 13 ++++-- client/include/interface/audiodev.h | 59 +++++++++++++++--------- client/src/main.c | 42 ++++++++--------- client/src/main.h | 2 +- 5 files changed, 75 insertions(+), 54 deletions(-) diff --git a/client/audiodevs/PipeWire/pipewire.c b/client/audiodevs/PipeWire/pipewire.c index cb50a625..174f6daf 100644 --- a/client/audiodevs/PipeWire/pipewire.c +++ b/client/audiodevs/PipeWire/pipewire.c @@ -264,9 +264,12 @@ struct LG_AudioDevOps LGAD_PipeWire = .name = "PipeWire", .init = pipewire_init, .free = pipewire_free, - .start = pipewire_start, - .play = pipewire_play, - .stop = pipewire_stop, - .volume = pipewire_volume, - .mute = pipewire_mute + .playback = + { + .start = pipewire_start, + .play = pipewire_play, + .stop = pipewire_stop, + .volume = pipewire_volume, + .mute = pipewire_mute + } }; diff --git a/client/audiodevs/PulseAudio/pulseaudio.c b/client/audiodevs/PulseAudio/pulseaudio.c index 723c904f..3b0dfefc 100644 --- a/client/audiodevs/PulseAudio/pulseaudio.c +++ b/client/audiodevs/PulseAudio/pulseaudio.c @@ -346,9 +346,12 @@ struct LG_AudioDevOps LGAD_PulseAudio = .name = "PulseAudio", .init = pulseaudio_init, .free = pulseaudio_free, - .start = pulseaudio_start, - .play = pulseaudio_play, - .stop = pulseaudio_stop, - .volume = pulseaudio_volume, - .mute = pulseaudio_mute + .playback = + { + .start = pulseaudio_start, + .play = pulseaudio_play, + .stop = pulseaudio_stop, + .volume = pulseaudio_volume, + .mute = pulseaudio_mute + } }; diff --git a/client/include/interface/audiodev.h b/client/include/interface/audiodev.h index 00449365..a573b67a 100644 --- a/client/include/interface/audiodev.h +++ b/client/include/interface/audiodev.h @@ -38,32 +38,47 @@ struct LG_AudioDevOps /* final free */ void (*free)(void); - /* setup the playback audio stream - * Note: currently SPICE only supports S16 samples so always assume so - */ - void (*start)(int channels, int sampleRate); + struct + { + /* start the playback audio stream + * Note: currently SPICE only supports S16 samples so always assume so + */ + void (*start)(int channels, int sampleRate); - /* called for each packet of output audio to play - * Note: size is the size of data in bytes, not frames/samples - */ - void (*play)(uint8_t * data, int size); + /* called for each packet of output audio to play + * Note: size is the size of data in bytes, not frames/samples + */ + void (*play)(uint8_t * data, int size); - /* called when SPICE reports the audio stream has stopped */ - void (*stop)(void); + /* called when SPICE reports the audio stream has stopped */ + void (*stop)(void); - /* [optional] called to set the volume of the channels */ - void (*volume)(int channels, const uint16_t volume[]); + /* [optional] called to set the volume of the channels */ + void (*volume)(int channels, const uint16_t volume[]); - /* [optional] called to set muting of the output */ - void (*mute)(bool mute); + /* [optional] called to set muting of the output */ + void (*mute)(bool mute); + } + playback; + + struct + { + /* start the record stream + * Note: currently SPICE only supports S16 samples so always assume so + */ + void (*start)(int channels, int sampleRate, + void (*dataFn)(uint8_t * data, int size)); + + /* called when SPICE reports the audio stream has stopped */ + void (*stop)(void); + + /* [optional] called to set the volume of the channels */ + void (*volume)(int channels, const uint16_t volume[]); + + /* [optional] called to set muting of the input */ + void (*mute)(bool mute); + } + record; }; -#define ASSERT_LG_AUDIODEV_VALID(x) \ - DEBUG_ASSERT((x)->name ); \ - DEBUG_ASSERT((x)->init ); \ - DEBUG_ASSERT((x)->free ); \ - DEBUG_ASSERT((x)->start ); \ - DEBUG_ASSERT((x)->play ); \ - DEBUG_ASSERT((x)->stop ); - #endif diff --git a/client/src/main.c b/client/src/main.c index 87b7a1af..b73e8305 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -765,7 +765,7 @@ int main_frameThread(void * unused) return 0; } -void audioStart(int channels, int sampleRate, PSAudioFormat format, +void playbackStart(int channels, int sampleRate, PSAudioFormat format, uint32_t time) { /* @@ -795,46 +795,46 @@ void audioStart(int channels, int sampleRate, PSAudioFormat format, static int lastChannels = 0; static int lastSampleRate = 0; - if (g_state.audioStarted) + if (g_state.playbackStarted) { if (channels != lastChannels || sampleRate != lastSampleRate) - g_state.audioDev->stop(); + g_state.audioDev->playback.stop(); else return; } lastChannels = channels; lastSampleRate = sampleRate; - g_state.audioStarted = true; + g_state.playbackStarted = true; DEBUG_INFO("%d channels @ %dHz", channels, sampleRate); - g_state.audioDev->start(channels, sampleRate); + g_state.audioDev->playback.start(channels, sampleRate); } } -static void audioStop(void) +static void playbackStop(void) { if (g_state.audioDev) - g_state.audioDev->stop(); - g_state.audioStarted = false; + g_state.audioDev->playback.stop(); + g_state.playbackStarted = false; } -static void audioVolume(int channels, const uint16_t volume[]) +static void playbackVolume(int channels, const uint16_t volume[]) { - if (g_state.audioDev && g_state.audioDev->volume) - g_state.audioDev->volume(channels, volume); + if (g_state.audioDev && g_state.audioDev->playback.volume) + g_state.audioDev->playback.volume(channels, volume); } -static void audioMute(bool mute) +static void playbackMute(bool mute) { - if (g_state.audioDev && g_state.audioDev->mute) - g_state.audioDev->mute(mute); + if (g_state.audioDev && g_state.audioDev->playback.mute) + g_state.audioDev->playback.mute(mute); } -static void audioData(uint8_t * data, size_t size) +static void playbackData(uint8_t * data, size_t size) { if (g_state.audioDev) - g_state.audioDev->play(data, size); + g_state.audioDev->playback.play(data, size); } static void checkUUID(void) @@ -900,11 +900,11 @@ int spiceThread(void * arg) .playback = { .enable = g_params.useSpiceAudio, - .start = audioStart, - .volume = audioVolume, - .mute = audioMute, - .stop = audioStop, - .data = audioData + .start = playbackStart, + .volume = playbackVolume, + .mute = playbackMute, + .stop = playbackStop, + .data = playbackData } }; diff --git a/client/src/main.h b/client/src/main.h index 6fc8c7fc..74254992 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -141,7 +141,7 @@ struct AppState bool autoIdleInhibitState; struct LG_AudioDevOps * audioDev; - bool audioStarted; + bool playbackStarted; }; struct AppParams