How to make SIP video calls in C#
Join the DZone community and get the full member experience.
Join For Freewhile 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:
c# voip stack
. 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.
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.
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);
}
}
}
Opinions expressed by DZone contributors are their own.
Comments