From 7afd7f12ed8fdb1d2e5e149efecc025c005c54de Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Tue, 25 Mar 2025 10:04:39 -0400 Subject: [PATCH] Huge refactor to improve memory efficiency mostly around pulseaudio --- CMakeLists.txt | 17 ++++++------ src/monocoque/devices/CMakeLists.txt | 2 -- src/monocoque/devices/sound.c | 26 ++++++++----------- .../devices/sound/usb_generic_shaker_pulse.c | 26 ++++++++++++++----- src/monocoque/devices/sounddevice.c | 25 +----------------- src/monocoque/gameloop/gameloop.c | 12 +++++---- src/monocoque/helper/confighelper.c | 8 +++++- src/monocoque/monocoque.c | 17 ++++-------- src/monocoque/simulatorapi/simapi | 2 +- 9 files changed, 60 insertions(+), 75 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc4e4af..4434dc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ endif() SET_SOURCE_FILES_PROPERTIES( src/monocoque.c PROPERTIES LANGUAGE C) -#set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_BUILD_TYPE Debug) 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 add_executable(monocoque src/monocoque/monocoque.c) -if(USE_PULSEAUDIO) - message("Using pulseaudio backend...") - add_compile_definitions(USE_PULSEAUDIO=true) - target_link_libraries(monocoque m hidapi-hidraw pulse serialport xml2 argtable2 config gameloop helper devices slog simulatorapi uv xdg-basedir ${LUA_LIBRARY}) -else() - message("Using portaudio backend...") - target_link_libraries(monocoque m hidapi-hidraw portaudio serialport xml2 argtable2 config gameloop helper devices slog simulatorapi uv xdg-basedir ${LUA_LIBRARY}) -endif() + +#if(USE_PULSEAUDIO) +#else() +#endif() +message("Using pulseaudio backend...") +add_compile_definitions(USE_PULSEAUDIO=true) +target_link_libraries(monocoque m hidapi-hidraw pulse serialport xml2 argtable2 config gameloop helper devices slog simulatorapi uv xdg-basedir ${LUA_LIBRARY}) target_include_directories(monocoque PUBLIC config ${LIBXML_INCLUDE_DIR}) diff --git a/src/monocoque/devices/CMakeLists.txt b/src/monocoque/devices/CMakeLists.txt index 7b6e097..7054d28 100644 --- a/src/monocoque/devices/CMakeLists.txt +++ b/src/monocoque/devices/CMakeLists.txt @@ -29,8 +29,6 @@ set(devices_source_files usb/wheels/cammusc5.c usb/wheels/cammusc12.h usb/wheels/cammusc12.c - sound/usb_generic_shaker.h - sound/usb_generic_shaker.c sound/usb_generic_shaker_pulse.c serial/arduino.h serial/arduino.c diff --git a/src/monocoque/devices/sound.c b/src/monocoque/devices/sound.c index 43cce17..e3a4ccb 100644 --- a/src/monocoque/devices/sound.c +++ b/src/monocoque/devices/sound.c @@ -2,8 +2,6 @@ #include "../slog/slog.h" -#ifdef USE_PULSEAUDIO - pa_threaded_mainloop* mainloop; pa_context* context; @@ -43,6 +41,7 @@ int setupsound() pa_threaded_mainloop_wait(mainloop); } + pa_threaded_mainloop_unlock(mainloop); slogi("successfully connected pulseaudio..."); return 1; } @@ -63,17 +62,14 @@ int freesound() } } -#else -int setupsound() -{ - slogi("connecting portaudio..."); -} - - -int freesound() -{ - -} - -#endif +//int setupsound() +//{ +// slogi("connecting portaudio..."); +//} +// +// +//int freesound() +//{ +// +//} diff --git a/src/monocoque/devices/sound/usb_generic_shaker_pulse.c b/src/monocoque/devices/sound/usb_generic_shaker_pulse.c index 8c58917..1f47606 100644 --- a/src/monocoque/devices/sound/usb_generic_shaker_pulse.c +++ b/src/monocoque/devices/sound/usb_generic_shaker_pulse.c @@ -1,5 +1,3 @@ -#ifdef USE_PULSEAUDIO - #include #include #include @@ -18,6 +16,9 @@ #define M_PI (3.14159265) #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) { SoundData* data = (SoundData*)userdata; @@ -93,23 +94,36 @@ void stream_state_cb(pa_stream *s, void *mainloop) { pa_threaded_mainloop_signal(mainloop, 0); } + 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); + int err = 0; if (sounddevice->stream) { + //pa_stream_cork(sounddevice->stream, 1, stream_success_cb, mainloop); pa_stream_disconnect(sounddevice->stream); pa_stream_unref(sounddevice->stream); // why is this wrong //pa_xfree(sounddevice->stream); + } + pa_threaded_mainloop_unlock(mainloop); + 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) { + this_context = context; pa_threaded_mainloop_lock(mainloop); 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 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 for(;;) { 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; 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); return 0; } - -#endif diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 44efbb1..85b0fe2 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -160,32 +160,16 @@ int sounddev_gearshift_update(SimDevice* this, SimData* simdata) } -#ifdef USE_PULSEAUDIO - int sounddev_free(SimDevice* this) { SoundDevice* sounddevice = (void *) this->derived; usb_generic_shaker_free(sounddevice, mainloop); - free(sounddevice); 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) { 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); -#else - usb_generic_shaker_init(sounddevice); -#endif + //usb_generic_shaker_init(sounddevice); } static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free }; diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index a5d7802..b67d7ac 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -11,7 +11,7 @@ #include "gameloop.h" #include "loopdata.h" - +#include "../devices/sound.h" #include "../helper/parameters.h" #include "../helper/confighelper.h" #include "../devices/simdevice.h" @@ -249,6 +249,8 @@ void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata) ms->configcheck = 0; } + setupsound(); + f->simdevices = malloc(f->numdevices * sizeof(SimDevice)); 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); int interval = 1000/devices[x].fps; 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 { - 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]); } free(ds); - doui = false; } else @@ -362,6 +363,7 @@ void shmdatamapcallback(uv_timer_t* handle) devices[x].update(&devices[x], simdata); } } + sleep(1); for (int x = 0; x < numdevices; x++) { if (devices[x].initialized == true) @@ -378,7 +380,7 @@ void shmdatamapcallback(uv_timer_t* handle) free(monocoque_serial_devices[d].portname); } } - + freesound(); int r = simfree(simdata, simmap, f->map); slogd("simfree returned %i", r); f->numdevices = 0; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 609b459..1f751ac 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -250,9 +250,12 @@ int getconfigtouse2(const char* config_file_str, char* car, int sim) { slogt("inside first pass"); config_t cfg; + slogt("init"); config_init(&cfg); + slogt("init??"); 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)); config_destroy(&cfg); return -1; @@ -826,7 +829,10 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co free(ds->specific_config_file); ds->specific_config_file = NULL; } - slogt("will try to load config file at %s", ds->specific_config_file); + else + { + slogt("will try to load config file at %s", ds->specific_config_file); + } } return error; } diff --git a/src/monocoque/monocoque.c b/src/monocoque/monocoque.c index 6508770..5a9b937 100644 --- a/src/monocoque/monocoque.c +++ b/src/monocoque/monocoque.c @@ -68,6 +68,8 @@ void SetSettingsFromParameters(Parameters* p, MonocoqueSettings* ms, char* confi { ms->program_action = A_CONFIG_TACH; } + + ms->force_udp_mode = false; } int main(int argc, char** argv) @@ -217,7 +219,7 @@ int main(int argc, char** argv) int error = 0; error = MONOCOQUE_ERROR_NONE; - setupsound(); + //setupsound(); bool pulseaudio = false; if (ms->program_action == A_PLAY) @@ -225,7 +227,7 @@ int main(int argc, char** argv) ms->useconfig = 1; slogi("running monocoque in gameloop mode.."); #ifdef USE_PULSEAUDIO - pa_threaded_mainloop_unlock(mainloop); + //pa_threaded_mainloop_unlock(mainloop); pulseaudio = true; #endif @@ -244,11 +246,7 @@ int main(int argc, char** argv) { slogi("running monocoque in test mode..."); -#ifdef USE_PULSEAUDIO - pa_threaded_mainloop_unlock(mainloop); - pulseaudio = true; -#endif - + setupsound(); ms->useconfig = 0; int configs = getNumberOfConfigs(ms->config_str); @@ -283,9 +281,6 @@ int main(int argc, char** argv) simdevices[x].free(&simdevices[x]); } } - } - if(pulseaudio == true) - { freesound(); } } @@ -298,5 +293,3 @@ cleanup_final: free(ms); exit(0); } - - diff --git a/src/monocoque/simulatorapi/simapi b/src/monocoque/simulatorapi/simapi index b25aa66..1608420 160000 --- a/src/monocoque/simulatorapi/simapi +++ b/src/monocoque/simulatorapi/simapi @@ -1 +1 @@ -Subproject commit b25aa66d783ada6ca9656a8abf874e157dcec5d6 +Subproject commit 16084208cddf8f4bfcb80318aabd7a49c8d3cca8