How to make SIP video calls in C#


/ Published in: C#
Save to your folder(s)

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.

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.


Copy this code and paste it in your HTML
  1. using System;
  2. using Ozeki.Media.MediaHandlers;
  3. using Ozeki.Media.MediaHandlers.Video;
  4. using Ozeki.VoIP;
  5. using Ozeki.VoIP.SDK;
  6.  
  7. namespace Video_call
  8. {
  9. internal class Program
  10. {
  11. private static ISoftPhone softphone; // softphone object
  12. private static IPhoneLine phoneLine; // phoneline object
  13. private static IPhoneCall call;
  14. private static string numberToDial;
  15. private static MediaConnector mediaConnector;
  16.  
  17. private static void Main(string[] args)
  18. {
  19. // Create a softphone object with RTP port range 5000-10000
  20. softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
  21.  
  22. // SIP account registration data, (supplied by your VoIP service provider)
  23. var registrationRequired = true;
  24. var userName = "444";
  25. var displayName = "444";
  26. var authenticationId = "444";
  27. var registerPassword = "444";
  28. var domainHost = "192.168.115.25";
  29. var domainPort = 5060;
  30.  
  31. var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
  32.  
  33. // Send SIP regitration request
  34. RegisterAccount(account);
  35.  
  36. // Prevents the termination of the application
  37. Console.ReadLine();
  38. }
  39.  
  40. static void RegisterAccount(SIPAccount account)
  41. {
  42. try
  43. {
  44. phoneLine = softphone.CreatePhoneLine(account);
  45. phoneLine.RegistrationStateChanged += sipAccount_RegStateChanged;
  46. softphone.RegisterPhoneLine(phoneLine);
  47. }
  48. catch (Exception ex)
  49. {
  50. Console.WriteLine("Error during SIP registration: " + ex);
  51. }
  52. }
  53.  
  54. private static void sipAccount_RegStateChanged(object sender, RegistrationStateChangedArgs e)
  55. {
  56. Console.WriteLine(e.State);
  57. if (e.State == RegState.RegistrationSucceeded)
  58. CreateCall();
  59. }
  60.  
  61. static void CreateCall()
  62. {
  63. //numberToDial = "333";
  64. //call = softphone.CreateCallObject(phoneLine, numberToDial);
  65. var dialParams = new DialParameters("333");
  66. dialParams.CallType = CallType.AudioVideo;
  67. call = softphone.CreateCallObject(phoneLine, dialParams);
  68.  
  69.  
  70. mediaConnector = new MediaConnector();
  71. var phoneCallVideoSender = new PhoneCallVideoSender();
  72. var cam = WebCamera.GetDefaultDevice();
  73.  
  74. if (cam != null)
  75. {
  76. cam.Start();
  77. mediaConnector.Connect(cam, phoneCallVideoSender);
  78. }
  79.  
  80. phoneCallVideoSender.AttachToCall(call);
  81.  
  82. call.CallStateChanged += call_CallStateChanged;
  83. call.Start();
  84. }
  85.  
  86. static void call_CallStateChanged(object sender, CallStateChangedArgs e)
  87. {
  88. Console.WriteLine("\nCall state: {0}.", e.State);
  89. }
  90. }
  91. }

URL: http://www.voip-sip-sdk.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.