From 33ad0ecd06e7c3683ce35b9b4f4273b94a067d99 Mon Sep 17 00:00:00 2001 From: Ernesto <90931029+edossantos-sipcaller@users.noreply.github.com> Date: Wed, 24 Apr 2024 16:06:02 -0300 Subject: [PATCH] Adding try-catch blocks to timer callbacks to avoid crashing the app when an exception is thrown. (#1107) Co-authored-by: Ernesto Dos Santos Afonso --- src/app/Media/Sources/AudioExtrasSource.cs | 36 +++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/app/Media/Sources/AudioExtrasSource.cs b/src/app/Media/Sources/AudioExtrasSource.cs index 3126d00d0..3b815c042 100644 --- a/src/app/Media/Sources/AudioExtrasSource.cs +++ b/src/app/Media/Sources/AudioExtrasSource.cs @@ -427,14 +427,21 @@ private void SendMusicSample(object state) /// private void SendSilenceSample(object state) { - if (!_isClosed && !_streamSendInProgress && _sendSampleTimer != null) + try { - lock (_sendSampleTimer) + if (!_isClosed && !_streamSendInProgress && _sendSampleTimer != null) { - short[] silencePcm = new short[_audioFormatManager.SelectedFormat.ClockRate / 1000 * _audioSamplePeriodMilliseconds]; - EncodeAndSend(silencePcm, _audioFormatManager.SelectedFormat.ClockRate); + lock (_sendSampleTimer) + { + short[] silencePcm = new short[_audioFormatManager.SelectedFormat.ClockRate / 1000 * _audioSamplePeriodMilliseconds]; + EncodeAndSend(silencePcm, _audioFormatManager.SelectedFormat.ClockRate); + } } } + catch (Exception e) + { + Log.LogError(e, "Exception sending silence sample"); + } } /// @@ -442,18 +449,25 @@ private void SendSilenceSample(object state) /// private void SendSignalGeneratorSample(object state) { - if (!_isClosed && !_streamSendInProgress && _sendSampleTimer != null) + try { - lock (_sendSampleTimer) + if (!_isClosed && !_streamSendInProgress && _sendSampleTimer != null) { - // Get the signal generator to generate the samples and then convert from signed linear to PCM. - float[] linear = new float[_audioFormatManager.SelectedFormat.ClockRate / 1000 * _audioSamplePeriodMilliseconds]; - _signalGenerator.Read(linear, 0, linear.Length); - short[] pcm = linear.Select(x => (short)(x * LINEAR_MAXIMUM)).ToArray(); + lock (_sendSampleTimer) + { + // Get the signal generator to generate the samples and then convert from signed linear to PCM. + float[] linear = new float[_audioFormatManager.SelectedFormat.ClockRate / 1000 * _audioSamplePeriodMilliseconds]; + _signalGenerator.Read(linear, 0, linear.Length); + short[] pcm = linear.Select(x => (short)(x * LINEAR_MAXIMUM)).ToArray(); - EncodeAndSend(pcm, _audioFormatManager.SelectedFormat.ClockRate); + EncodeAndSend(pcm, _audioFormatManager.SelectedFormat.ClockRate); + } } } + catch (Exception e) + { + Log.LogError(e, "Exception sending signal generator sample"); + } } ///