[client] audio: move audio code into it's own unit

This commit is contained in:
Geoffrey McRae 2022-01-06 23:24:13 +11:00
parent e6bd36ec7c
commit a114ea3de4
5 changed files with 192 additions and 87 deletions

View File

@ -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

154
client/src/audio.c Normal file
View File

@ -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 <string.h>
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);
}

29
client/src/audio.h Normal file
View File

@ -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);

View File

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

View File

@ -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