From a114ea3de4e223f69a11744f4b2076471f07738f Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 6 Jan 2022 23:24:13 +1100 Subject: [PATCH] [client] audio: move audio code into it's own unit --- client/CMakeLists.txt | 1 + client/src/audio.c | 154 ++++++++++++++++++++++++++++++++++++++++++ client/src/audio.h | 29 ++++++++ client/src/main.c | 91 +++---------------------- client/src/main.h | 4 -- 5 files changed, 192 insertions(+), 87 deletions(-) create mode 100644 client/src/audio.c create mode 100644 client/src/audio.h diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 61e3cede..4c1b257e 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -122,6 +122,7 @@ set(SOURCES src/main.c src/core.c src/app.c + src/audio.c src/config.c src/keybind.c src/ll.c diff --git a/client/src/audio.c b/client/src/audio.c new file mode 100644 index 00000000..b11c5c55 --- /dev/null +++ b/client/src/audio.c @@ -0,0 +1,154 @@ +/** + * Looking Glass + * Copyright © 2017-2022 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 "main.h" +#include "dynamic/audiodev.h" + +#include + +typedef struct +{ + struct LG_AudioDevOps * audioDev; + + struct + { + bool started; + int volumeChannels; + uint16_t * volume; + bool mute; + } + playback; +} +AudioState; + +static AudioState audio = { 0 }; + +void audio_init(void) +{ + // search for the best audiodev to use + for(int i = 0; i < LG_AUDIODEV_COUNT; ++i) + if (LG_AudioDevs[i]->init()) + { + audio.audioDev = LG_AudioDevs[i]; + DEBUG_INFO("Using AudioDev: %s", audio.audioDev->name); + return; + } + + DEBUG_WARN("Failed to initialize an audio backend"); +} + +void audio_free(void) +{ + if (!audio.audioDev) + return; + + audio.audioDev->free(); + audio.audioDev = NULL; + + free(audio.playback.volume); + audio.playback.volume = NULL; +} + +void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format, + uint32_t time) +{ + if (!audio.audioDev) + return; + + static int lastChannels = 0; + static int lastSampleRate = 0; + + if (audio.playback.started) + { + if (channels != lastChannels || sampleRate != lastSampleRate) + audio.audioDev->playback.stop(); + else + return; + } + + lastChannels = channels; + lastSampleRate = sampleRate; + audio.playback.started = true; + + DEBUG_INFO("%d channels @ %dHz", channels, sampleRate); + audio.audioDev->playback.start(channels, sampleRate); + + // if a volume level was stored, set it before we return + if (audio.playback.volume) + audio.audioDev->playback.volume( + audio.playback.volumeChannels, + audio.playback.volume); + + // set the inital mute state + audio.audioDev->playback.mute(audio.playback.mute); +} + +void audio_playbackStop(void) +{ + if (!audio.audioDev || !audio.playback.started) + return; + + audio.audioDev->playback.stop(); + audio.playback.started = false; +} + +void audio_playbackVolume(int channels, const uint16_t volume[]) +{ + if (!audio.audioDev || !audio.audioDev->playback.volume) + return; + + // if playback has not started yet, store the volume levels for later + if (!audio.playback.started) + { + if (audio.playback.volumeChannels < channels) + { + free(audio.playback.volume); + audio.playback.volume = malloc(sizeof(uint16_t) * channels); + } + memcpy(audio.playback.volume, volume, sizeof(uint16_t) * channels); + audio.playback.volumeChannels = channels; + return; + } + + audio.audioDev->playback.volume(channels, volume); +} + +void audio_playbackMute(bool mute) +{ + if (!audio.audioDev || !audio.audioDev->playback.mute) + return; + + // if playback has not yet started, store the mute status for later + if (!audio.playback.started) + { + audio.playback.mute = mute; + return; + } + + audio.audioDev->playback.mute(mute); +} + +void audio_playbackData(uint8_t * data, size_t size) +{ + if (!audio.audioDev || !audio.playback.started) + return; + + audio.audioDev->playback.play(data, size); +} diff --git a/client/src/audio.h b/client/src/audio.h new file mode 100644 index 00000000..ed4ae8c6 --- /dev/null +++ b/client/src/audio.h @@ -0,0 +1,29 @@ +/** + * Looking Glass + * Copyright © 2017-2022 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 + */ + +void audio_init(void); +void audio_free(void); + +void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format, + uint32_t time); +void audio_playbackStop(void); +void audio_playbackVolume(int channels, const uint16_t volume[]); +void audio_playbackMute(bool mute); +void audio_playbackData(uint8_t * data, size_t size); diff --git a/client/src/main.c b/client/src/main.c index b73e8305..fa8f5ba6 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -53,6 +53,7 @@ #include "core.h" #include "app.h" +#include "audio.h" #include "keybind.h" #include "clipboard.h" #include "kb.h" @@ -765,78 +766,6 @@ int main_frameThread(void * unused) return 0; } -void playbackStart(int channels, int sampleRate, PSAudioFormat format, - uint32_t time) -{ - /* - * we probe here so that the audiodev is operating in the context of the SPICE - * thread/loop to avoid any audio API threading issues - */ - static int probed = false; - if (!probed) - { - probed = true; - - // search for the best audiodev to use - for(int i = 0; i < LG_AUDIODEV_COUNT; ++i) - if (LG_AudioDevs[i]->init()) - { - g_state.audioDev = LG_AudioDevs[i]; - DEBUG_INFO("Using AudioDev: %s", g_state.audioDev->name); - break; - } - - if (!g_state.audioDev) - DEBUG_WARN("Failed to initialize an audio backend"); - } - - if (g_state.audioDev) - { - static int lastChannels = 0; - static int lastSampleRate = 0; - - if (g_state.playbackStarted) - { - if (channels != lastChannels || sampleRate != lastSampleRate) - g_state.audioDev->playback.stop(); - else - return; - } - - lastChannels = channels; - lastSampleRate = sampleRate; - g_state.playbackStarted = true; - - DEBUG_INFO("%d channels @ %dHz", channels, sampleRate); - g_state.audioDev->playback.start(channels, sampleRate); - } -} - -static void playbackStop(void) -{ - if (g_state.audioDev) - g_state.audioDev->playback.stop(); - g_state.playbackStarted = false; -} - -static void playbackVolume(int channels, const uint16_t volume[]) -{ - if (g_state.audioDev && g_state.audioDev->playback.volume) - g_state.audioDev->playback.volume(channels, volume); -} - -static void playbackMute(bool mute) -{ - if (g_state.audioDev && g_state.audioDev->playback.mute) - g_state.audioDev->playback.mute(mute); -} - -static void playbackData(uint8_t * data, size_t size) -{ - if (g_state.audioDev) - g_state.audioDev->playback.play(data, size); -} - static void checkUUID(void) { if (!g_state.spiceUUIDValid || !g_state.guestUUIDValid) @@ -854,6 +783,7 @@ void spiceReady(void) { // set the intial mouse mode purespice_mouseMode(true); + audio_init(); PSServerInfo info; if (!purespice_getServerInfo(&info)) @@ -900,11 +830,11 @@ int spiceThread(void * arg) .playback = { .enable = g_params.useSpiceAudio, - .start = playbackStart, - .volume = playbackVolume, - .mute = playbackMute, - .stop = playbackStop, - .data = playbackData + .start = audio_playbackStart, + .volume = audio_playbackVolume, + .mute = audio_playbackMute, + .stop = audio_playbackStop, + .data = audio_playbackData } }; @@ -928,12 +858,7 @@ int spiceThread(void * arg) end: - if (g_state.audioDev) - { - g_state.audioDev->free(); - g_state.audioDev = NULL; - } - + audio_free(); g_state.state = APP_STATE_SHUTDOWN; return 0; } diff --git a/client/src/main.h b/client/src/main.h index 74254992..2e609771 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -24,7 +24,6 @@ #include "dynamic/displayservers.h" #include "dynamic/renderers.h" -#include "dynamic/audiodev.h" #include "common/thread.h" #include "common/types.h" @@ -139,9 +138,6 @@ struct AppState bool resizeDone; bool autoIdleInhibitState; - - struct LG_AudioDevOps * audioDev; - bool playbackStarted; }; struct AppParams