Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 19 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua_internal.h> |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 21 | #include "ua.h" |
| 22 | |
| 23 | #define THIS_FILE "symbian_ua.cpp" |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 24 | #define LOG_LEVEL 3 |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 25 | |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 26 | // |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 27 | // Basic config. |
| 28 | // |
| 29 | #define SIP_PORT 5060 |
| 30 | |
| 31 | |
| 32 | // |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 33 | // Destination URI (to make call, or to subscribe presence) |
| 34 | // |
Benny Prijono | 0e2c646 | 2008-02-14 13:39:24 +0000 | [diff] [blame^] | 35 | #define SIP_DST_URI "sip:100@pjsip.lab" |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 36 | |
| 37 | // |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 38 | // Account |
| 39 | // |
Benny Prijono | 0e2c646 | 2008-02-14 13:39:24 +0000 | [diff] [blame^] | 40 | #define HAS_SIP_ACCOUNT 1 // 0 to disable registration |
| 41 | #define SIP_DOMAIN "pjsip.lab" |
| 42 | #define SIP_USER "400" |
| 43 | #define SIP_PASSWD "400" |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 44 | |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 45 | // |
| 46 | // Outbound proxy for all accounts |
| 47 | // |
| 48 | #define SIP_PROXY NULL |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 49 | //#define SIP_PROXY "sip:192.168.0.8" |
| 50 | |
| 51 | |
| 52 | // |
| 53 | // Configure nameserver if DNS SRV is to be used with both SIP |
| 54 | // or STUN (for STUN see other settings below) |
| 55 | // |
Benny Prijono | 0e2c646 | 2008-02-14 13:39:24 +0000 | [diff] [blame^] | 56 | //#define NAMESERVER NULL |
| 57 | #define NAMESERVER "192.168.0.2" |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 58 | |
| 59 | // |
| 60 | // STUN server |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 61 | #if 0 |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 62 | // Use this to have the STUN server resolved normally |
| 63 | # define STUN_DOMAIN NULL |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 64 | # define STUN_SERVER "stun.xten.com" |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 65 | #elif 0 |
| 66 | // Use this to have the STUN server resolved with DNS SRV |
| 67 | # define STUN_DOMAIN "iptel.org" |
| 68 | # define STUN_SERVER NULL |
| 69 | #else |
| 70 | // Use this to disable STUN |
| 71 | # define STUN_DOMAIN NULL |
| 72 | # define STUN_SERVER NULL |
| 73 | #endif |
| 74 | |
| 75 | // |
| 76 | // Use ICE? |
| 77 | // |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 78 | #define USE_ICE 1 |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 79 | |
| 80 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 81 | // |
| 82 | // Globals |
| 83 | // |
| 84 | static pjsua_acc_id g_acc_id = PJSUA_INVALID_ID; |
| 85 | static pjsua_call_id g_call_id = PJSUA_INVALID_ID; |
| 86 | static pjsua_buddy_id g_buddy_id = PJSUA_INVALID_ID; |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 87 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 88 | |
| 89 | /* Callback called by the library upon receiving incoming call */ |
| 90 | static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, |
| 91 | pjsip_rx_data *rdata) |
| 92 | { |
| 93 | pjsua_call_info ci; |
| 94 | |
| 95 | PJ_UNUSED_ARG(acc_id); |
| 96 | PJ_UNUSED_ARG(rdata); |
| 97 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 98 | if (g_call_id != PJSUA_INVALID_ID) { |
| 99 | pjsua_call_answer(call_id, PJSIP_SC_BUSY_HERE, NULL, NULL); |
| 100 | return; |
| 101 | } |
| 102 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 103 | pjsua_call_get_info(call_id, &ci); |
| 104 | |
| 105 | PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!", |
| 106 | (int)ci.remote_info.slen, |
| 107 | ci.remote_info.ptr)); |
| 108 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 109 | g_call_id = call_id; |
| 110 | |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 111 | /* Automatically answer incoming calls with 180/Ringing */ |
| 112 | pjsua_call_answer(call_id, 180, NULL, NULL); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Callback called by the library when call's state has changed */ |
| 116 | static void on_call_state(pjsua_call_id call_id, pjsip_event *e) |
| 117 | { |
| 118 | pjsua_call_info ci; |
| 119 | |
| 120 | PJ_UNUSED_ARG(e); |
| 121 | |
| 122 | pjsua_call_get_info(call_id, &ci); |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 123 | |
| 124 | if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { |
| 125 | if (call_id == g_call_id) |
| 126 | g_call_id = PJSUA_INVALID_ID; |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 127 | } else if (ci.state != PJSIP_INV_STATE_INCOMING) { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 128 | if (g_call_id == PJSUA_INVALID_ID) |
| 129 | g_call_id = call_id; |
| 130 | } |
| 131 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 132 | PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id, |
| 133 | (int)ci.state_text.slen, |
| 134 | ci.state_text.ptr)); |
| 135 | } |
| 136 | |
| 137 | /* Callback called by the library when call's media state has changed */ |
| 138 | static void on_call_media_state(pjsua_call_id call_id) |
| 139 | { |
| 140 | pjsua_call_info ci; |
| 141 | |
| 142 | pjsua_call_get_info(call_id, &ci); |
| 143 | |
| 144 | if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) { |
| 145 | // When media is active, connect call to sound device. |
| 146 | pjsua_conf_connect(ci.conf_slot, 0); |
| 147 | pjsua_conf_connect(0, ci.conf_slot); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 152 | /* Handler on buddy state changed. */ |
| 153 | static void on_buddy_state(pjsua_buddy_id buddy_id) |
| 154 | { |
| 155 | pjsua_buddy_info info; |
| 156 | pjsua_buddy_get_info(buddy_id, &info); |
| 157 | |
| 158 | PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s", |
| 159 | (int)info.uri.slen, |
| 160 | info.uri.ptr, |
| 161 | (int)info.status_text.slen, |
| 162 | info.status_text.ptr)); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* Incoming IM message (i.e. MESSAGE request)! */ |
| 167 | static void on_pager(pjsua_call_id call_id, const pj_str_t *from, |
| 168 | const pj_str_t *to, const pj_str_t *contact, |
| 169 | const pj_str_t *mime_type, const pj_str_t *text) |
| 170 | { |
| 171 | /* Note: call index may be -1 */ |
| 172 | PJ_UNUSED_ARG(call_id); |
| 173 | PJ_UNUSED_ARG(to); |
| 174 | PJ_UNUSED_ARG(contact); |
| 175 | PJ_UNUSED_ARG(mime_type); |
| 176 | |
| 177 | PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s", |
| 178 | (int)from->slen, from->ptr, |
| 179 | (int)text->slen, text->ptr)); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* Received typing indication */ |
| 184 | static void on_typing(pjsua_call_id call_id, const pj_str_t *from, |
| 185 | const pj_str_t *to, const pj_str_t *contact, |
| 186 | pj_bool_t is_typing) |
| 187 | { |
| 188 | PJ_UNUSED_ARG(call_id); |
| 189 | PJ_UNUSED_ARG(to); |
| 190 | PJ_UNUSED_ARG(contact); |
| 191 | |
| 192 | PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s", |
| 193 | (int)from->slen, from->ptr, |
| 194 | (is_typing?"is typing..":"has stopped typing"))); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* Call transfer request status. */ |
| 199 | static void on_call_transfer_status(pjsua_call_id call_id, |
| 200 | int status_code, |
| 201 | const pj_str_t *status_text, |
| 202 | pj_bool_t final, |
| 203 | pj_bool_t *p_cont) |
| 204 | { |
| 205 | PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s", |
| 206 | call_id, status_code, |
| 207 | (int)status_text->slen, status_text->ptr, |
| 208 | (final ? "[final]" : ""))); |
| 209 | |
| 210 | if (status_code/100 == 2) { |
| 211 | PJ_LOG(3,(THIS_FILE, |
| 212 | "Call %d: call transfered successfully, disconnecting call", |
| 213 | call_id)); |
| 214 | pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL); |
| 215 | *p_cont = PJ_FALSE; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
Benny Prijono | aecabfc | 2007-10-26 05:35:42 +0000 | [diff] [blame] | 220 | /* NAT detection result */ |
| 221 | static void on_nat_detect(const pj_stun_nat_detect_result *res) |
| 222 | { |
| 223 | if (res->status != PJ_SUCCESS) { |
| 224 | pjsua_perror(THIS_FILE, "NAT detection failed", res->status); |
| 225 | } else { |
| 226 | PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name)); |
| 227 | } |
| 228 | } |
| 229 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 230 | /* Notification that call is being replaced. */ |
| 231 | static void on_call_replaced(pjsua_call_id old_call_id, |
| 232 | pjsua_call_id new_call_id) |
| 233 | { |
| 234 | pjsua_call_info old_ci, new_ci; |
| 235 | |
| 236 | pjsua_call_get_info(old_call_id, &old_ci); |
| 237 | pjsua_call_get_info(new_call_id, &new_ci); |
| 238 | |
| 239 | PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by " |
| 240 | "call %d with %.*s", |
| 241 | old_call_id, |
| 242 | (int)old_ci.remote_info.slen, old_ci.remote_info.ptr, |
| 243 | new_call_id, |
| 244 | (int)new_ci.remote_info.slen, new_ci.remote_info.ptr)); |
| 245 | } |
| 246 | |
| 247 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 248 | /* Logging callback */ |
Benny Prijono | 797e338 | 2007-12-01 09:10:07 +0000 | [diff] [blame] | 249 | static void log_writer(int level, const char *buf, int len) |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 250 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 251 | static wchar_t buf16[PJ_LOG_MAX_SIZE]; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 252 | |
| 253 | PJ_UNUSED_ARG(level); |
| 254 | |
| 255 | pj_ansi_to_unicode(buf, len, buf16, PJ_ARRAY_SIZE(buf16)); |
| 256 | |
| 257 | TPtrC16 aBuf((const TUint16*)buf16, (TInt)len); |
| 258 | console->Write(aBuf); |
Benny Prijono | 797e338 | 2007-12-01 09:10:07 +0000 | [diff] [blame] | 259 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * app_startup() |
| 264 | * |
| 265 | * url may contain URL to call. |
| 266 | */ |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 267 | static pj_status_t app_startup() |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 268 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 269 | pj_status_t status; |
| 270 | |
| 271 | /* Redirect log before pjsua_init() */ |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 272 | pj_log_set_log_func(&log_writer); |
| 273 | |
| 274 | /* Set log level */ |
| 275 | pj_log_set_level(LOG_LEVEL); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 276 | |
| 277 | /* Create pjsua first! */ |
| 278 | status = pjsua_create(); |
| 279 | if (status != PJ_SUCCESS) { |
| 280 | pjsua_perror(THIS_FILE, "pjsua_create() error", status); |
| 281 | return status; |
| 282 | } |
| 283 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 284 | /* Init pjsua */ |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 285 | pjsua_config cfg; |
| 286 | pjsua_logging_config log_cfg; |
| 287 | pjsua_media_config med_cfg; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 288 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 289 | pjsua_config_default(&cfg); |
| 290 | cfg.max_calls = 2; |
| 291 | cfg.thread_cnt = 0; // Disable threading on Symbian |
| 292 | cfg.cb.on_incoming_call = &on_incoming_call; |
| 293 | cfg.cb.on_call_media_state = &on_call_media_state; |
| 294 | cfg.cb.on_call_state = &on_call_state; |
| 295 | cfg.cb.on_buddy_state = &on_buddy_state; |
| 296 | cfg.cb.on_pager = &on_pager; |
| 297 | cfg.cb.on_typing = &on_typing; |
| 298 | cfg.cb.on_call_transfer_status = &on_call_transfer_status; |
| 299 | cfg.cb.on_call_replaced = &on_call_replaced; |
Benny Prijono | aecabfc | 2007-10-26 05:35:42 +0000 | [diff] [blame] | 300 | cfg.cb.on_nat_detect = &on_nat_detect; |
| 301 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 302 | if (SIP_PROXY) { |
| 303 | cfg.outbound_proxy_cnt = 1; |
| 304 | cfg.outbound_proxy[0] = pj_str(SIP_PROXY); |
| 305 | } |
| 306 | |
| 307 | if (NAMESERVER) { |
| 308 | cfg.nameserver_count = 1; |
| 309 | cfg.nameserver[0] = pj_str(NAMESERVER); |
| 310 | } |
| 311 | |
| 312 | if (NAMESERVER && STUN_DOMAIN) { |
| 313 | cfg.stun_domain = pj_str(STUN_DOMAIN); |
| 314 | } else if (STUN_SERVER) { |
| 315 | cfg.stun_host = pj_str(STUN_SERVER); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | pjsua_logging_config_default(&log_cfg); |
| 320 | log_cfg.console_level = 4; |
| 321 | log_cfg.cb = &log_writer; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 322 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 323 | pjsua_media_config_default(&med_cfg); |
| 324 | med_cfg.thread_cnt = 0; // Disable threading on Symbian |
| 325 | med_cfg.has_ioqueue = PJ_FALSE; |
| 326 | med_cfg.clock_rate = 8000; |
Benny Prijono | 5df8bb6 | 2007-12-28 18:55:02 +0000 | [diff] [blame] | 327 | med_cfg.audio_frame_ptime = 40; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 328 | med_cfg.ec_tail_len = 0; |
| 329 | med_cfg.enable_ice = USE_ICE; |
| 330 | |
| 331 | status = pjsua_init(&cfg, &log_cfg, &med_cfg); |
| 332 | if (status != PJ_SUCCESS) { |
| 333 | pjsua_perror(THIS_FILE, "pjsua_init() error", status); |
| 334 | pjsua_destroy(); |
| 335 | return status; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* Add UDP transport. */ |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 339 | pjsua_transport_config tcfg; |
| 340 | pjsua_transport_id tid; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 341 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 342 | pjsua_transport_config_default(&tcfg); |
| 343 | tcfg.port = SIP_PORT; |
| 344 | status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tcfg, &tid); |
| 345 | if (status != PJ_SUCCESS) { |
| 346 | pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 347 | pjsua_destroy(); |
| 348 | return status; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 351 | /* Add account for the transport */ |
| 352 | pjsua_acc_add_local(tid, PJ_TRUE, &g_acc_id); |
| 353 | |
| 354 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 355 | /* Initialization is done, now start pjsua */ |
| 356 | status = pjsua_start(); |
| 357 | if (status != PJ_SUCCESS) { |
| 358 | pjsua_perror(THIS_FILE, "Error starting pjsua", status); |
| 359 | pjsua_destroy(); |
| 360 | return status; |
| 361 | } |
| 362 | |
| 363 | /* Register to SIP server by creating SIP account. */ |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 364 | if (HAS_SIP_ACCOUNT) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 365 | pjsua_acc_config cfg; |
| 366 | |
| 367 | pjsua_acc_config_default(&cfg); |
| 368 | cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN); |
| 369 | cfg.reg_uri = pj_str("sip:" SIP_DOMAIN); |
| 370 | cfg.cred_count = 1; |
Benny Prijono | 0e2c646 | 2008-02-14 13:39:24 +0000 | [diff] [blame^] | 371 | cfg.cred_info[0].realm = pj_str("*"); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 372 | cfg.cred_info[0].scheme = pj_str("digest"); |
| 373 | cfg.cred_info[0].username = pj_str(SIP_USER); |
| 374 | cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 375 | cfg.cred_info[0].data = pj_str(SIP_PASSWD); |
| 376 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 377 | status = pjsua_acc_add(&cfg, PJ_TRUE, &g_acc_id); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 378 | if (status != PJ_SUCCESS) { |
| 379 | pjsua_perror(THIS_FILE, "Error adding account", status); |
| 380 | pjsua_destroy(); |
| 381 | return status; |
| 382 | } |
| 383 | } |
| 384 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 385 | if (SIP_DST_URI) { |
| 386 | pjsua_buddy_config bcfg; |
| 387 | |
| 388 | pjsua_buddy_config_default(&bcfg); |
| 389 | bcfg.uri = pj_str(SIP_DST_URI); |
| 390 | bcfg.subscribe = PJ_FALSE; |
| 391 | |
| 392 | pjsua_buddy_add(&bcfg, &g_buddy_id); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 393 | } |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 394 | return PJ_SUCCESS; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | //////////////////////////////////////////////////////////////////////////// |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 399 | /* |
| 400 | * The interractive console UI |
| 401 | */ |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 402 | #include <e32base.h> |
| 403 | |
| 404 | class ConsoleUI : public CActive |
| 405 | { |
| 406 | public: |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 407 | ConsoleUI(CActiveSchedulerWait *asw, CConsoleBase *con); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 408 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 409 | // Run console UI |
| 410 | void Run(); |
| 411 | |
| 412 | // Stop |
| 413 | void Stop(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 414 | |
| 415 | protected: |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 416 | // Cancel asynchronous read. |
| 417 | void DoCancel(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 418 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 419 | // Implementation: called when read has completed. |
| 420 | void RunL(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 421 | |
| 422 | private: |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 423 | CActiveSchedulerWait *asw_; |
| 424 | CConsoleBase *con_; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | |
| 428 | ConsoleUI::ConsoleUI(CActiveSchedulerWait *asw, CConsoleBase *con) |
| 429 | : CActive(EPriorityStandard), asw_(asw), con_(con) |
| 430 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 431 | CActiveScheduler::Add(this); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Run console UI |
| 435 | void ConsoleUI::Run() |
| 436 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 437 | con_->Read(iStatus); |
| 438 | SetActive(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // Stop console UI |
| 442 | void ConsoleUI::Stop() |
| 443 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 444 | DoCancel(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Cancel asynchronous read. |
| 448 | void ConsoleUI::DoCancel() |
| 449 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 450 | con_->ReadCancel(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 453 | static void PrintMenu() |
| 454 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 455 | PJ_LOG(3, (THIS_FILE, "\n\n" |
| 456 | "Menu:\n" |
| 457 | " d Dump states\n" |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 458 | " D Dump states detail\n" |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 459 | " P Dump pool factory\n" |
Benny Prijono | 5df8bb6 | 2007-12-28 18:55:02 +0000 | [diff] [blame] | 460 | " l Start loopback audio device\n" |
| 461 | " L Stop loopback audio device\n" |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 462 | " m Call " SIP_DST_URI "\n" |
| 463 | " a Answer call\n" |
| 464 | " g Hangup all calls\n" |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 465 | " s Subscribe " SIP_DST_URI "\n" |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 466 | " S Unsubscribe presence\n" |
| 467 | " o Set account online\n" |
| 468 | " O Set account offline\n" |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 469 | " w Quit\n")); |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 472 | // Implementation: called when read has completed. |
| 473 | void ConsoleUI::RunL() |
| 474 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 475 | TKeyCode kc = con_->KeyCode(); |
| 476 | pj_bool_t reschedule = PJ_TRUE; |
| 477 | |
| 478 | switch (kc) { |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 479 | case 'w': |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 480 | asw_->AsyncStop(); |
| 481 | reschedule = PJ_FALSE; |
| 482 | break; |
| 483 | case 'D': |
| 484 | case 'd': |
| 485 | pjsua_dump(kc == 'D'); |
| 486 | break; |
| 487 | case 'p': |
| 488 | case 'P': |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 489 | pj_pool_factory_dump(pjsua_get_pool_factory(), PJ_TRUE); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 490 | break; |
Benny Prijono | 5df8bb6 | 2007-12-28 18:55:02 +0000 | [diff] [blame] | 491 | case 'l': |
| 492 | pjsua_conf_connect(0, 0); |
| 493 | break; |
| 494 | case 'L': |
| 495 | pjsua_conf_disconnect(0, 0); |
| 496 | break; |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 497 | case 'm': |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 498 | if (g_call_id != PJSUA_INVALID_ID) { |
| 499 | PJ_LOG(3,(THIS_FILE, "Another call is active")); |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | if (pjsua_verify_sip_url(SIP_DST_URI) == PJ_SUCCESS) { |
| 504 | pj_str_t dst = pj_str(SIP_DST_URI); |
| 505 | pjsua_call_make_call(g_acc_id, &dst, 0, NULL, |
| 506 | NULL, &g_call_id); |
| 507 | } else { |
| 508 | PJ_LOG(3,(THIS_FILE, "Invalid SIP URI")); |
| 509 | } |
| 510 | break; |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 511 | case 'a': |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 512 | if (g_call_id != PJSUA_INVALID_ID) |
| 513 | pjsua_call_answer(g_call_id, 200, NULL, NULL); |
| 514 | break; |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 515 | case 'g': |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 516 | pjsua_call_hangup_all(); |
| 517 | break; |
| 518 | case 's': |
| 519 | case 'S': |
| 520 | if (g_buddy_id != PJSUA_INVALID_ID) |
| 521 | pjsua_buddy_subscribe_pres(g_buddy_id, kc=='s'); |
| 522 | break; |
| 523 | case 'o': |
| 524 | case 'O': |
| 525 | pjsua_acc_set_online_status(g_acc_id, kc=='o'); |
| 526 | break; |
| 527 | default: |
| 528 | PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed", |
| 529 | kc, kc)); |
| 530 | break; |
| 531 | } |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 532 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 533 | PrintMenu(); |
| 534 | |
| 535 | if (reschedule) |
| 536 | Run(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 539 | #if 0 |
| 540 | // IP networking related testing |
| 541 | static pj_status_t test_addr(void) |
| 542 | { |
| 543 | int af; |
| 544 | unsigned i, count; |
| 545 | pj_addrinfo ai[8]; |
| 546 | pj_sockaddr ifs[8]; |
| 547 | const pj_str_t *hostname; |
| 548 | pj_hostent he; |
| 549 | pj_status_t status; |
| 550 | |
| 551 | pj_log_set_log_func(&log_writer); |
| 552 | |
| 553 | status = pj_init(); |
| 554 | if (status != PJ_SUCCESS) { |
| 555 | pjsua_perror(THIS_FILE, "pj_init() error", status); |
| 556 | return status; |
| 557 | } |
| 558 | |
| 559 | af = pj_AF_INET(); |
| 560 | |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 561 | #if 0 |
| 562 | pj_in_addr in_addr; |
| 563 | pj_str_t aa = pj_str("1.1.1.1"); |
| 564 | in_addr = pj_inet_addr(&aa); |
| 565 | char *the_addr = pj_inet_ntoa(in_addr); |
| 566 | PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr)); |
| 567 | |
| 568 | aa = pj_str("192.168.0.15"); |
| 569 | in_addr = pj_inet_addr(&aa); |
| 570 | the_addr = pj_inet_ntoa(in_addr); |
| 571 | PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr)); |
| 572 | |
| 573 | aa = pj_str("2.2.2.2"); |
| 574 | in_addr = pj_inet_addr(&aa); |
| 575 | the_addr = pj_inet_ntoa(in_addr); |
| 576 | PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr)); |
| 577 | |
| 578 | return -1; |
| 579 | #endif |
| 580 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 581 | // Hostname |
| 582 | hostname = pj_gethostname(); |
| 583 | if (hostname == NULL) { |
| 584 | status = PJ_ERESOLVE; |
| 585 | pjsua_perror(THIS_FILE, "pj_gethostname() error", status); |
| 586 | goto on_return; |
| 587 | } |
| 588 | |
| 589 | PJ_LOG(3,(THIS_FILE, "Hostname: %.*s", hostname->slen, hostname->ptr)); |
| 590 | |
| 591 | // Gethostbyname |
| 592 | status = pj_gethostbyname(hostname, &he); |
| 593 | if (status != PJ_SUCCESS) { |
| 594 | pjsua_perror(THIS_FILE, "pj_gethostbyname() error", status); |
| 595 | } else { |
| 596 | PJ_LOG(3,(THIS_FILE, "gethostbyname: %s", |
| 597 | pj_inet_ntoa(*(pj_in_addr*)he.h_addr))); |
| 598 | } |
| 599 | |
| 600 | // Getaddrinfo |
| 601 | count = PJ_ARRAY_SIZE(ai); |
| 602 | status = pj_getaddrinfo(af, hostname, &count, ai); |
| 603 | if (status != PJ_SUCCESS) { |
| 604 | pjsua_perror(THIS_FILE, "pj_getaddrinfo() error", status); |
| 605 | } else { |
| 606 | for (i=0; i<count; ++i) { |
| 607 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 608 | PJ_LOG(3,(THIS_FILE, "Addrinfo: %s", |
| 609 | pj_sockaddr_print(&ai[i].ai_addr, ipaddr, sizeof(ipaddr), 2))); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | // Enum interface |
| 614 | count = PJ_ARRAY_SIZE(ifs); |
| 615 | status = pj_enum_ip_interface(af, &count, ifs); |
| 616 | if (status != PJ_SUCCESS) { |
| 617 | pjsua_perror(THIS_FILE, "pj_enum_ip_interface() error", status); |
| 618 | } else { |
| 619 | for (i=0; i<count; ++i) { |
| 620 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 621 | PJ_LOG(3,(THIS_FILE, "Interface: %s", |
| 622 | pj_sockaddr_print(&ifs[i], ipaddr, sizeof(ipaddr), 2))); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // Get default iinterface |
| 627 | status = pj_getdefaultipinterface(af, &ifs[0]); |
| 628 | if (status != PJ_SUCCESS) { |
| 629 | pjsua_perror(THIS_FILE, "pj_getdefaultipinterface() error", status); |
| 630 | } else { |
| 631 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 632 | PJ_LOG(3,(THIS_FILE, "Default IP: %s", |
| 633 | pj_sockaddr_print(&ifs[0], ipaddr, sizeof(ipaddr), 2))); |
| 634 | } |
| 635 | |
| 636 | // Get default IP address |
| 637 | status = pj_gethostip(af, &ifs[0]); |
| 638 | if (status != PJ_SUCCESS) { |
| 639 | pjsua_perror(THIS_FILE, "pj_gethostip() error", status); |
| 640 | } else { |
| 641 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 642 | PJ_LOG(3,(THIS_FILE, "Host IP: %s", |
| 643 | pj_sockaddr_print(&ifs[0], ipaddr, sizeof(ipaddr), 2))); |
| 644 | } |
| 645 | |
| 646 | status = -1; |
| 647 | |
| 648 | on_return: |
| 649 | pj_shutdown(); |
| 650 | return status; |
| 651 | } |
| 652 | #endif |
| 653 | |
| 654 | |
Benny Prijono | 6b3ccdf | 2008-01-14 11:54:21 +0000 | [diff] [blame] | 655 | #include <es_sock.h> |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 656 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 657 | //////////////////////////////////////////////////////////////////////////// |
| 658 | int ua_main() |
| 659 | { |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 660 | RSocketServ aSocketServer; |
| 661 | RConnection aConn; |
| 662 | TInt err; |
| 663 | pj_symbianos_params sym_params; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 664 | pj_status_t status; |
| 665 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 666 | // Initialize RSocketServ |
| 667 | if ((err=aSocketServer.Connect()) != KErrNone) |
| 668 | return PJ_STATUS_FROM_OS(err); |
| 669 | |
| 670 | // Open up a connection |
| 671 | if ((err=aConn.Open(aSocketServer)) != KErrNone) { |
| 672 | aSocketServer.Close(); |
| 673 | return PJ_STATUS_FROM_OS(err); |
| 674 | } |
| 675 | |
| 676 | if ((err=aConn.Start()) != KErrNone) { |
| 677 | aSocketServer.Close(); |
| 678 | return PJ_STATUS_FROM_OS(err); |
| 679 | } |
| 680 | |
| 681 | // Set Symbian OS parameters in pjlib. |
| 682 | // This must be done before pj_init() is called. |
| 683 | pj_bzero(&sym_params, sizeof(sym_params)); |
| 684 | sym_params.rsocketserv = &aSocketServer; |
| 685 | sym_params.rconnection = &aConn; |
| 686 | pj_symbianos_set_params(&sym_params); |
| 687 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 688 | // Initialize pjsua |
| 689 | status = app_startup(); |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 690 | //status = test_addr(); |
| 691 | if (status != PJ_SUCCESS) { |
| 692 | aConn.Close(); |
| 693 | aSocketServer.Close(); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 694 | return status; |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 695 | } |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 696 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 697 | // Run the UI |
| 698 | CActiveSchedulerWait *asw = new CActiveSchedulerWait; |
| 699 | ConsoleUI *con = new ConsoleUI(asw, console); |
| 700 | |
| 701 | con->Run(); |
| 702 | |
| 703 | PrintMenu(); |
| 704 | asw->Start(); |
| 705 | |
| 706 | delete con; |
| 707 | delete asw; |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 708 | |
| 709 | // Dump memory statistics |
| 710 | PJ_LOG(3,(THIS_FILE, "Max heap usage: %u.%03uMB", |
| 711 | pjsua_var.cp.peak_used_size / 1000000, |
| 712 | (pjsua_var.cp.peak_used_size % 1000000)/1000)); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 713 | |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 714 | // check max stack usage |
| 715 | #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0 |
| 716 | pj_thread_t* this_thread = pj_thread_this(); |
| 717 | if (!this_thread) |
| 718 | return status; |
| 719 | |
| 720 | const char* max_stack_file; |
| 721 | int max_stack_line; |
| 722 | status = pj_thread_get_stack_info(this_thread, &max_stack_file, &max_stack_line); |
| 723 | |
| 724 | PJ_LOG(3,(THIS_FILE, "Max stack usage: %u at %s:%d", |
| 725 | pj_thread_get_stack_max_usage(this_thread), |
| 726 | max_stack_file, max_stack_line)); |
| 727 | #endif |
| 728 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 729 | // Shutdown pjsua |
| 730 | pjsua_destroy(); |
| 731 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 732 | // Close connection and socket server |
| 733 | aConn.Close(); |
| 734 | aSocketServer.Close(); |
| 735 | |
Benny Prijono | 797e338 | 2007-12-01 09:10:07 +0000 | [diff] [blame] | 736 | return status; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 737 | } |
| 738 | |