|
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
Very simple SIP User Agent with registration, call, and media, all in under 200 lines of code.
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00065 pjsua_call_answer(call_id, 200, NULL, NULL);
00066 }
00067
00068
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
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
00090 pjsua_conf_connect(ci.conf_slot, 0);
00091 pjsua_conf_connect(0, ci.conf_slot);
00092 }
00093 }
00094
00095
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
00105
00106
00107
00108 int main(int argc, char *argv[])
00109 {
00110 pjsua_acc_id acc_id;
00111 pj_status_t status;
00112
00113
00114 status = pjsua_create();
00115 if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
00116
00117
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
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
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
00151 status = pjsua_start();
00152 if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
00153
00154
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
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
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
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
|
|