Skip to content

Commit

Permalink
Adding try-catch blocks to timer callbacks to avoid crashing the app …
Browse files Browse the repository at this point in the history
…when an exception is thrown. (#1107)

Co-authored-by: Ernesto Dos Santos Afonso <edossantos@sipcaller.com>
  • Loading branch information
edossantos-sipcaller and edossantos-sipcaller authored Apr 24, 2024
1 parent 10effcb commit 33ad0ec
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/app/Media/Sources/AudioExtrasSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,33 +427,47 @@ private void SendMusicSample(object state)
/// </summary>
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");
}
}

/// <summary>
/// Sends a sample from a signal generator generated waveform.
/// </summary>
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");
}
}

/// <summary>
Expand Down

0 comments on commit 33ad0ec

Please sign in to comment.