From 06469b368934c4830ff6d678ef88591c30e4b281 Mon Sep 17 00:00:00 2001 From: karlstav Date: Sun, 18 Feb 2018 17:20:34 +0100 Subject: [PATCH] softer pthread exiting with pthread_exit pulseauio thread now uses pthread_exit to exit if something unexpected happens it also sets audio.terminate to 1 and writes the error message to a audio.error_message so it can be printed by main thread after cleanup. this also enables main thread to cleanup terminal upon unexpected error on audio thread --- cava.c | 12 ++++++++++-- input/fifo.c | 1 + input/pulse.c | 13 +++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cava.c b/cava.c index ecc1063..84d4139 100644 --- a/cava.c +++ b/cava.c @@ -350,6 +350,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co if (is_loop_device_for_sure(audio.source)) { if (directory_exists("/sys/")) { if (! directory_exists("/sys/module/snd_aloop/")) { + cleanup(); fprintf(stderr, "Linux kernel module \"snd_aloop\" does not seem to be loaded.\n" "Maybe run \"sudo modprobe snd_aloop\".\n"); @@ -369,11 +370,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co nanosleep (&req, NULL); n++; if (n > 2000) { - #ifdef DEBUG cleanup(); fprintf(stderr, "could not get rate and/or format, problems with audio thread? quiting...\n"); - #endif exit(EXIT_FAILURE); } } @@ -808,6 +807,15 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co for (o = 0; o < bars; o++) { flastd[o] = f[o]; } + + //checking if audio thread has exited unexpectedly + if (audio.terminate == 1) { + cleanup(); + fprintf(stderr, + "Audio thread exited unexpectedly. %s\n", audio.error_message); + exit(EXIT_FAILURE); + } + }//resize terminal }//reloading config diff --git a/input/fifo.c b/input/fifo.c index d953ad1..4eead26 100644 --- a/input/fifo.c +++ b/input/fifo.c @@ -10,6 +10,7 @@ struct audio_data { int im; //input mode alsa, fifo or pulse int channels; int terminate; // shared variable used to terminate audio thread + char error_message[1024]; }; diff --git a/input/pulse.c b/input/pulse.c index f95c41e..3b906fe 100644 --- a/input/pulse.c +++ b/input/pulse.c @@ -131,8 +131,10 @@ void* input_pulse(void* data) int error; if (!(s = pa_simple_new(NULL, "cava", PA_STREAM_RECORD, audio->source, "audio for cava", &ss, NULL, &pb, &error))) { - fprintf(stderr, __FILE__": Could not open pulseaudio source: %s, %s. To find a list of your pulseaudio sources run 'pacmd list-sources'\n",audio->source, pa_strerror(error)); - exit(EXIT_FAILURE); + //fprintf(stderr, __FILE__": Could not open pulseaudio source: %s, %s. To find a list of your pulseaudio sources run 'pacmd list-sources'\n",audio->source, pa_strerror(error)); + sprintf(audio->error_message, __FILE__": Could not open pulseaudio source: %s, %s. To find a list of your pulseaudio sources run 'pacmd list-sources'\n",audio->source, pa_strerror(error)); + audio->terminate = 1; + pthread_exit(NULL); } n = 0; @@ -140,8 +142,11 @@ void* input_pulse(void* data) while (1) { /* Record some data ... */ if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) { - fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); - exit(EXIT_FAILURE); + //fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); + //exit(EXIT_FAILURE); + sprintf(audio->error_message, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); + audio->terminate = 1; + pthread_exit(NULL); } //sorting out channels