Course 2 / Lecture 1

How to build a VoIP PBX in C#

How to create a simple VoIP PBX in C#

Ozeki VoIP SIP SDK provides revolutionary technology for creating your own SIP PBX. To make your work easier it offers you default VoIP PBX behavior. This page explains the essential terms concerning simple VoIP PBX functionalities and how you can start to develop your own PBX with Ozeki SIP SDK.

Download: base-pbx.zip

Introduction

Please be informed that PBX development support is available in Ozeki VoIP SIP SDK from version 10.0. If you use an older version, please update your Ozeki SIP SDK. You can download the latest version from the download page.

A Private Branch Exchange (PBX) is a hardware or software solution for establishing communication lines between different types of communication end-points, regardless of using wired, mobile or VoIP technology (Figure 1). The PBX establishes the communication line between the end points through SIP accounts and SIP messages.

The core PBX functionalities are provided by Ozeki VoIP SIP SDK. This article introduces how you can implement a PBX software with default behavior. This simple PBX will be capable for handling registration requests from any extensions with any SIP accounts, and the calling process will be done in between two end points in the simplest way. It means that one party calls a number and the end point with that number will ring. When the callee answers the call, the phone line establishes and the communication can be done.

The following program code uses the background support of Ozeki VoIP SIP SDK, therefore you will need to download and install Ozeki SIP SDK on your computer before starting to use the program code. You will also need to have Visual Studio 2010 or compatible IDE and .NET Framework installed on your system, as the program code below is written in C# language.

When you want to write your own PBX software you will need to create a C# console application as a PBX does not necessarily need a graphical user interface. You will need to set the Ozeki VoIP SIP SDK as a reference for your project.

If you want to use the Ozeki provided tools without labeling, you will need some using lines in the code (Code 1). These two directives are enough for the basic PBX functionality implementation.

using Ozeki.Network;
using Ozeki.VoIP;
using Ozeki.VoIP.PBX;
using Ozeki.VoIP.PBX.Extensions;
using Ozeki.VoIP.SIP;
Code 1 - Using the Ozeki VoIP SDK packages

As for using the inbuilt PBX functionality of Ozeki VoIP SIP SDK, your PBX class needs to be derived from the PBXBase class provided by the SDK. This class implements a standard PBX behavior that can be redefined if you wish. In this example, the PBX will use the default behavior provided by Ozeki SIP SDK (Code 2).

class MyPBX : PBXBase
Code 2 - The PBX class needs to be derived from the PBXBase class

The constructor of the PBX class does not need to be rewritten, you can use the one of the base class. In this example, the constructor is extended with some logging information (Code 3) that is printed on the console. The main behavior of the constructor, however, is not overridden.

		public MyPBX(string localAddress, int minPortRange, int maxPortRange) : base(minPortRange, maxPortRange)
        {
            this.localAddress = localAddress;
            Console.WriteLine("PBX starting...");
            Console.WriteLine("Local address: " + localAddress);
        }
Code 3 - PBX constructor

The only method that you need to redefine in your PBX class is the one shown in Code 4. You need to set the listening port for the PBX system in order to reach it from the communication end points. The other code lines are printing log information on the console.

protected override void OnStart()
{
    SetListenPort(localAddress, 5060, TransportType.Udp);
    Console.WriteLine("Listened port: 5060(UDP)");

	Console.WriteLine("PBX started.");
    base.OnStart();
}
Code 4 - Listening port specification

The PBXBase class provides methods for extension registration and unregistration handling. In this example these methods are extended with log printing instructions but above that they use the provided implementation. Code 5 shows the registration method.

		protected override RegisterResult OnRegisterReceived(ISIPExtension extension, SIPAddress from, int expires)
        {
            Console.WriteLine("Register received from: " + extension.ExtensionID);
            return base.OnRegisterReceived(extension, from, expires);
        }
Code 5 - Extension registration

In Code 6 you can see the extension unregistration handling method. In this case the method is just as simple as the registration one. It uses the Ozeki provided implementation extended with a log printing instruction.

		protected override void OnUnregisterReceived(ISIPExtension extension)
        {
            Console.WriteLine("Unregister received from: " + extension.ExtensionID);
            base.OnUnregisterReceived(extension);
        }
Code 6 - Extension unregister

The PBX also needs to handle the call requests. The PBXBase class provides a standard implementation for this purpose too, you only need to call it, or, as in this example, you can extend it with some log printing (Code 7);

		protected override void OnCallRequestReceived(ISessionCall call)
        {
            Console.WriteLine("Call request received. Caller: " + call.DialInfo.CallerID + " callee: " + call.DialInfo.Dialed);
            base.OnCallRequestReceived(call);
        }
Code 7 - Call request handling

As you could see, implementing a basic PBX system is a piece of cake with Ozeki VoIP SIP SDK. In fact you only needed to override the OnStart method for the proper running. All the other codes are only for log extensions.

If you have any questions or need assistance, please contact us at info@voip-sip-sdk.com

You can select a suitable Ozeki VoIP SIP SDK license for building your PBX on licensing information page

Related Pages

More information