Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.79/5 (15 votes)
18 Mar 2011CPOL9 min read 139.6K   11.6K   44  
Essay and tutorial about how to easily build a softphone application

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)


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions