⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sipua.c

📁 未改进的经典的SIP NETWORK API。 请尽快给下载权限
💻 C
📖 第 1 页 / 共 3 页
字号:
/* @@ WARNING: This example is automatically included in the user
   @@ documentation. (Note: The lines containing '@@' are removed
   @@ automatically). */

/*    
 *                        WARNING
 *
 * This sample application uses the sleep() system call
 * in callback routines, for the sake of simplicity. This
 * allows simple coding of the sample, to introcuce user
 * delays or call duration.
 *
 * BUT, a real-world application should never stop during 
 * a callback routine. During the processing of a callback, 
 * no other indications can be delivered, incoming SIP 
 * messages are stored in the SIP stack and cause congestion.
 *
 * If you need to stop or wait for I/O before answering the 
 * request, you must return from the callback and trigger
 * an asynchronous mechanisn (threads, timers, ...) to answer
 * the request at a later time.
 */


#include <string.h>
#include <stdio.h>
#include <sys/signal.h>
#include <pthread.h>
#include <arpa/inet.h>

#include "sip_api.h"

sip_bool_t     sip_exit               = SIP_FALSE;
sip_bool_t     interactive            = SIP_FALSE;
sip_bool_t     no_bye                 = SIP_FALSE;
sip_bool_t     no_delay               = SIP_FALSE;
sip_bool_t     auto_exit              = SIP_TRUE;
int            cancel_delay           = 0;
int            uas_bye_delay          = 0;
int            uas_invite_delay       = 0;
char         * subscribe_event_name   = NULL;
int            unscuscribe_counter    = 0;
/*
 * FACILITY:
 *
 *      sip_call_on_initiate
 *
 * ABSTRACT:
 *
 *      The SIP call initiate reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      request_uri   : The request URI to forward the INVITE
 *      from          : The from header
 *      to            : The to header
 *      contact       : The contact header
 *      request       : Additional headers in the INVITE
 *      body          : The INVITE body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_initiate
 (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_t            * from,
    sip_msg_header_t            * to,
    sip_msg_header_t            * contact,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  printf("entering call_on_initiate\n");

  sip_status_t    sip_status;
  sip_msg_header_t * contact_response;
  char from_string[255];
  char req_uri_string[255];

  if(sip_msg_print_header(from, from_string, 255, SIP_FALSE, NULL) > 0) {
    printf("Call received %s.\n",from_string);
  }
  if(sip_msg_print_uri(request_uri, req_uri_string, 255, NULL) > 0) {
    printf("Call received request_uri: %s.\n",req_uri_string);
  }
 
  if(uas_invite_delay) {
    printf("Adding a %d sec delay before answering 180 Ringing\n",
	   uas_invite_delay);

    

    sleep(uas_invite_delay);
  }

  /* We first reply with a 180 ringing */
  sip_status = sip_call_progress(instance,
                                 call,
                                 dialog,
                                 transaction,
                                 180,
                                 "Ringing",
                                 NULL,
                                 NULL,
                                 SIP_FALSE,
                                 NULL);
  if (sip_status != SIP_NORMAL)
  {
    printf("Error %d in sending a call in progress\n", sip_status);
    return SIP_NORMAL;
  }

  if(!no_delay) {
    printf("Waiting 3 seconds before accepting the call...\n");
    sleep(3);
  }

  contact_response = sip_msg_new_contact_header
    (request_uri, NULL, -1, -1, NULL, NULL);

  if(!contact_response) {
    printf("Unable to build the Contact: header\n");
    exit(1);
  }
  
  if(!body) {
    sip_msg_body_t     * sdp_body;
    sdp_announcement_t * sdp;

    sdp = sdp_new_announcement(0, 
                               "media", 
                               "user1", 
                               "12341234", 
                               "2",
                               "IN",
                               "IP4",
                               "127.0.0.2",
                               NULL);
    
    if(!sdp) {
      printf("Unable to build SDP announcement\n");
      exit(1);
    }
    
    sdp_body = sip_msg_new_sdp_body(sdp,
                                    SIP_FALSE, NULL);
    
    /* Then send a 200 OK */
    sip_status = sip_call_accept(instance,
                                 call,
                                 dialog,
                                 transaction,
                                 200,
                                 "OK",
                                 contact_response,
                                 NULL,
                                 sdp_body,
                                 NULL);

    sip_msg_free_body(sdp_body);

  } else {

    /* Then send a 200 OK */
    sip_status = sip_call_accept(instance,
                                 call,
                                 dialog,
                                 transaction,
                                 200,
                                 "OK",
                                 contact_response,
                                 NULL,
                                 body,
                                 NULL);

  }

  if (sip_status != SIP_NORMAL)
  {
    printf("The call can't be accepted, rc = %d.\n", sip_status);
  } else {
    printf("Call accepted...\n");
  }

  sip_msg_free_header(contact_response);

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_connect
 *
 * ABSTRACT:
 *
 *      The SIP call connect reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call handling instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      request_uri   : The request URI to forward the INVITE
 *      request       : The request headers
 *      body          : The message body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_connect
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  printf("The call is established\n");
  printf("received ACK\n");

  if(uas_bye_delay) {
    sip_trans_t new_trans;

    printf("Waiting %d second before releasing the call\n",
	   uas_bye_delay);
    sleep(uas_bye_delay);
    
    printf("Releasing the call.\n");

    /* send a CANCEL request */
    sip_call_release(instance,
		     call,
		     dialog,
		     &new_trans,
		     NULL,
		     NULL,
		     SIP_CALL_RELEASE_AUTOMATIC,
                     user_info);
  }

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_progress
 *
 * ABSTRACT:
 *
 *      The SIP call Ringing reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      status_code   : The status code (1xx) received in the response 
 *      reason_phrase : The character string in the received status line
 *      response      : Additional headers in the response
 *      body          : The received body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_progress
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_uint32_t                  status_code,
    char                        * reason_phrase,
    sip_msg_header_vector_t     * response,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  printf("Progressing: %s...\n", reason_phrase);

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_accept
 *
 * ABSTRACT:
 *
 *      The SIP call accept reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      status_code   : The status code (200) received in the response 
 *      reason_phrase : The character string in the received status line
 *      response      : Additional headers in the response
 *      body          : The received body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_accept
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_uint32_t                  status_code,
    char                        * reason_phrase,
    sip_msg_header_vector_t     * response,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  sip_trans_t new_trans;

  printf("The call has been accepted\n");

  /* send ACK request */
  sip_call_connect(instance,
                   call,
                   dialog,
                   transaction,
                   NULL,
                   NULL,
                   NULL);

  if(!no_bye) {

    printf("Waiting 3 seconds before releasing the call...\n");
    sleep(3);
    printf("Sending BYE request...\n");
    
    /* send BYE request */
    sip_call_release(instance,
		     call,
		     dialog,
		     &new_trans,                   
		     NULL,
		     NULL,
		     SIP_CALL_RELEASE_GRACEFUL,
                     user_info);
    
    printf("The call is terminated.\n");
    
    sip_exit = SIP_TRUE;
  }
  
  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_release
 *
 * ABSTRACT:
 *
 *      The SIP call release reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call handling instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      request_uri   : The request URI in the message
 *      request       : Additional headers in the INVITE
 *      body          : The received body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_release
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  sip_call_reply(instance,
                 call,
                 dialog,
                 transaction,
                 SIP_CALL_NO_SUBSCRIPTION,
                 200,
                 NULL,
                 NULL,
                 NULL,
                 SIP_FALSE,
                 NULL);

  printf("The current call has been released\n");
  printf("BYE received!!!\n");
  
  sip_exit = SIP_TRUE;

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_cancel
 *
 * ABSTRACT:
 *
 *      The SIP call cancel reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call handling instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      request_uri   : The request URI in the message
 *      request       : Additional headers in the INVITE
 *      body          : The received body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_cancel
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   trans,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
   )
{
  printf("The current call has been canceled\n");

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_get_media
 *
 * ABSTRACT:
 *
 *      The SIP get_media reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -