From cdf96b75fcc77ad3b52ddcaaf6fb6cd55f72e4e2 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Wed, 26 Oct 2022 10:07:09 +0200 Subject: [PATCH] handle audio resampler init exception gracefully Catch the exception. Can occur mainly if wrong '--param resampler=' option is given. --- src/audio/audio.cpp | 17 +++++++++++++++-- src/rtp/audio_decoders.cpp | 15 ++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 8aa24ef12..b67a7a637 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -88,6 +89,7 @@ #include "tv.h" #include "transmit.h" #include "pdb.h" +#include "ug_runtime_error.hpp" #include "utils/color_out.h" #include "utils/net.h" #include "utils/thread.h" @@ -695,6 +697,10 @@ static void *audio_receiver_thread(void *arg) cp->decoder_state = dec_state; dec_state->enabled = true; dec_state->pbuf_data.decoder = (struct state_audio_decoder *) audio_decoder_init(s->audio_channel_map, s->audio_scale, s->requested_encryption, (audio_playback_ctl_t) audio_playback_ctl, s->audio_playback_device, s->audio_receiver_module.get()); + if (!dec_state->pbuf_data.decoder) { + exit_uv(1); + break; + } audio_decoder_set_volume(dec_state->pbuf_data.decoder, s->muted_receiver ? 0.0 : s->volume); assert(dec_state->pbuf_data.decoder != NULL); cp->decoder_state_deleter = audio_decoder_state_deleter; @@ -982,7 +988,14 @@ static void *audio_sender_thread(void *arg) set_thread_name(__func__); struct state_audio *s = (struct state_audio *) arg; struct audio_frame *buffer = NULL; - audio_frame2_resampler resampler_state; + unique_ptr resampler_state; + try { + resampler_state = unique_ptr(new audio_frame2_resampler); + } catch (ug_runtime_error &e) { + log_msg(LOG_LEVEL_ERROR, MOD_NAME "%s\n", e.what()); + exit_uv(1); + return NULL; + } printf("Audio sending started.\n"); @@ -1039,7 +1052,7 @@ static void *audio_sender_thread(void *arg) bf_n.change_bps(2); } - bf_n.resample(resampler_state, resample_to); + bf_n.resample(*resampler_state, resample_to); } // COMPRESS process_statistics(s, &bf_n); diff --git a/src/rtp/audio_decoders.cpp b/src/rtp/audio_decoders.cpp index 96babb515..3fd342d37 100644 --- a/src/rtp/audio_decoders.cpp +++ b/src/rtp/audio_decoders.cpp @@ -63,6 +63,7 @@ #include "crypto/crc.h" #include "crypto/openssl_decrypt.h" #include "rang.hpp" +#include "ug_runtime_error.hpp" #include "utils/color_out.h" #include "utils/macros.h" #include "utils/packet_counter.h" @@ -279,14 +280,20 @@ ADD_TO_PARAM("soft-resample", "* soft-resample=/\n" void *audio_decoder_init(char *audio_channel_map, const char *audio_scale, const char *encryption, audio_playback_ctl_t c, void *p_state, struct module *parent) { - struct state_audio_decoder *s; + struct state_audio_decoder *s = NULL; bool scale_auto = false; double scale_factor = 1.0; char *tmp = nullptr; assert(audio_scale != NULL); - s = new struct state_audio_decoder(); + try { + s = new struct state_audio_decoder(); + } catch (ug_runtime_error &e) { + log_msg(LOG_LEVEL_ERROR, MOD_NAME "%s\n", e.what()); + goto error; + } + s->magic = AUDIO_DECODER_MAGIC; s->audio_playback_ctl_func = c; s->audio_playback_state = p_state; @@ -422,7 +429,9 @@ void *audio_decoder_init(char *audio_channel_map, const char *audio_scale, const error: free(tmp); - audio_decoder_destroy(s); + if (s) { + audio_decoder_destroy(s); + } return NULL; }