Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

How to Build a .NET Softphone in C# with SIP, SDP, RTP and RTCP

, 18 Mar 2011 CPOL
Essay and tutorial about how to easily build a softphone application

Editorial Note

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VoIP;
using VoIP.SDK;

namespace FunnyPhone
{
    class Program
    {
        static Dictionary<string,IPhoneCall> Calls;
        static PongCallListener FunnyCallListener;

        static void Main(string[] args)
        {
            ISoftPhone SoftPhone = new SoftPhone("", 5000, 8000, 5060);
            //ISoftPhone SoftPhone = new VoIP.SDK.Mock.ArbSoftPhone();
            SoftPhone.IncommingCall += (SoftPhone_IncommingCall);
            IPhoneLine PhoneLine = null;
            FunnyCallListener = new PongCallListener();

            Calls = new Dictionary<string, IPhoneCall>();

            Console.WriteLine("Be funny!");

            Console.Write("Display name: "); string displayName = Console.ReadLine();
            Console.Write("Username: "); string username = Console.ReadLine();
            Console.Write("Register name: "); string registerName = Console.ReadLine();
            Console.Write("Register password: "); string registerPassword = Console.ReadLine();
            Console.Write("Domain server: "); string domainServer = Console.ReadLine();

            string[] domains = domainServer.Split(':');
            int port = 5060;
            if (domains.Length == 2)
                port = Int32.Parse(domains[1]);
            SIPAccount account = new SIPAccount(true, displayName, username, registerName, registerPassword, domains[0], port);
            PhoneLine = SoftPhone.CreateAndRegisterPhoneLine(account);

            while (true)
            {
                string statement = Console.ReadLine().Trim();
                if (statement.StartsWith("exit"))
                    break;

                if (!Calls.ContainsKey(statement))
                {
                    if (PhoneLine.RegisteredInfo == PhoneLineInformation.RegistrationSucceded)
                    {
                        IPhoneCall Call = SoftPhone.CreateCallObject(PhoneLine, statement, FunnyCallListener);
                        Calls.Add(statement, Call);
                        Call.CallStateChanged += (Call_CallStateChanged);
                        Call.Start();
                    }
                }
            }
            foreach (IPhoneCall call in Calls.Values)
                call.HangUp();
            SoftPhone.Close();
        }

        static void SoftPhone_IncommingCall(object sender, VoIPEventArgs<IPhoneCall> e)
        {
            e.Item.AttachListener(FunnyCallListener);
            e.Item.CallStateChanged += (Call_CallStateChanged);
            Calls.Add(e.Item.DialInfo, e.Item);
            e.Item.Accept();
        }

        static void Call_CallStateChanged(object sender, VoIPEventArgs<CallState> e)
        {
            if (e.Item > CallState.InCall)
            {
                IPhoneCall call = sender as IPhoneCall;
                if (call == null)
                    return;

                Calls.Remove(call.DialInfo);

                call.DetachListener(FunnyCallListener);
                call.CallStateChanged -= (Call_CallStateChanged);
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 18 Mar 2011
Article Copyright 2011 by Esteban Murandi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid