Huge refactor to improve memory efficiency mostly around pulseaudio

This commit is contained in:
Paul Dino Jones 2025-03-25 10:04:39 -04:00
parent e0d9c78f60
commit 7afd7f12ed
9 changed files with 60 additions and 75 deletions

View File

@ -11,7 +11,7 @@ endif()
SET_SOURCE_FILES_PROPERTIES( src/monocoque.c PROPERTIES LANGUAGE C) SET_SOURCE_FILES_PROPERTIES( src/monocoque.c PROPERTIES LANGUAGE C)
#set(CMAKE_BUILD_TYPE Debug) set(CMAKE_BUILD_TYPE Debug)
project(monocoque) project(monocoque)
@ -38,14 +38,13 @@ set(HIDAPI_WITH_LIBUSB FALSE) # surely will be used only on Linux
set(BUILD_SHARED_LIBS TRUE) # HIDAPI as static library on all platforms set(BUILD_SHARED_LIBS TRUE) # HIDAPI as static library on all platforms
add_executable(monocoque src/monocoque/monocoque.c) add_executable(monocoque src/monocoque/monocoque.c)
if(USE_PULSEAUDIO)
message("Using pulseaudio backend...") #if(USE_PULSEAUDIO)
add_compile_definitions(USE_PULSEAUDIO=true) #else()
target_link_libraries(monocoque m hidapi-hidraw pulse serialport xml2 argtable2 config gameloop helper devices slog simulatorapi uv xdg-basedir ${LUA_LIBRARY}) #endif()
else() message("Using pulseaudio backend...")
message("Using portaudio backend...") add_compile_definitions(USE_PULSEAUDIO=true)
target_link_libraries(monocoque m hidapi-hidraw portaudio serialport xml2 argtable2 config gameloop helper devices slog simulatorapi uv xdg-basedir ${LUA_LIBRARY}) target_link_libraries(monocoque m hidapi-hidraw pulse serialport xml2 argtable2 config gameloop helper devices slog simulatorapi uv xdg-basedir ${LUA_LIBRARY})
endif()
target_include_directories(monocoque PUBLIC config ${LIBXML_INCLUDE_DIR}) target_include_directories(monocoque PUBLIC config ${LIBXML_INCLUDE_DIR})

View File

@ -29,8 +29,6 @@ set(devices_source_files
usb/wheels/cammusc5.c usb/wheels/cammusc5.c
usb/wheels/cammusc12.h usb/wheels/cammusc12.h
usb/wheels/cammusc12.c usb/wheels/cammusc12.c
sound/usb_generic_shaker.h
sound/usb_generic_shaker.c
sound/usb_generic_shaker_pulse.c sound/usb_generic_shaker_pulse.c
serial/arduino.h serial/arduino.h
serial/arduino.c serial/arduino.c

View File

@ -2,8 +2,6 @@
#include "../slog/slog.h" #include "../slog/slog.h"
#ifdef USE_PULSEAUDIO
pa_threaded_mainloop* mainloop; pa_threaded_mainloop* mainloop;
pa_context* context; pa_context* context;
@ -43,6 +41,7 @@ int setupsound()
pa_threaded_mainloop_wait(mainloop); pa_threaded_mainloop_wait(mainloop);
} }
pa_threaded_mainloop_unlock(mainloop);
slogi("successfully connected pulseaudio..."); slogi("successfully connected pulseaudio...");
return 1; return 1;
} }
@ -63,17 +62,14 @@ int freesound()
} }
} }
#else
int setupsound() //int setupsound()
{ //{
slogi("connecting portaudio..."); // slogi("connecting portaudio...");
} //}
//
//
int freesound() //int freesound()
{ //{
//
} //}
#endif

View File

@ -1,5 +1,3 @@
#ifdef USE_PULSEAUDIO
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
@ -18,6 +16,9 @@
#define M_PI (3.14159265) #define M_PI (3.14159265)
#endif #endif
static int * volatile drain_result = NULL;
static pa_context *this_context = NULL;
void gear_sound_stream(pa_stream *s, size_t length, void *userdata) { void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
SoundData* data = (SoundData*)userdata; SoundData* data = (SoundData*)userdata;
@ -93,23 +94,36 @@ void stream_state_cb(pa_stream *s, void *mainloop) {
pa_threaded_mainloop_signal(mainloop, 0); pa_threaded_mainloop_signal(mainloop, 0);
} }
int usb_generic_shaker_free(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop) int usb_generic_shaker_free(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop)
{ {
if(!mainloop)
{
// if this happens we are in trouble
return 1;
}
pa_threaded_mainloop_lock(mainloop); pa_threaded_mainloop_lock(mainloop);
int err = 0; int err = 0;
if (sounddevice->stream) if (sounddevice->stream)
{ {
//pa_stream_cork(sounddevice->stream, 1, stream_success_cb, mainloop);
pa_stream_disconnect(sounddevice->stream); pa_stream_disconnect(sounddevice->stream);
pa_stream_unref(sounddevice->stream); pa_stream_unref(sounddevice->stream);
// why is this wrong // why is this wrong
//pa_xfree(sounddevice->stream); //pa_xfree(sounddevice->stream);
} }
pa_threaded_mainloop_unlock(mainloop); pa_threaded_mainloop_unlock(mainloop);
return err; return err;
} }
int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, int channels, const char* streamname) int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, int channels, const char* streamname)
{ {
this_context = context;
pa_threaded_mainloop_lock(mainloop); pa_threaded_mainloop_lock(mainloop);
pa_stream *stream; pa_stream *stream;
@ -173,12 +187,14 @@ int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* main
// for now i'm only supporting playing on one specified channel which is the concept you should build your setups around // for now i'm only supporting playing on one specified channel which is the concept you should build your setups around
cv.values[pan] = channel_volume; cv.values[pan] = channel_volume;
assert(pa_stream_connect_playback(stream, devname, &buffer_attr, stream_flags, &cv, NULL) == 0); //assert(pa_stream_connect_playback(stream, devname, &buffer_attr, stream_flags, &cv, NULL) == 0);
pa_stream_connect_playback(stream, devname, &buffer_attr, stream_flags, &cv, NULL);
// Wait for the stream to be ready // Wait for the stream to be ready
for(;;) { for(;;) {
pa_stream_state_t stream_state = pa_stream_get_state(stream); pa_stream_state_t stream_state = pa_stream_get_state(stream);
assert(PA_STREAM_IS_GOOD(stream_state)); //assert(PA_STREAM_IS_GOOD(stream_state));
PA_STREAM_IS_GOOD(stream_state);
if (stream_state == PA_STREAM_READY) break; if (stream_state == PA_STREAM_READY) break;
pa_threaded_mainloop_wait(mainloop); pa_threaded_mainloop_wait(mainloop);
} }
@ -192,5 +208,3 @@ int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* main
pa_threaded_mainloop_unlock(mainloop); pa_threaded_mainloop_unlock(mainloop);
return 0; return 0;
} }
#endif

View File

@ -160,32 +160,16 @@ int sounddev_gearshift_update(SimDevice* this, SimData* simdata)
} }
#ifdef USE_PULSEAUDIO
int sounddev_free(SimDevice* this) int sounddev_free(SimDevice* this)
{ {
SoundDevice* sounddevice = (void *) this->derived; SoundDevice* sounddevice = (void *) this->derived;
usb_generic_shaker_free(sounddevice, mainloop); usb_generic_shaker_free(sounddevice, mainloop);
free(sounddevice); free(sounddevice);
return 0; return 0;
} }
#else
int sounddev_free(SimDevice* this)
{
SoundDevice* sounddevice = (void *) this->derived;
usb_generic_shaker_free(sounddevice);
free(sounddevice);
return 0;
}
#endif
int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreIdentifier tyre, SoundDeviceSettings sds) int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreIdentifier tyre, SoundDeviceSettings sds)
{ {
slogi("initializing standalone sound device..."); slogi("initializing standalone sound device...");
@ -241,15 +225,8 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreId
} }
#ifdef USE_PULSEAUDIO
//pa_threaded_mainloop* mainloop;
//pa_context* context;
usb_generic_shaker_init(sounddevice, mainloop, context, devname, sds.volume, sds.pan, sds.channels, streamname); usb_generic_shaker_init(sounddevice, mainloop, context, devname, sds.volume, sds.pan, sds.channels, streamname);
#else //usb_generic_shaker_init(sounddevice);
usb_generic_shaker_init(sounddevice);
#endif
} }
static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free }; static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free };

View File

@ -11,7 +11,7 @@
#include "gameloop.h" #include "gameloop.h"
#include "loopdata.h" #include "loopdata.h"
#include "../devices/sound.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../devices/simdevice.h" #include "../devices/simdevice.h"
@ -249,6 +249,8 @@ void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata)
ms->configcheck = 0; ms->configcheck = 0;
} }
setupsound();
f->simdevices = malloc(f->numdevices * sizeof(SimDevice)); f->simdevices = malloc(f->numdevices * sizeof(SimDevice));
int initdevices = devinit(f->simdevices, configureddevices, ds, ms); int initdevices = devinit(f->simdevices, configureddevices, ds, ms);
@ -280,11 +282,11 @@ void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata)
uv_handle_set_data((uv_handle_t*) dt, (void*) dld); uv_handle_set_data((uv_handle_t*) dt, (void*) dld);
int interval = 1000/devices[x].fps; int interval = 1000/devices[x].fps;
uv_timer_start(dt, devicetimercallback, 0, interval); uv_timer_start(dt, devicetimercallback, 0, interval);
slogi("starting device type %i at id at %i fps (%i ms ticks)", devices[x].type, x, devices[x].fps, interval); slogi("starting device type %i at id at %i fps: %i (%i ms ticks)", devices[x].type, x, devices[x].fps, interval);
} }
else else
{ {
slogw("skipped id %i of type %i", x, devices[x].type); slogw("skipped id %i", x);
} }
} }
@ -294,7 +296,6 @@ void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata)
settingsfree(ds[i]); settingsfree(ds[i]);
} }
free(ds); free(ds);
doui = false; doui = false;
} }
else else
@ -362,6 +363,7 @@ void shmdatamapcallback(uv_timer_t* handle)
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
} }
} }
sleep(1);
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
if (devices[x].initialized == true) if (devices[x].initialized == true)
@ -378,7 +380,7 @@ void shmdatamapcallback(uv_timer_t* handle)
free(monocoque_serial_devices[d].portname); free(monocoque_serial_devices[d].portname);
} }
} }
freesound();
int r = simfree(simdata, simmap, f->map); int r = simfree(simdata, simmap, f->map);
slogd("simfree returned %i", r); slogd("simfree returned %i", r);
f->numdevices = 0; f->numdevices = 0;

View File

@ -250,9 +250,12 @@ int getconfigtouse2(const char* config_file_str, char* car, int sim)
{ {
slogt("inside first pass"); slogt("inside first pass");
config_t cfg; config_t cfg;
slogt("init");
config_init(&cfg); config_init(&cfg);
slogt("init??");
if (!config_read_file(&cfg, config_file_str)) if (!config_read_file(&cfg, config_file_str))
{ {
sloge("config read error on pass 1");
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg); config_destroy(&cfg);
return -1; return -1;
@ -826,8 +829,11 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
free(ds->specific_config_file); free(ds->specific_config_file);
ds->specific_config_file = NULL; ds->specific_config_file = NULL;
} }
else
{
slogt("will try to load config file at %s", ds->specific_config_file); slogt("will try to load config file at %s", ds->specific_config_file);
} }
}
return error; return error;
} }

View File

@ -68,6 +68,8 @@ void SetSettingsFromParameters(Parameters* p, MonocoqueSettings* ms, char* confi
{ {
ms->program_action = A_CONFIG_TACH; ms->program_action = A_CONFIG_TACH;
} }
ms->force_udp_mode = false;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@ -217,7 +219,7 @@ int main(int argc, char** argv)
int error = 0; int error = 0;
error = MONOCOQUE_ERROR_NONE; error = MONOCOQUE_ERROR_NONE;
setupsound(); //setupsound();
bool pulseaudio = false; bool pulseaudio = false;
if (ms->program_action == A_PLAY) if (ms->program_action == A_PLAY)
@ -225,7 +227,7 @@ int main(int argc, char** argv)
ms->useconfig = 1; ms->useconfig = 1;
slogi("running monocoque in gameloop mode.."); slogi("running monocoque in gameloop mode..");
#ifdef USE_PULSEAUDIO #ifdef USE_PULSEAUDIO
pa_threaded_mainloop_unlock(mainloop); //pa_threaded_mainloop_unlock(mainloop);
pulseaudio = true; pulseaudio = true;
#endif #endif
@ -244,11 +246,7 @@ int main(int argc, char** argv)
{ {
slogi("running monocoque in test mode..."); slogi("running monocoque in test mode...");
#ifdef USE_PULSEAUDIO setupsound();
pa_threaded_mainloop_unlock(mainloop);
pulseaudio = true;
#endif
ms->useconfig = 0; ms->useconfig = 0;
int configs = getNumberOfConfigs(ms->config_str); int configs = getNumberOfConfigs(ms->config_str);
@ -283,9 +281,6 @@ int main(int argc, char** argv)
simdevices[x].free(&simdevices[x]); simdevices[x].free(&simdevices[x]);
} }
} }
}
if(pulseaudio == true)
{
freesound(); freesound();
} }
} }
@ -298,5 +293,3 @@ cleanup_final:
free(ms); free(ms);
exit(0); exit(0);
} }

@ -1 +1 @@
Subproject commit b25aa66d783ada6ca9656a8abf874e157dcec5d6 Subproject commit 16084208cddf8f4bfcb80318aabd7a49c8d3cca8