Esempio n. 1
0
// helper for setting up sip library pjsua
static void setup_sip(void)
{
	pj_status_t status;
	
	log_message("Setting up pjsua ... ");
	
	// create pjsua  
	status = pjsua_create();
	if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
	
	// configure pjsua	
	pjsua_config cfg;
	pjsua_config_default(&cfg);
	
	// enable just 1 simultaneous call 
	cfg.max_calls = 1;
		
	// callback configuration		
	cfg.cb.on_call_media_state = &on_call_media_state;
	cfg.cb.on_call_state = &on_call_state;
		
	// logging configuration
	pjsua_logging_config log_cfg;		
	pjsua_logging_config_default(&log_cfg);
	log_cfg.console_level = PJSUA_LOG_LEVEL;
		
	// initialize pjsua 
	status = pjsua_init(&cfg, &log_cfg, NULL);
	if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
	
	// add udp transport
	pjsua_transport_config udpcfg;
	pjsua_transport_config_default(&udpcfg);
		
	udpcfg.port = 5060;
	status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &udpcfg, NULL);
	if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
	
	// initialization is done, start pjsua
	status = pjsua_start();
	if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
	
	// disable sound - use null sound device
	status = pjsua_set_null_snd_dev();
	if (status != PJ_SUCCESS) error_exit("Error disabling audio", status);
	
	log_message("Done.\n");
}
Esempio n. 2
0
bool SipPhone::init_pjsua() {
  Logger::debug("SipPhone::init_pjsua...");

  // create pjsua  
  pj_status_t status = pjsua_create();
  if (status != PJ_SUCCESS) {
    Logger::error("pjsua_create() failed (%s)", Helper::getPjStatusAsString(status).c_str());
    return false;
  }

  // configure pjsua
  pjsua_config ua_cfg;
  pjsua_config_default(&ua_cfg);
  // enable just 1 simultaneous call 
  ua_cfg.max_calls = 1; // TODO
  // callback configuration
  ua_cfg.cb.on_call_state = &SipAccount::onCallStateCB;
  ua_cfg.cb.on_incoming_call = &SipAccount::onIncomingCallCB;
  //ua_cfg.cb.on_call_media_state = &SipAccount::onCallMediaStateCB;

  // logging configuration
  pjsua_logging_config log_cfg;    
  pjsua_logging_config_default(&log_cfg);
  log_cfg.level = pj_log_get_level();

  // media configuration
  pjsua_media_config media_cfg;
  pjsua_media_config_default(&media_cfg);
  media_cfg.clock_rate = 8000; // TODO: default of 16000 seems not to work :-(

  // initialize pjsua 
  status = pjsua_init(&ua_cfg, &log_cfg, &media_cfg);
  if (status != PJ_SUCCESS) {
    Logger::error("pjsua_init() failed (%s)", Helper::getPjStatusAsString(status).c_str());
    return false;
  }

  // add udp transport
  pjsua_transport_config udpcfg;
  pjsua_transport_config_default(&udpcfg);

  status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &udpcfg, NULL);
  if (status != PJ_SUCCESS) {
    Logger::error("pjsua_transport_create() failed (%s)", Helper::getPjStatusAsString(status).c_str());
    return false;
  }

  // disable sound device - use null sound device
  status = pjsua_set_null_snd_dev();
  if (status != PJ_SUCCESS) {
    Logger::error("pjsua_set_null_snd_dev() failed (%s)", Helper::getPjStatusAsString(status).c_str());
    return false;
  }

  // initialization is done, start pjsua
  status = pjsua_start();
  if (status != PJ_SUCCESS) {
    Logger::error("pjsua_start() failed (%s)", Helper::getPjStatusAsString(status).c_str());
    return false;
  }

#if 0
  m_Pool = pjsua_pool_create("SipPhone.cpp", 128, 128);
  if (m_Pool == NULL) {
    Logger::error("pjsua_pool_create() failed");
    return false;
  }
#endif

  return true;
}
Esempio n. 3
0
/*
 * main()
 *
 * argv[] contains the registration information.
 */
int main(int argc, char *argv[]) {
  pj_status_t status;
  int loglevel = 0;

  if (argc < 4) {
    usage(argv[0]);
    exit(1);
  }

  if (argc > 5){
    sscanf(argv[5], "%d", &loglevel);
  }

  pjsua_acc_id acc_id;

  /* Create pjsua first! */
  status = pjsua_create();
  if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);


  /* Init pjsua */
  {
    pjsua_config cfg;
    pjsua_logging_config log_cfg;
    pjsua_media_config media_cfg;

    // This will prevent the SIP stack from trying to switch to TCP.
    // It will prevent the stack from giving "Transport unavailable" errors.
    // http://trac.pjsip.org/repos/wiki/Using_SIP_TCP
    pjsip_cfg()->endpt.disable_tcp_switch = PJ_TRUE;

    pjsua_config_default(&cfg);
    cfg.cb.on_incoming_call    = &on_incoming_call;
    cfg.cb.on_call_media_state = &on_call_media_state;
    cfg.cb.on_call_state       = &on_call_state;
    cfg.cb.on_call_tsx_state   = &on_call_tsx_state;

    pjsua_logging_config_default(&log_cfg);
    log_cfg.console_level = loglevel; // 0 = Mute console, 3 = somewhat useful, 4 = overly verbose.
    pjsua_media_config_default(&media_cfg);

    status = pjsua_init(&cfg, &log_cfg, &media_cfg);
    if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
  }
  /* Add UDP transport. */
  {
    pjsua_transport_config cfg;

    pjsua_transport_config_default(&cfg);
    sscanf(argv[4], "%d", &cfg.port);
    status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
    if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
    printf("Created UDP transport on port %d.\n", cfg.port);
  }

 /* Initialization is done, now start pjsua */
  status = pjsua_start();
  if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);

  /* Register to SIP server by creating SIP account. */
  {
    char reg_uri_buf[80] = "sip:";

    char uri_buf[80] = "sip:";
    pjsua_acc_config cfg;
    char* username = argv[1];
    char* password = argv[2];
    char* domain   = argv[3];

    strcat (uri_buf, username);
    strcat (reg_uri_buf, domain);

    strcat (uri_buf, "@");
    strcat (uri_buf, domain);

    pjsua_acc_config_default(&cfg);
    cfg.id = pj_str(uri_buf);

    printf("Registering: %.*s.\n", (int)cfg.id.slen, cfg.id.ptr);

    cfg.reg_uri = pj_str(reg_uri_buf);
    cfg.cred_count = 1;
    cfg.cred_info[0].realm = pj_str(domain);
    cfg.cred_info[0].scheme = pj_str("Digest");
    cfg.cred_info[0].username = pj_str(username);
    cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
    cfg.cred_info[0].data = pj_str(password);
    cfg.register_on_acc_add = PJ_FALSE;


    status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
    if (status != PJ_SUCCESS) error_exit("Error adding account", status);

    or_event(OR_READY, "Agent initialized..");

  }

  /* Start by muting the audio device. This a passive agent, and sound
     is just wasted CPU time and uwanted side effects. */
  pjsua_set_null_snd_dev();
  fflush(stdout);
  /* Wait until user press "q" to quit. */
  for (;;) {
    char option[256];

    if (fgets(option, sizeof (option), stdin) == NULL) {
      or_status (OR_ERROR, "EOF while reading stdin, will quit now..");
      break;
    }

    /* Dial command. */
    if (option[0] == 'd') {
      pj_str_t uri = pj_str(&option[1]);
      status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);
      if (status != PJ_SUCCESS) {
        or_status (OR_ERROR, "Could not make call");
      }
      else {
        or_status (OR_OK, "Dialling...");
      }
    }

    /* Register */
    else if (option[0] == 'r') {
      register_account(acc_id);
    }

    /* Unregister */
    else if (option[0] == 'u') {
      unregister_account(acc_id);
    }

    /* Enable autoanswer */
    else if (option[0] == 'a') {
      if (option[1] == '1') {
        autoanswer[0] = true;
        or_status(OR_OK, "Autoanswer enabled.");
      } else {
        or_status(OR_ERROR, "Invalid account.");
      }
    }

    /* Disable autoanswer (manual answer) */
    else if (option[0] == 'm') {
      autoanswer[0] = false;
      or_status(OR_OK, "Autoanswer disabled.");
    }

    /* Pickup incoming call, unsupported for now. */
    else if (option[0] == 'p') {
      if (current_call != PJSUA_INVALID_ID) {
        pjsua_call_answer(current_call, 200, NULL, NULL);
        or_status(OR_OK, "Call picked up.");
      } else {
        or_status(OR_ERROR, "No call to pick up.");
      }
    }

    /* Hang up current call */
    else if (option[0] == 'H') {
      if (current_call != PJSUA_INVALID_ID) {
        pjsua_call_hangup (current_call, 0,NULL, NULL);
        or_status (OR_OK, "Hanging up current call...");
      } else {
        or_status(OR_ERROR, "No call to hang up.");
      }
    }

    /* Full hangup.. */
    else if (option[0] == 'h') {
      pjsua_call_hangup_all();
      or_status (OR_OK, "Hanging up all calls...");
    }

    /* Status  */
    else if (option[0] == 's') {
      or_dump_status();
    }

    /* Exit application. */
    else if (option[0] == 'q') {
      break;
    }

    else {
      or_status (OR_ERROR, "Unknown command:");
    }
  }
  pjsua_destroy();
  or_status (OR_OK, "Exiting...");

  return 0;
}