sipsorcery-org / sipsorcery

A WebRTC, SIP and VoIP library for C# and .NET. Designed for real-time communications apps.
https://sipsorcery-org.github.io/sipsorcery
Other
1.39k stars 424 forks source link

Cannot Play Messages On Linux with "SendAudioFromStream" #1126

Closed piercarlo62 closed 1 month ago

piercarlo62 commented 1 month ago

Hello, I have a problem with play messages in Linux.

I created a Sip Client Automation that output some registered messages when you call the client. this is the code:

I get the media session on Client Answer with following code:

        private VoIPMediaSession CreateMediaSession()
        {
            var audioOptions = new AudioSourceOptions { AudioSource = AudioSourcesEnum.None };
            AudioExtrasSource audioExtrasSource = new(new AudioEncoder(), audioOptions);
            MediaEndPoints mediaEndPoints = new() { AudioSource = audioExtrasSource };
            VoIPMediaSession voipMediaSession = new(mediaEndPoints)
            {
                AcceptRtpFromAny = true
            };

            voipMediaSession.OnRtpPacketReceived += OnRtpPacketReceived;
            voipMediaSession.AudioExtrasSource.OnSendFromAudioStreamComplete += OnSendFromAudioStreamComplete;

            return voipMediaSession;
        }

And I play Messages with following code:

        public async Task PlayAudioAsync(byte[] buffer, bool isFinal)
        {
            if (PreMessageIsOn || BackgroundIsOn)
                MediaSession?.AudioExtrasSource.CancelSendAudioFromStream();
            var raw = await FFMpegUtil.GetRawFromWav(buffer);
            using var ms = new MemoryStream(raw);
            m_AudioBuffer = raw;
            OutputAudioBufferIndex = 0;
            IsFinalMessage = isFinal;
            await MediaSession?.AudioExtrasSource.SendAudioFromStream(ms, AudioSamplingRatesEnum.Rate8KHz);
        }

So buffer is coming from

File.ReadAllBytesAsync(messageFullPath)

and with FFMPegUtil.GetRawFromWav(buffer) I get the buffer without wav header.

So, under windows it works very well but on Linux I can't heard the message.

I miss somenthing or it is a Linux problem?

and eventuallly some workaround ?

Thank you

piercarlo62 commented 1 month ago

Ok I found the mistake it was a misconfiguration on ffmpg now works fine on linux too.