To understand the use of Pjsip, SIMPLE_PJSUA.C is a good example, although the code is only a short line of 172, but it shows the full use of the pjsua-lib layer of the process, registration process and basic call process.
Here are the main processes in the SIMPLE_PJSUA.C in the learning process:
Let's look at the main function of PJSIP-APPS/SRC/SAMPLES/SIMPLE_PJSUA.C.
* * Main () * * argv[1] may contain URL to call.
*/int main (int argc, char *argv[]) {pjsua_acc_id acc_id;
pj_status_t status; Creating PJSIP/* Create Pjsua first!
* * status = Pjsua_create ();
if (status!= pj_success) error_exit ("Error in Pjsua_create ()", status); Verify that the called SIP address is correct/* If argument is specified, it's got to be a valid SIP URL/if (argc > 1) {status = PJSU
A_verify_url (argv[1]);
if (status!= pj_success) error_exit ("Invalid URL in argv", status);
}//Initialize PJSUA, set callback function/* Init Pjsua/{pjsua_config cfg;
Pjsua_logging_config log_cfg;
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;
Pjsua_logging_config_default (&LOG_CFG);
Log_cfg.console_level = 4;
Status = Pjsua_init (&cfg, &log_cfg, NULL);
if (status!= pj_success) error_exit ("Error in Pjsua_init ()", status);
}
Create the PJSIP transport port/* ADD UDP transport.
* * {pjsua_transport_config cfg;
Pjsua_transport_config_default (&CFG);
Cfg.port = 5060;
Status = Pjsua_transport_create (PJSIP_TRANSPORT_UDP, &cfg, NULL);
if (status!= pj_success) error_exit ("Error creating transport", status);
}//Start PJSIP/* initialization is done and now start pjsua/status = Pjsua_start ();
if (status!= pj_success) error_exit ("Error starting Pjsua", status); Set up SIP user account/* Register to SIP server by creating SIP accounts.
* * {pjsua_acc_config cfg;
Pjsua_acc_config_default (&CFG);
Cfg.id = Pj_str ("SIP:" Sip_user "@" Sip_domain);
Cfg.reg_uri = Pj_str ("SIP:" Sip_domain);
Cfg.cred_count = 1;
Cfg.cred_info[0].realm = Pj_str (Sip_domain);
Cfg.cred_info[0].scheme = Pj_str ("digest");
Cfg.cred_info[0].username = Pj_str (Sip_user);
Cfg.cred_info[0].data_type = pjsip_cred_data_plain_passwd;
Cfg.cred_info[0].data = Pj_str (SIP_PASSWD); Status = Pjsua_acc_add (&cfg, PJ_true, &acc_id);
if (status!= pj_success) error_exit ("Error adding account", status); //Initiate a call/* If URL is specified and make called to the URL.
*/if (argc > 1) {pj_str_t URI = Pj_str (argv[1]);
Status = Pjsua_call_make_call (acc_id, &uri, 0, NULL, NULL, NULL);
if (status!= pj_success) error_exit ("Error making call", status); ///Loop wait/* Waiting until user press "Q" to quit. * for (;;)
{char option[10];
Puts ("Press ' h ' to hangup all calls, ' Q ' to quit");
If fgets (option, sizeof (option), stdin) = NULL) {puts ("EOF while reading stdin, would quit now ...");
Break
} if (option[0] = = ' Q ') break;
if (option[0] = = ' h ') pjsua_call_hangup_all ();
}/* Destroy Pjsua * * Pjsua_destroy ();
return 0; }
callback function for caller ID
Caller callback function
/* Callback called by the library upon receiving incoming call
/static void On_incoming_call (PJSUA_ACC_ ID acc_id, pjsua_call_id call_id,
pjsip_rx_data *rdata)
{
pjsua_call_info ci;
Pj_unused_arg (acc_id);
Pj_unused_arg (rdata); obtain call Information
Pjsua_call_get_info (call_id, &ci);
Pj_log (3, (This_file, "incoming called from%.*s!!",
(int) Ci.remote_info.slen,
ci.remote_info.ptr)); Automatic Answer call
/* Automatically answer incoming calls with 200/ok
/pjsua_call_answer (call_id, MB, NULL, NULL);
Call state Change callback function, does not do substantive operations:
Call status Change callback function
/* Callback called by the library when called ' s state has changed
/static void On_call_state (Pjsua_c all_id call_id, pjsip_event *e)
{
pjsua_call_info ci;
Pj_unused_arg (e);
Pjsua_call_get_info (call_id, &ci);
Pj_log (3, (This_file, "Call%d state=%.*s", call_id,
(int) Ci.state_text.slen,
ci.state_text.ptr));
callback function for media State change:
The callback function for media state change/
* Callback called by the library when call ' s media states has changed/
static void On_call_media _state (pjsua_call_id call_id)
{
pjsua_call_info ci;
Pjsua_call_get_info (call_id, &ci); connect the call and sound device
if (ci.media_status = = pjsua_call_media_active) {/When the media is
active, connect Call to sound device.
Pjsua_conf_connect (ci.conf_slot, 0);
Pjsua_conf_connect (0, Ci.conf_slot);
}