I am new in the .Net and, especially, in the SIP space.
I am using the SIPSorcery nuget package to create a SIP softphone of sorts. I want to receive a call and record it. This is my code:
static async Task Main()
{
SIPTransport _sipTransport = new SIPTransport();
SIPRegistrationUserAgent _userAgent = new(_sipTransport, SIP_USERNAME, SIP_PASSWORD, SIP_SERVER, DEFAULT_EXPIRY);
_userAgent.RegistrationSuccessful += (uri, resp) =>
{
Console.WriteLine($"{uri} registration succeeded.");
SIPUserAgent _ua = new SIPUserAgent(_sipTransport, null, true);
_ua.OnIncomingCall += async (ua, req) =>
{
WindowsAudioEndPoint winAudioEP = new WindowsAudioEndPoint(new AudioEncoder());
VoIPMediaSession voipMediaSession = new VoIPMediaSession(winAudioEP.ToMediaEndPoints());
voipMediaSession.AcceptRtpFromAny = true;
voipMediaSession.OnRtpPacketReceived += OnRtpPacketReceived;
var uas = ua.AcceptCall(req);
await ua.Answer(uas, voipMediaSession);
};
};
_userAgent.Start();
}
private static void OnRtpPacketReceived(IPEndPoint remoteEndPoint, SDPMediaTypesEnum mediaType, RTPPacket rtpPacket)
{
if (mediaType == SDPMediaTypesEnum.audio)
{
var sample = rtpPacket.Payload;
for (int index = 0; index < sample.Length; index++)
{
if (rtpPacket.Header.PayloadType == (int)SDPWellKnownMediaFormatsEnum.PCMA)
{
short pcm = NAudio.Codecs.ALawDecoder.ALawToLinearSample(sample[index]);
byte[] pcmSample = new byte[] { (byte)(pcm & 0xFF), (byte)(pcm >> 8) };
_waveFile.Write(pcmSample, 0, 2);
}
else
{
short pcm = NAudio.Codecs.MuLawDecoder.MuLawToLinearSample(sample[index]);
byte[] pcmSample = new byte[] { (byte)(pcm & 0xFF), (byte)(pcm >> 8) };
_waveFile.Write(pcmSample, 0, 2);
}
}
}
}
The registration and the connection works. I do also get audio and save it in that _waveFile file.
However, the audio I get is noise. No real voice or anything like that. In the case of calling a sip address, I did not encounter such a problem. That is why I am confused. I would think I would have the problem when calling too if it was something that has to do with a Codec or something like that.