Search code examples
c#.netasterisksipsip-server

SIP Sorcery (C#) : How the server can reject call & Receiver can detect that server has rejected a call


 SIPUserAgent objSIPUserAgent = new SIPUserAgent();
 WindowsAudioEndPoint objWindowsAudioEndPoint = new WindowsAudioEndPoint(new AudioEncoder());
 VoIPMediaSession objVoIPMediaSession = new VoIPMediaSession(objWindowsAudioEndPoint.ToMediaEndPoints());
 objVoIPMediaSession.AcceptRtpFromAny = true;

 string strDestinationAddress = strDialledNumber + gblStrDomain;
 bool blnCallResult = await objSIPUserAgent.Call(strDestinationAddress, gblStrExtentionNumber, gblStrExtentionPassword, objVoIPMediaSession);
  • Server is calling to the reciver by using above snippet.
  • Receiver notified with incoming call

I have two questions in this scenario.

  1. How the server can reject/cancel outgoing call?
  2. How receiver can detect that the call is rejected/cancelled by server?

Solution

  • bool result = objSIPUserAgent.HangUp();
    

    This can be used to reject or cancel the outgoing call.

    And Below, we can subscribe to the event OnCallRejected which gets fired the call is rejected by the server

    objSIPUserAgent.OnCallRejected += (sender, args) => {
        
    };
    

    Also can OnCallTerminated event . This is fired when the call is cancelled by the server.

    objSIPUserAgent.OnCallTerminated += (sender, args) => {
        if (args.Reason == CallTerminationReason.LocalCanceled) {
            // Handle call canceled event here
        }
    };
    

    Hope this helps.

    OnCallRejected and OnCallTerminated events are not available in version 6.0.12 of the library. These events were added in a later version, specifically version 7.0.0

    For older version,

    objSIPUserAgent.CallHungup += (uas, reason, lastResponse) =>
    {
        if (reason == SIPCallTerminateReasonsEnum.None)
        {
            // The call was terminated normally.
            Console.WriteLine("Call Terminated.");
        }
        else if (reason == 
              SIPCallTerminateReasonsEnum.RemoteCancel) {
            Console.WriteLine("Cancelled by remote party")
         }
    };