diff --git a/include/dsd.h b/include/dsd.h index e0ff7b9..ef65096 100644 --- a/include/dsd.h +++ b/include/dsd.h @@ -1130,7 +1130,7 @@ extern "C" { #ifdef USE_RTLSDR void open_rtlsdr_stream(dsd_opts *opts); void cleanup_rtlsdr_stream(); -void get_rtlsdr_sample(int16_t *sample, dsd_opts * opts, dsd_state * state); +int get_rtlsdr_sample(int16_t *sample, dsd_opts * opts, dsd_state * state); void rtlsdr_sighandler(); void rtl_dev_tune(dsd_opts * opts, long int frequency); int rtl_return_rms(); diff --git a/src/dsd_main.c b/src/dsd_main.c index b09b462..c8e1fce 100644 --- a/src/dsd_main.c +++ b/src/dsd_main.c @@ -26,8 +26,15 @@ #include "provoice_const.h" #include "git_ver.h" +#include + volatile uint8_t exitflag; //fix for issue #136 +void handler(int) +{ + exitflag = 1; +} + int pretty_colors() { fprintf (stderr,"%sred\n", KRED); @@ -1279,7 +1286,7 @@ if (opts->audio_out_type == 0) openPulseOutput(opts); } - while (1) + while (!exitflag) { noCarrier (opts, state); @@ -1342,6 +1349,14 @@ cleanupAndExit (dsd_opts * opts, dsd_state * state) closeWavOutFileR (opts, state); } closeSymbolOutFile (opts, state); + if (opts->rtl_started == 1) + { + cleanup_rtlsdr_stream(); + } + if (opts->use_ncurses_terminal == 1) + { + ncursesClose(opts); + } //close MBE out files if (opts->mbe_out_f != NULL) closeMbeOutFile (opts, state); @@ -2570,6 +2585,9 @@ main (int argc, char **argv) // opts.audio_out_fd = opts.audio_in_fd; //not sure that this really does much anymore, other than cause problems } + signal(SIGINT, handler); + signal(SIGTERM, handler); + if (opts.playfiles == 1) { state.aout_gain = 25; //BUGFIX: No audio output when playing back .amb/.imb files diff --git a/src/dsd_ncurses.c b/src/dsd_ncurses.c index 9956ab3..e113565 100644 --- a/src/dsd_ncurses.c +++ b/src/dsd_ncurses.c @@ -1732,8 +1732,6 @@ void ncursesMenu (dsd_opts * opts, dsd_state * state) } if (choice == 20) { - ncursesClose(); - //cleanup_rtlsdr_stream(); cleanupAndExit (opts, state); } @@ -3414,7 +3412,6 @@ ncursesPrinter (dsd_opts * opts, dsd_state * state) if (c == 113) //'q' key, quit { - ncursesClose(); cleanupAndExit (opts, state); } diff --git a/src/dsd_symbol.c b/src/dsd_symbol.c index a28472d..330bb6e 100644 --- a/src/dsd_symbol.c +++ b/src/dsd_symbol.c @@ -121,7 +121,6 @@ getSymbol (dsd_opts * opts, dsd_state * state, int have_sync) //else cleanup and exit else { - if (opts->use_ncurses_terminal == 1) ncursesClose(opts); cleanupAndExit(opts, state); } } @@ -131,7 +130,10 @@ getSymbol (dsd_opts * opts, dsd_state * state, int have_sync) #ifdef USE_RTLSDR // TODO: need to read demodulated stream here // get_rtlsdr_sample(&sample); - get_rtlsdr_sample(&sample, opts, state); + if (get_rtlsdr_sample(&sample, opts, state) < 0) + { + cleanupAndExit(opts, state); + } if (opts->monitor_input_audio == 1 && state->lastsynctype == -1 && sample < 32767 && sample > -32767) { state->pulse_raw_out_buffer = sample; //steal raw out buffer sample here? @@ -195,7 +197,6 @@ getSymbol (dsd_opts * opts, dsd_state * state, int have_sync) { fprintf (stderr, "Connection to TCP Server Disconnected.\n"); fprintf (stderr, "Closing DSD-FME.\n"); - if (opts->use_ncurses_terminal == 1) ncursesClose(opts); cleanupAndExit(opts, state); } @@ -533,7 +534,6 @@ getSymbol (dsd_opts * opts, dsd_state * state, int have_sync) //else cleanup and exit else { - if (opts->use_ncurses_terminal == 1) ncursesClose(opts); cleanupAndExit(opts, state); } } diff --git a/src/rtl_sdr_fm.cpp b/src/rtl_sdr_fm.cpp index 810b617..be42f2f 100644 --- a/src/rtl_sdr_fm.cpp +++ b/src/rtl_sdr_fm.cpp @@ -1075,16 +1075,28 @@ void cleanup_rtlsdr_stream() // } //find way to modify this function to allow hopping (tuning) while squelched and send 0 sample? -void get_rtlsdr_sample(int16_t *sample, dsd_opts * opts, dsd_state * state) +int get_rtlsdr_sample(int16_t *sample, dsd_opts * opts, dsd_state * state) { - if (output.queue.empty()) + while (output.queue.empty()) { - safe_cond_wait(&output.ready, &output.ready_m); + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_nsec += 10e6; + + pthread_mutex_lock(&output.ready_m); + pthread_cond_timedwait(&output.ready, &output.ready_m, &ts); + pthread_mutex_unlock(&output.ready_m); + + if (exitflag) + { + return -1; + } } pthread_rwlock_wrlock(&output.rw); *sample = output.queue.front() * volume_multiplier; output.queue.pop(); pthread_rwlock_unlock(&output.rw); + return 0; } //function may lag since it isn't running as its own thread