info@voip-sip-sdk.com Tel: +36 1 371 0150

How to handle codecs? C# example for enabling and disabling codecs

Download: Codecs_handler.zip

Ozeki VoIP SIP SDK gives you the opportunity to use various audio and video codecs in order to provide outstanding voice and video quality. This article will be a great help in identifying video quality issues and in finding the best solution to avoid them. This example demonstrates how to set the selected codecs in c#. 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 SIP INVITE article before you begin to study how to handle codecs.

What does codec handling mean? When is it needed?

VoIP video communication is based on some background implementation of coder and decoder definitions called codecs. These standard tools are used for defining the digitization and digital to analog conversion methods for video data streams. The codecs also hold information about the compression of video data with saving the quality.


Figure 1 - Making video calls with codec information

The video quality is improving day by day and the video communication also requires more clear and smooth video and sound quality. This all depends on the codecs you use when implementing a VoIP solution.

Ozeki VoIP SIP SDK provides reliable support for several video codecs that can be used in video communication and also for capturing and replaying video phoning sessions. Codec handling means that out of the several supported codecs you can determine which one(s) you wish to use during a given call. This way, you can enhance compatibility and customize your calls.

How to handle codecs using C#?

With the help of the softphone object, you can have access to the list of available codecs. In order for you to be able to disable a codec, the DisableCodec() method is necessary, while for enabling a codec, you should use the EnableCodec() method. Both of them work in a similar manner. All you have to do is to provide the payloadtype of the given codec. (This is a number which identifies the given codec.) With these, you can specify what kind of codec is used under your call. (see the example) If you do not wish to manually provide such codecs, the default codec settings will be valid.

Codecs handler example in C#

using System;
using Ozeki.VoIP;

namespace Codecs_handler
{
    class Program
    {
        static ISoftPhone softphone;   // softphone object
        static IPhoneLine phoneLine;   // phoneline object
        static IPhoneCall call;

        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 = "sipusername";
            var displayName = "sipdisplayname";
            var authenticationId = "authenticationid";
            var registerPassword = "Password";
            var domainHost = "pbxip.voipprovider.com";
            var domainPort = 5060;

            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);

            // Send SIP regitration request
            RegisterAccount(account);

            // Prevents the termination of the application
            Console.ReadLine();
        }

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

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

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

                foreach (var codec in softphone.Codecs)
                {
                    softphone.DisableCodec(codec.PayloadType);

                    if (codec.PayloadType == 8)
                        softphone.EnableCodec(codec.PayloadType);
                }

                CreateCall();
            }
        }

        static void CreateCall()
        {  
            var numberToDial = "1001";
            call = softphone.CreateCallObject(phoneLine, numberToDial);
            call.CallStateChanged += call_CallStateChanged;
            call.Start();
        }  
  
        static void call_CallStateChanged(object sender, CallStateChangedArgs e)
        {
            Console.WriteLine("Call state: {0}.", e.State);
        }
    }
}

Training guides, simple examples

If you would like to visit the detailed developer documentations, you can learn much more from the Ozeki VoIP SIP SDK Training chapter.

SIP INVITE PDU sent over the network

In the SIP INVITE PDU, you can see which codecs will be used during the call. This example used only the PCMA codec.

Invite request (UDP message, Softphone -> Softphone)

INVITE sip:1001@192.168.115.172 SIP/2.0
Via: SIP/2.0/UDP 192.168.115.172:7368;branch=z9hG4bK2cf5f019-d164-4043-b86d-
c0e887fa3f48;rport
To: 
From: "1000";tag=whxmonoc
CSeq: 2 INVITE
Call-ID: eoybjbgdxndrmhgmxbhlkpywvkmwymjltppjsijitjdnkeyxqw
Max-Forwards: 70
Contact: 
User-Agent: Ozeki VoIP SIP SDK v10.1.8
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, SUBSCRIBE, NOTIFY, REFER, INFO, MESSAGE
Content-Type: application/sdp
Proxy-Authorization:Digest username="1000",realm="OzekiPBX",
nonce="4756c1d83e9f422c9ed2476c479a52a2", 
response="18a149b7a9780e8da1f62d8e401d7b18",uri="sip:1001@192.168.115.172",
algorithm=MD5
Content-Length: 195

v=0
o=- 2012526930 2012526930 IN IP4 192.168.115.172
s=Ozeki VoIP SIP SDK v10.1.8
c=IN IP4 192.168.115.172
t=0 0
m=audio 7445 RTP/AVP 8
a=rtpmap:8 PCMA/8000
a=sendrecv
m=video 0 RTP/AVP

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

Related Pages

Ozeki Cookie Policy
Ozeki Informatics Ltd uses cookies to provide you the best experience on this website. The further use of the website will be considered as an agreement to the use of cookies. For more information read this website.

Cookies are enabled You are browsing the optimized version of this website. For more information read this website.