How to manage multiple phone lines at the same time

This article is a detailed guide about how to manage multiple phone lines when you use Ozeki VoIP SIP SDK. To use this example, you need to have Ozeki VoIP SIP SDK installed, and a reference to ozeki.dll should be added to your visual studio project. It's also recommended to visit the How to register to a SIP PBX article before you begin to study this functionality.

multiple phone lines
Figure 1 - Multiple phone lines

What is multiple phone lines mean? When is it needed?

Advanced VoIP communication has to include the support of multiple phone lines. (Figure 1) It's main advantage is that you can register to more than one VoIP Provider and you can even decide which one of these VoIP Providers you wish to use for the given calls. For instance, if a company has offices on different locations, at any of these locations, it will be possible to register then make calls as if you were at any of the different offices.

How to implement multiple phone lines using C#?

The phone line is directly connected to the SIP account you register to the PBX system. You can register more SIP accounts in your PBX and change them during the softphone runtime. In this case you can choose which line you wish to use for phone calls.

This example program handles two SIP accounts, therefore two phone lines can be used in it. The softphone function is similar in this example to the basic softphones, but there are some differences. The Softphone class stores the two registered phone line objects in phoneLine and phoneLineTwo properties.

Multiple phone line example in C#

using System;
using System.Threading;
using Ozeki.VoIP;

namespace Multiple_phone_lines
{
    class Program
    {
        static ISoftPhone softphone;   // softphone object
        static IPhoneLine phoneLine;   // phoneline object
        static IPhoneLine phoneLineTwo;  // second phoneline object

        private static void Main(string[] args)
        {
            // Create a softphone object with RTP port range 5000-10000
            softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

            // SIP account registration data, (supplied by your VoIP service provider)
            var registrationRequired = true;
            var userName = "858";
            var displayName = "858";
            var authenticationId = "858";
            var registerPassword = "858";
            var domainHost = "192.168.115.100";
            var domainPort = 5060;

            // Second SIP account registration data, (supplied by your VoIP service provider)    
            var registrationRequiredTwo = true;
            var authenticationIdTwo = "859";
            var userNameTwo = "859";
            var displayNameTwo = "859";
            var registerPasswordTwo = "859";
            var domainHostTwo = "192.168.115.100";
            var domainPortTwo = 5060;

            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
            var accountTwo = new SIPAccount(registrationRequiredTwo, displayNameTwo, userNameTwo, authenticationIdTwo, registerPasswordTwo, domainHostTwo, domainPortTwo);

            // Send SIP regitration request
            RegisterAccount(phoneLine, account);
            RegisterAccount(phoneLineTwo, accountTwo);

            // Prevents the termination of the application
            while (true) Thread.Sleep(50);
        }

        static void RegisterAccount(IPhoneLine phoneLine, SIPAccount account)
        {
            try
            {
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += line_RegStateChanged;
                softphone.RegisterPhoneLine(phoneLine);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error during SIP registration: " + ex);
            }
        }

        static void line_RegStateChanged(object sender, RegistrationStateChangedArgs e)
        {
            if (e.State == RegState.NotRegistered || e.State == RegState.Error)
                Console.WriteLine("Registration failed!");

            if (e.State == RegState.RegistrationSucceeded)
            {
                Console.WriteLine("Registration succeeded - Online!");
            }
        }
    }
}

Related Pages

More information