Skip to content

Audio playback lag and brief interruptions when streaming WAV to call #1477

@PrasannaLakshmiD

Description

@PrasannaLakshmiD

AlloyAIS.wav

Hi team,

I'm using SIPSorcery to play an audio prompt into a live call session. The playback works, but sometimes I hear small breaks or lags in the middle of the audio—it doesn’t play smoothly every time.

Observed behavior:
The audio starts correctly but sometimes lags or briefly cuts out midway.
The WAV file itself is clean (verified by local playback).
The issue occurs intermittently—not on every playback.
I am making multiple simultaneous calls to the same SIP extension and playing different requested audio files concurrently (each call uses its own AudioExtrasSource).

Environment details:
.NET version: Core 9
SIPSorcery version: 8.0.23
Platform: Linux and Windows both
Audio file: 8kHz, 16-bit mono WAV(3CX Supported wav file format)

Here’s a simplified version of the code I’m using:

public async void AttachToTransport(SIPTransport transport)
{
var registrationAgent = new SIPUserAgent(transport, null, true);

registrationAgent.OnIncomingCall += async (ua, req) =>
{
    string callId = req.Header?.CallId ?? "(none)";
    try
    {
        var user = req.Header.To.ToURI.ToString();
        var start = user.IndexOf(':') + 1;
        var end = user.IndexOf('@');
        string bargeinExtNum = user.Substring(start, end - start);
        _log.Information("Call [INVITE] from {From} Call-ID={CallId}", req.Header?.From?.ToString(), callId);
        string audioPath = "Audio/SupportedWavFile.wav";
        var audioSession = new AudioSendOnlyMediaSession { AcceptRtpFromAny = true };
        audioSession.OnAudioFormatsNegotiated += formats =>
        {
            foreach (var f in formats)
                _log.Information("🎚️ [Negotiated Codec] {Codec} {Clock}Hz", f.Codec, (int)f.ClockRate);
        };

        audioSession.AudioExtrasSource.RestrictFormats(fmt =>
            fmt.Codec == AudioCodecsEnum.PCMU || fmt.Codec == AudioCodecsEnum.PCMA);
        // Each incoming call gets its own SIPUserAgent
        var callAgent = new SIPUserAgent(transport, null, true);
        var uas = callAgent.AcceptCall(req);

        bool answered = await callAgent.Answer(uas, audioSession);
        audioSession.AudioExtrasSource.SetSource(AudioSourcesEnum.Silence);
        if (!answered)
        {
            _log.Warning("Failed to answer barge-in call.");
            try { ua.Hangup(); } catch { }
            return;
        }

        // Play prompt or beep
        if (string.IsNullOrWhiteSpace(audioPath) || !File.Exists(audioPath) || new FileInfo(audioPath).Length == 0)
        {
            _log.Warning("No valid audio file({audioPath}) found.", audioPath);
        }            
        else
        {
            try
            {
                byte[] audioBytes = await File.ReadAllBytesAsync(audioPath);
                using var ms = new MemoryStream(audioBytes);
                await audioSession.AudioExtrasSource.SendAudioFromStream(ms, AudioSamplingRatesEnum.Rate8KHz);
            }
            catch (Exception ex)
            {
                _log.Error(ex, "Error sending audio from file {AudioPath}: {Error}", audioPath, ex.Message);
            }
        }
        audioSession.AudioExtrasSource.SetSource(AudioSourcesEnum.Silence);
        // Add small delay before hangup to flush RTP
        await Task.Delay(300);
        callAgent.Hangup();
    }
    catch (Exception ex)
    {
        _log.Error(ex, "Error handling Call-ID={CallId} exception is : {Error}", callId, ex.Message );
        try { ua?.Hangup(); } catch { }
    }
    finally
    {
        if (!string.IsNullOrEmpty(hCallID))
            BargeHistoryDictionaryCache.Remove(hCallID);
    }
};

}

Can you please check if there’s a known issue with audio streaming consistency or any recommended pattern (like using timers, fixed frame chunking, or buffered streaming) to avoid these small interruptions?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions