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


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 = "";
            var domainPort = 5060;

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

            // Send SIP regitration request

            // Prevents the termination of the application

        static void RegisterAccount(SIPAccount account)
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += sipAccount_RegStateChanged;
            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)

                    if (codec.PayloadType == 8)


        static void CreateCall()
            var numberToDial = "1001";
            call = softphone.CreateCallObject(phoneLine, numberToDial);
            call.CallStateChanged += call_CallStateChanged;
        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@ SIP/2.0
Via: SIP/2.0/UDP;branch=z9hG4bK2cf5f019-d164-4043-b86d-
From: "1000";tag=whxmonoc
Call-ID: eoybjbgdxndrmhgmxbhlkpywvkmwymjltppjsijitjdnkeyxqw
Max-Forwards: 70
User-Agent: Ozeki VoIP SIP SDK v10.1.8
Content-Type: application/sdp
Proxy-Authorization:Digest username="1000",realm="OzekiPBX",
Content-Length: 195

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

If you have any questions or need assistance, please contact us at

Related Pages

Copyright © 2000- - Ozeki Ltd |
Legal | Privacy | Terms of use | 7357 | | Login