pjsip logo pjsip.org
Open source SIP stack and media stack for presence, im/instant messaging, and multimedia communication

HOME

SIP/media Features
High Performance SIP
Small Footprint SIP
Symbian Port

FAQ

Documentation

Licensing

Download

Development (Trac)

Projects using pjsip

Mailing List

Open Source Links


About: PJLIB, PJLIB-UTIL, PJSIP, and PJMEDIA are created by: Benny Prijono
<bennylp@pjsip.org>


 

Home --> Documentations --> PJSIP Reference

Samples: Simple PJSUA

Very simple SIP User Agent with registration, call, and media, all in under 200 lines of code.

00001 /* $Id: simple_pjsua.c 2039 2008-06-20 22:44:47Z bennylp $ */
00002 /* 
00003  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00018  */
00019 
00040 #include <pjsua-lib/pjsua.h>
00041 
00042 #define THIS_FILE       "APP"
00043 
00044 #define SIP_DOMAIN      "example.com"
00045 #define SIP_USER        "alice"
00046 #define SIP_PASSWD      "secret"
00047 
00048 
00049 /* Callback called by the library upon receiving incoming call */
00050 static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
00051                              pjsip_rx_data *rdata)
00052 {
00053     pjsua_call_info ci;
00054 
00055     PJ_UNUSED_ARG(acc_id);
00056     PJ_UNUSED_ARG(rdata);
00057 
00058     pjsua_call_get_info(call_id, &ci);
00059 
00060     PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
00061                          (int)ci.remote_info.slen,
00062                          ci.remote_info.ptr));
00063 
00064     /* Automatically answer incoming calls with 200/OK */
00065     pjsua_call_answer(call_id, 200, NULL, NULL);
00066 }
00067 
00068 /* Callback called by the library when call's state has changed */
00069 static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
00070 {
00071     pjsua_call_info ci;
00072 
00073     PJ_UNUSED_ARG(e);
00074 
00075     pjsua_call_get_info(call_id, &ci);
00076     PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
00077                          (int)ci.state_text.slen,
00078                          ci.state_text.ptr));
00079 }
00080 
00081 /* Callback called by the library when call's media state has changed */
00082 static void on_call_media_state(pjsua_call_id call_id)
00083 {
00084     pjsua_call_info ci;
00085 
00086     pjsua_call_get_info(call_id, &ci);
00087 
00088     if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
00089         // When media is active, connect call to sound device.
00090         pjsua_conf_connect(ci.conf_slot, 0);
00091         pjsua_conf_connect(0, ci.conf_slot);
00092     }
00093 }
00094 
00095 /* Display error and exit application */
00096 static void error_exit(const char *title, pj_status_t status)
00097 {
00098     pjsua_perror(THIS_FILE, title, status);
00099     pjsua_destroy();
00100     exit(1);
00101 }
00102 
00103 /*
00104  * main()
00105  *
00106  * argv[1] may contain URL to call.
00107  */
00108 int main(int argc, char *argv[])
00109 {
00110     pjsua_acc_id acc_id;
00111     pj_status_t status;
00112 
00113     /* Create pjsua first! */
00114     status = pjsua_create();
00115     if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
00116 
00117     /* If argument is specified, it's got to be a valid SIP URL */
00118     if (argc > 1) {
00119         status = pjsua_verify_sip_url(argv[1]);
00120         if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);
00121     }
00122 
00123     /* Init pjsua */
00124     {
00125         pjsua_config cfg;
00126         pjsua_logging_config log_cfg;
00127 
00128         pjsua_config_default(&cfg);
00129         cfg.cb.on_incoming_call = &on_incoming_call;
00130         cfg.cb.on_call_media_state = &on_call_media_state;
00131         cfg.cb.on_call_state = &on_call_state;
00132 
00133         pjsua_logging_config_default(&log_cfg);
00134         log_cfg.console_level = 4;
00135 
00136         status = pjsua_init(&cfg, &log_cfg, NULL);
00137         if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
00138     }
00139 
00140     /* Add UDP transport. */
00141     {
00142         pjsua_transport_config cfg;
00143 
00144         pjsua_transport_config_default(&cfg);
00145         cfg.port = 5060;
00146         status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
00147         if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
00148     }
00149 
00150     /* Initialization is done, now start pjsua */
00151     status = pjsua_start();
00152     if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
00153 
00154     /* Register to SIP server by creating SIP account. */
00155     {
00156         pjsua_acc_config cfg;
00157 
00158         pjsua_acc_config_default(&cfg);
00159         cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
00160         cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
00161         cfg.cred_count = 1;
00162         cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);
00163         cfg.cred_info[0].scheme = pj_str("digest");
00164         cfg.cred_info[0].username = pj_str(SIP_USER);
00165         cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
00166         cfg.cred_info[0].data = pj_str(SIP_PASSWD);
00167 
00168         status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
00169         if (status != PJ_SUCCESS) error_exit("Error adding account", status);
00170     }
00171 
00172     /* If URL is specified, make call to the URL. */
00173     if (argc > 1) {
00174         pj_str_t uri = pj_str(argv[1]);
00175         status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);
00176         if (status != PJ_SUCCESS) error_exit("Error making call", status);
00177     }
00178 
00179     /* Wait until user press "q" to quit. */
00180     for (;;) {
00181         char option[10];
00182 
00183         puts("Press 'h' to hangup all calls, 'q' to quit");
00184         fgets(option, sizeof(option), stdin);
00185 
00186         if (option[0] == 'q')
00187             break;
00188 
00189         if (option[0] == 'h')
00190             pjsua_call_hangup_all();
00191     }
00192 
00193     /* Destroy pjsua */
00194     pjsua_destroy();
00195 
00196     return 0;
00197 }

 


PJSIP Open Source, high performance, small footprint, and very very portable SIP stack
(C)2003-2008 Benny Prijono