Home > Code > C# > How to record voice calls (C#, SIP)

How to record voice calls (C#, SIP)

by Jay H.   on Feb 02, 2015   Category: C#   |  Views: 1158    |  Points: 25   |  Starter 


Do you need a couple of lines of codes to implement voice call recording? Just copy and use this code! It really helped me! To share my success, this content is intended to be a short tutorial on how to record SIP voice calls using C#.NET. This guide is recommended for intermediate-level C# developers primarily, since only one complete code is presented instead of explaining its snippets step-by-step. First of all, carry out the following configuration steps to make sure that your system is ready for coding:

• Create a new project (Visual C# Console Application) in Visual Studio

• Add the VoIPSDK.dll file to your references - it is available on this website: www.voip-sip-sdk.com

• Install a VoIP PBX (allowing you to initiate and accept voice calls) and create a new SIP account for the call recorder

The brief description of the code: After the necessary using lines there is a need a softphone and a phone line object, and some further media handler objects to be able to record audio streams into .wav audio files. Thereafter, you need to specify a SIP account for the call recorder (including the data of your PBX). To ensure that the two audio streams will be mixed you need to connect the microphone and the mediaReceiver objects to the mixer. After this you need to connect the mixer to the recorder that will record the mixed audio stream into the file specified by the filename parameter. The rest of the code presents how to save and store a call as .wav file.


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

namespace Record_Voice_Call
{
class Program
{
static ISoftPhone softphone; // softphone object
static IPhoneLine phoneLine; // phoneline object
static IPhoneCall call;
static string caller;
static string filename;
static Microphone microphone;
static Speaker speaker;
static PhoneCallAudioSender mediaSender;
static PhoneCallAudioReceiver mediaReceiver;
static MediaConnector connector;
static WaveStreamRecorder recorder;

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 = "100";
var displayName = "100";
var authenticationId = "100";
var registerPassword = "100";
var domainHost = "192.168.114.7";
var domainPort = 5060;

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

// Send SIP registration request
RegisterAccount(account);

microphone = Microphone.GetDefaultDevice();
speaker = Speaker.GetDefaultDevice();
mediaSender = new PhoneCallAudioSender();
mediaReceiver = new PhoneCallAudioReceiver();
connector = new MediaConnector();

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

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

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

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

static void softphone_IncomingCall(object sender, VoIPEventArgs<IPhoneCall> e)
{
call = e.Item;
caller = call.DialInfo.CallerID;
call.CallStateChanged += call_CallStateChanged;
call.Answer();
}

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

if (e.State == CallState.Answered)
SetupDevices();

if (e.State.IsCallEnded())
CloseDevices();
}

static void SetupDevices()
{
microphone.Start();
connector.Connect(microphone, mediaSender);

speaker.Start();
connector.Connect(mediaReceiver, speaker);

filename = string.Format("{0}-{1}-{2}-{3}.wav", caller, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); // a string to generate file name
recorder = new WaveStreamRecorder(filename); // new recorder object

connector.Connect(microphone, recorder); // connects the outgoing voice to the recorder
connector.Connect(mediaReceiver, recorder); // connects the incoming voice to the recorder

mediaSender.AttachToCall(call);
mediaReceiver.AttachToCall(call);

recorder.Start(); // starts the recording
}

static void CloseDevices()
{
recorder.Dispose();
microphone.Dispose();
speaker.Dispose();

mediaReceiver.Detach();
mediaSender.Detach();
connector.Dispose();
}
}
}



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