Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem recording incoming call audio on Windows #1159

Closed
Rac0n opened this issue Aug 8, 2024 · 3 comments
Closed

Problem recording incoming call audio on Windows #1159

Rac0n opened this issue Aug 8, 2024 · 3 comments

Comments

@Rac0n
Copy link

Rac0n commented Aug 8, 2024

Hello,

I am new in .Net and, especially, in the SIP space. I am using this library in order to get a call from a softphone.

I managed to make the connection to receive the call. However, I am having trouble with recording it. I do get a file and some audio in it but the volume is very low and there is a lot of distortion (or noise). I would assume it is a codec problem, or a audio feature one.

This is basically my code. The softphone uses the G729 codec.

`private static readonly WaveFormat _waveFormat = new WaveFormat(8000, 16, 1);

static async Task Main()
{
var outputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), "NAudio");
Directory.CreateDirectory(outputFolder);
outputFilePath = Path.Combine(outputFolder, "output.wav");
_waveFile = new WaveFileWriter(outputFilePath, _waveFormat);

_sipTransport = new SIPTransport();
_sipTransport.AddSIPChannel(new SIPUDPChannel(new IPEndPoint(IPAddress.Any, SIP_PORT)));

_userAgent = new(_sipTransport, SIP_USERNAME, SIP_PASSWORD, $"{SIP_SERVER}:{SIP_PORT}", DEFAULT_EXPIRY);

_userAgent.RegistrationSuccessful += (uri, resp) =>
{
    Console.WriteLine($"{uri} registration succeeded.");

    Console.WriteLine(">>>>>");
    Console.WriteLine(resp.ToString());
    Console.WriteLine(">>>>>");

    _ua = new SIPUserAgent(_sipTransport, null, true);

    _ua.OnIncomingCall += async (ua, req) =>
    {
        Console.WriteLine(">>>>>");
        Console.WriteLine(req.ToString());
        Console.WriteLine(">>>>>");

        var uas = ua.AcceptCall(req);

        WindowsAudioEndPoint winAudioEP = new WindowsAudioEndPoint(new AudioEncoder());
        winAudioEP.RestrictFormats((format) => format.Codec == AudioCodecsEnum.G729);

        VoIPMediaSession voipMediaSession = new VoIPMediaSession(winAudioEP.ToMediaEndPoints());

        
        voipMediaSession.AcceptRtpFromAny = true;
        voipMediaSession.OnRtpPacketReceived += OnRtpPacketReceived;

        await Task.Delay(500);

        await ua.Answer(uas, voipMediaSession);

        if (ua.IsCallActive)
        {
            await voipMediaSession.Start();
            await winAudioEP.PauseAudioSink();
        }
    };
};

_userAgent.Start();

Console.ReadLine();
_ua.Hangup();
_waveFile?.Close();

_userAgent.Stop();
Task.Delay(1500).Wait();
_sipTransport.Shutdown();

}

private static void OnRtpPacketReceived(IPEndPoint remoteEndPoint, SDPMediaTypesEnum mediaType, RTPPacket rtpPacket)
{
if (mediaType == SDPMediaTypesEnum.audio)
{
var sample = rtpPacket.Payload;

    if (rtpPacket.Header.PayloadType == (int)SDPWellKnownMediaFormatsEnum.G729)
    {
        G729Decoder decoder = new G729Decoder();
        byte[] pcmSample = decoder.Process(sample);

        Console.WriteLine(pcmSample.Length);



        _waveFile.Write(pcmSample, 0, pcmSample.Length);
    }
    else {

        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);
            }
        }
    }
}

}`

@sipsorcery
Copy link
Member

Did you see the RecordIncomingCall example?

@Rac0n
Copy link
Author

Rac0n commented Oct 29, 2024

Hello! I did look at the example! I also managed to find the problem. I used a different codec instead of G729. I used G711 and everything worked. Thanks for the response!

@sipsorcery
Copy link
Member

Gr8, glad you solved it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants