Home > Code > C# > How to make SIP video calls in C#

How to make SIP video calls in C#

by Sacha Manji   on Jan 26, 2015   Category: C#   |  Views: 1521    |  Points: 25   |  Starter 


While searching on the Internet on how to make SIP video calls using C#, I recognised that there aren’t any brief and straightforward tutorial in this topic. I found multi-page articles (sorry, but some of them are full of bullsh*t) and neverending forum threads, but none of them provided me complete solution. Therefore, I undertook to create a short and concise guide on how to make video calls in C# using the VoIP technology.

Look at the prerequisites:

- PBX: To be able to make and receive video calls you, a phone system (e.g. Asterisk) is essentially needed. You need to add a new SIP account in your PBX for this application.

- Visual Studio: This solution is based on a console softphone, so a new Visual C# Console Application is just enough.

- VoIPSDK.dll: I used prewritten VoIP components to implement the SIP video calling feature. The necessary .dll file can be found on this website: http://www.voip-sip-sdk.com/. It should be added to your references.

- Test phone: To test your application you can use any VoIP phone stat supports video calling (e.g. Bria softphone).

Now take a look at the code. As you can see below, just a few lines of C# code are enough to connect the application to a PBX and to initiate a video call. Firstly, you need to perform the SIP registration tasks. You need to create a softphone and a phone line object, then you need to specify the SIP account to be used for the phone line. These configurations are needed to be able to register to your PBX. After calling the RegisterPhoneLine method the regsitration procedure starts, and the application will indicates its status due to the mySoftphone_PhoneLineStateChanged method. The PhoneCallVideoSender and PhoneCallVideoReceiver objects are responsible for video handling. To handle the USB webcamera, the WebCamera object can be used. The CallType class is used to identify whether the call is a video or an audio call.

The C# code to make a SIP video call:


using System;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.Video;
using Ozeki.VoIP;
using Ozeki.VoIP.SDK;

namespace Video_call
{
internal class Program
{
private static ISoftPhone softphone; // softphone object
private static IPhoneLine phoneLine; // phoneline object
private static IPhoneCall call;
private static string numberToDial;
private static MediaConnector mediaConnector;

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 = "444";
var displayName = "444";
var authenticationId = "444";
var registerPassword = "444";
var domainHost = "192.168.115.25";
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)
{
Console.WriteLine(e.State);
if (e.State == RegState.RegistrationSucceeded)
CreateCall();
}

static void CreateCall()
{
//numberToDial = "333";
//call = softphone.CreateCallObject(phoneLine, numberToDial);
var dialParams = new DialParameters("333");
dialParams.CallType = CallType.AudioVideo;
call = softphone.CreateCallObject(phoneLine, dialParams);


mediaConnector = new MediaConnector();
var phoneCallVideoSender = new PhoneCallVideoSender();
var cam = WebCamera.GetDefaultDevice();

if (cam != null)
{
cam.Start();
mediaConnector.Connect(cam, phoneCallVideoSender);
}

phoneCallVideoSender.AttachToCall(call);

call.CallStateChanged += call_CallStateChanged;
call.Start();
}

static void call_CallStateChanged(object sender, CallStateChangedArgs e)
{
Console.WriteLine("\nCall state: {0}.", e.State);
}
}
}


Test the program:

To make a test call, provide valid SIP account details for this console application to be able to register to your PBX, then specify a telephone number to be dialled (it can be an other SIP account that has been previously registered to the PBX). After running the application, it dials the provided phone number automatically meanwhile sending the image of the webcamera.



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts