Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 21 | #include <pjsua-lib/pjsua_internal.h> |
Nanang Izzuddin | a940b36 | 2009-02-23 13:53:30 +0000 | [diff] [blame] | 22 | //#include <pjmedia/symbian_sound_aps.h> |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 23 | #include "ua.h" |
| 24 | |
| 25 | #define THIS_FILE "symbian_ua.cpp" |
Nanang Izzuddin | da37ea3 | 2009-03-06 16:24:43 +0000 | [diff] [blame] | 26 | #define CON_LOG_LEVEL 3 // console log level |
| 27 | #define FILE_LOG_LEVEL 4 // logfile log level |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 28 | |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 29 | // |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 30 | // Basic config. |
| 31 | // |
| 32 | #define SIP_PORT 5060 |
| 33 | |
| 34 | |
| 35 | // |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 36 | // Destination URI (to make call, or to subscribe presence) |
| 37 | // |
Benny Prijono | 0e2c646 | 2008-02-14 13:39:24 +0000 | [diff] [blame] | 38 | #define SIP_DST_URI "sip:100@pjsip.lab" |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 39 | |
| 40 | // |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 41 | // Account |
| 42 | // |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 43 | #define HAS_SIP_ACCOUNT 0 // 1 to enable registration |
Benny Prijono | 0e2c646 | 2008-02-14 13:39:24 +0000 | [diff] [blame] | 44 | #define SIP_DOMAIN "pjsip.lab" |
| 45 | #define SIP_USER "400" |
| 46 | #define SIP_PASSWD "400" |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 47 | |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 48 | // |
| 49 | // Outbound proxy for all accounts |
| 50 | // |
| 51 | #define SIP_PROXY NULL |
Benny Prijono | 4b55e0a | 2008-12-18 13:22:52 +0000 | [diff] [blame] | 52 | //#define SIP_PROXY "<sip:192.168.0.8;lr>" |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 53 | |
Nanang Izzuddin | 940d3f6 | 2009-06-22 14:06:40 +0000 | [diff] [blame] | 54 | // |
| 55 | // Set to 1 if TCP is desired (experimental) |
| 56 | // |
| 57 | #define ENABLE_SIP_TCP 0 |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 58 | |
| 59 | // |
| 60 | // Configure nameserver if DNS SRV is to be used with both SIP |
| 61 | // or STUN (for STUN see other settings below) |
| 62 | // |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 63 | #define NAMESERVER NULL |
| 64 | //#define NAMESERVER "192.168.0.2" |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 65 | |
| 66 | // |
| 67 | // STUN server |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 68 | #if 0 |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 69 | // Use this to have the STUN server resolved normally |
| 70 | # define STUN_DOMAIN NULL |
Benny Prijono | 4b55e0a | 2008-12-18 13:22:52 +0000 | [diff] [blame] | 71 | # define STUN_SERVER "stun.pjsip.org" |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 72 | #elif 0 |
| 73 | // Use this to have the STUN server resolved with DNS SRV |
Benny Prijono | 4b55e0a | 2008-12-18 13:22:52 +0000 | [diff] [blame] | 74 | # define STUN_DOMAIN "pjsip.org" |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 75 | # define STUN_SERVER NULL |
| 76 | #else |
| 77 | // Use this to disable STUN |
| 78 | # define STUN_DOMAIN NULL |
| 79 | # define STUN_SERVER NULL |
| 80 | #endif |
| 81 | |
| 82 | // |
| 83 | // Use ICE? |
| 84 | // |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 85 | #define USE_ICE 1 |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 86 | |
Nanang Izzuddin | 6a6392f | 2008-06-02 18:30:15 +0000 | [diff] [blame] | 87 | // |
| 88 | // Use SRTP? |
| 89 | // |
| 90 | #define USE_SRTP PJSUA_DEFAULT_USE_SRTP |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 91 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 92 | // |
| 93 | // Globals |
| 94 | // |
| 95 | static pjsua_acc_id g_acc_id = PJSUA_INVALID_ID; |
| 96 | static pjsua_call_id g_call_id = PJSUA_INVALID_ID; |
| 97 | static pjsua_buddy_id g_buddy_id = PJSUA_INVALID_ID; |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 98 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 99 | |
| 100 | /* Callback called by the library upon receiving incoming call */ |
| 101 | static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, |
| 102 | pjsip_rx_data *rdata) |
| 103 | { |
| 104 | pjsua_call_info ci; |
| 105 | |
| 106 | PJ_UNUSED_ARG(acc_id); |
| 107 | PJ_UNUSED_ARG(rdata); |
| 108 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 109 | if (g_call_id != PJSUA_INVALID_ID) { |
| 110 | pjsua_call_answer(call_id, PJSIP_SC_BUSY_HERE, NULL, NULL); |
| 111 | return; |
| 112 | } |
| 113 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 114 | pjsua_call_get_info(call_id, &ci); |
| 115 | |
| 116 | PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!", |
| 117 | (int)ci.remote_info.slen, |
| 118 | ci.remote_info.ptr)); |
| 119 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 120 | g_call_id = call_id; |
| 121 | |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 122 | /* Automatically answer incoming calls with 180/Ringing */ |
| 123 | pjsua_call_answer(call_id, 180, NULL, NULL); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* Callback called by the library when call's state has changed */ |
| 127 | static void on_call_state(pjsua_call_id call_id, pjsip_event *e) |
| 128 | { |
| 129 | pjsua_call_info ci; |
| 130 | |
| 131 | PJ_UNUSED_ARG(e); |
| 132 | |
| 133 | pjsua_call_get_info(call_id, &ci); |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 134 | |
| 135 | if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { |
| 136 | if (call_id == g_call_id) |
| 137 | g_call_id = PJSUA_INVALID_ID; |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 138 | } else if (ci.state != PJSIP_INV_STATE_INCOMING) { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 139 | if (g_call_id == PJSUA_INVALID_ID) |
| 140 | g_call_id = call_id; |
| 141 | } |
| 142 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 143 | PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id, |
| 144 | (int)ci.state_text.slen, |
| 145 | ci.state_text.ptr)); |
| 146 | } |
| 147 | |
| 148 | /* Callback called by the library when call's media state has changed */ |
| 149 | static void on_call_media_state(pjsua_call_id call_id) |
| 150 | { |
| 151 | pjsua_call_info ci; |
| 152 | |
| 153 | pjsua_call_get_info(call_id, &ci); |
| 154 | |
| 155 | if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) { |
| 156 | // When media is active, connect call to sound device. |
| 157 | pjsua_conf_connect(ci.conf_slot, 0); |
| 158 | pjsua_conf_connect(0, ci.conf_slot); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 163 | /* Handler on buddy state changed. */ |
| 164 | static void on_buddy_state(pjsua_buddy_id buddy_id) |
| 165 | { |
| 166 | pjsua_buddy_info info; |
| 167 | pjsua_buddy_get_info(buddy_id, &info); |
| 168 | |
| 169 | PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s", |
| 170 | (int)info.uri.slen, |
| 171 | info.uri.ptr, |
| 172 | (int)info.status_text.slen, |
| 173 | info.status_text.ptr)); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* Incoming IM message (i.e. MESSAGE request)! */ |
| 178 | static void on_pager(pjsua_call_id call_id, const pj_str_t *from, |
| 179 | const pj_str_t *to, const pj_str_t *contact, |
| 180 | const pj_str_t *mime_type, const pj_str_t *text) |
| 181 | { |
| 182 | /* Note: call index may be -1 */ |
| 183 | PJ_UNUSED_ARG(call_id); |
| 184 | PJ_UNUSED_ARG(to); |
| 185 | PJ_UNUSED_ARG(contact); |
| 186 | PJ_UNUSED_ARG(mime_type); |
| 187 | |
| 188 | PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s", |
| 189 | (int)from->slen, from->ptr, |
| 190 | (int)text->slen, text->ptr)); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /* Received typing indication */ |
| 195 | static void on_typing(pjsua_call_id call_id, const pj_str_t *from, |
| 196 | const pj_str_t *to, const pj_str_t *contact, |
| 197 | pj_bool_t is_typing) |
| 198 | { |
| 199 | PJ_UNUSED_ARG(call_id); |
| 200 | PJ_UNUSED_ARG(to); |
| 201 | PJ_UNUSED_ARG(contact); |
| 202 | |
| 203 | PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s", |
| 204 | (int)from->slen, from->ptr, |
| 205 | (is_typing?"is typing..":"has stopped typing"))); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /* Call transfer request status. */ |
| 210 | static void on_call_transfer_status(pjsua_call_id call_id, |
| 211 | int status_code, |
| 212 | const pj_str_t *status_text, |
| 213 | pj_bool_t final, |
| 214 | pj_bool_t *p_cont) |
| 215 | { |
| 216 | PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s", |
| 217 | call_id, status_code, |
| 218 | (int)status_text->slen, status_text->ptr, |
| 219 | (final ? "[final]" : ""))); |
| 220 | |
| 221 | if (status_code/100 == 2) { |
| 222 | PJ_LOG(3,(THIS_FILE, |
| 223 | "Call %d: call transfered successfully, disconnecting call", |
| 224 | call_id)); |
| 225 | pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL); |
| 226 | *p_cont = PJ_FALSE; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | |
Benny Prijono | aecabfc | 2007-10-26 05:35:42 +0000 | [diff] [blame] | 231 | /* NAT detection result */ |
| 232 | static void on_nat_detect(const pj_stun_nat_detect_result *res) |
| 233 | { |
| 234 | if (res->status != PJ_SUCCESS) { |
| 235 | pjsua_perror(THIS_FILE, "NAT detection failed", res->status); |
| 236 | } else { |
| 237 | PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name)); |
| 238 | } |
| 239 | } |
| 240 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 241 | /* Notification that call is being replaced. */ |
| 242 | static void on_call_replaced(pjsua_call_id old_call_id, |
| 243 | pjsua_call_id new_call_id) |
| 244 | { |
| 245 | pjsua_call_info old_ci, new_ci; |
| 246 | |
| 247 | pjsua_call_get_info(old_call_id, &old_ci); |
| 248 | pjsua_call_get_info(new_call_id, &new_ci); |
| 249 | |
| 250 | PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by " |
| 251 | "call %d with %.*s", |
| 252 | old_call_id, |
| 253 | (int)old_ci.remote_info.slen, old_ci.remote_info.ptr, |
| 254 | new_call_id, |
| 255 | (int)new_ci.remote_info.slen, new_ci.remote_info.ptr)); |
| 256 | } |
| 257 | |
| 258 | |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 259 | //#include<e32debug.h> |
| 260 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 261 | /* Logging callback */ |
Benny Prijono | 797e338 | 2007-12-01 09:10:07 +0000 | [diff] [blame] | 262 | static void log_writer(int level, const char *buf, int len) |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 263 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 264 | static wchar_t buf16[PJ_LOG_MAX_SIZE]; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 265 | |
| 266 | PJ_UNUSED_ARG(level); |
| 267 | |
| 268 | pj_ansi_to_unicode(buf, len, buf16, PJ_ARRAY_SIZE(buf16)); |
| 269 | |
| 270 | TPtrC16 aBuf((const TUint16*)buf16, (TInt)len); |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 271 | //RDebug::Print(aBuf); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 272 | console->Write(aBuf); |
Benny Prijono | 797e338 | 2007-12-01 09:10:07 +0000 | [diff] [blame] | 273 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * app_startup() |
| 278 | * |
| 279 | * url may contain URL to call. |
| 280 | */ |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 281 | static pj_status_t app_startup() |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 282 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 283 | pj_status_t status; |
| 284 | |
| 285 | /* Redirect log before pjsua_init() */ |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 286 | pj_log_set_log_func(&log_writer); |
| 287 | |
| 288 | /* Set log level */ |
Nanang Izzuddin | da37ea3 | 2009-03-06 16:24:43 +0000 | [diff] [blame] | 289 | pj_log_set_level(CON_LOG_LEVEL); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 290 | |
| 291 | /* Create pjsua first! */ |
| 292 | status = pjsua_create(); |
| 293 | if (status != PJ_SUCCESS) { |
| 294 | pjsua_perror(THIS_FILE, "pjsua_create() error", status); |
| 295 | return status; |
| 296 | } |
| 297 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 298 | /* Init pjsua */ |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 299 | pjsua_config cfg; |
| 300 | pjsua_logging_config log_cfg; |
| 301 | pjsua_media_config med_cfg; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 302 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 303 | pjsua_config_default(&cfg); |
| 304 | cfg.max_calls = 2; |
| 305 | cfg.thread_cnt = 0; // Disable threading on Symbian |
Nanang Izzuddin | 6a6392f | 2008-06-02 18:30:15 +0000 | [diff] [blame] | 306 | cfg.use_srtp = USE_SRTP; |
| 307 | cfg.srtp_secure_signaling = 0; |
| 308 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 309 | cfg.cb.on_incoming_call = &on_incoming_call; |
| 310 | cfg.cb.on_call_media_state = &on_call_media_state; |
| 311 | cfg.cb.on_call_state = &on_call_state; |
| 312 | cfg.cb.on_buddy_state = &on_buddy_state; |
| 313 | cfg.cb.on_pager = &on_pager; |
| 314 | cfg.cb.on_typing = &on_typing; |
| 315 | cfg.cb.on_call_transfer_status = &on_call_transfer_status; |
| 316 | cfg.cb.on_call_replaced = &on_call_replaced; |
Benny Prijono | aecabfc | 2007-10-26 05:35:42 +0000 | [diff] [blame] | 317 | cfg.cb.on_nat_detect = &on_nat_detect; |
| 318 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 319 | if (SIP_PROXY) { |
| 320 | cfg.outbound_proxy_cnt = 1; |
| 321 | cfg.outbound_proxy[0] = pj_str(SIP_PROXY); |
| 322 | } |
| 323 | |
| 324 | if (NAMESERVER) { |
| 325 | cfg.nameserver_count = 1; |
| 326 | cfg.nameserver[0] = pj_str(NAMESERVER); |
| 327 | } |
| 328 | |
| 329 | if (NAMESERVER && STUN_DOMAIN) { |
| 330 | cfg.stun_domain = pj_str(STUN_DOMAIN); |
| 331 | } else if (STUN_SERVER) { |
| 332 | cfg.stun_host = pj_str(STUN_SERVER); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | pjsua_logging_config_default(&log_cfg); |
Nanang Izzuddin | da37ea3 | 2009-03-06 16:24:43 +0000 | [diff] [blame] | 337 | log_cfg.level = FILE_LOG_LEVEL; |
| 338 | log_cfg.console_level = CON_LOG_LEVEL; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 339 | log_cfg.cb = &log_writer; |
Nanang Izzuddin | 83457d5 | 2009-02-16 16:29:05 +0000 | [diff] [blame] | 340 | log_cfg.log_filename = pj_str("C:\\data\\symbian_ua.log"); |
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_media_config_default(&med_cfg); |
| 343 | med_cfg.thread_cnt = 0; // Disable threading on Symbian |
| 344 | med_cfg.has_ioqueue = PJ_FALSE; |
| 345 | med_cfg.clock_rate = 8000; |
Benny Prijono | 5df8bb6 | 2007-12-28 18:55:02 +0000 | [diff] [blame] | 346 | med_cfg.audio_frame_ptime = 40; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 347 | med_cfg.ec_tail_len = 0; |
| 348 | med_cfg.enable_ice = USE_ICE; |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 349 | med_cfg.snd_auto_close_time = 0; // wait for 0 seconds idle before sound dev get auto-closed |
Nanang Izzuddin | a377597 | 2009-03-03 18:25:55 +0000 | [diff] [blame] | 350 | //med_cfg.no_vad = PJ_TRUE; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 351 | |
| 352 | status = pjsua_init(&cfg, &log_cfg, &med_cfg); |
| 353 | if (status != PJ_SUCCESS) { |
| 354 | pjsua_perror(THIS_FILE, "pjsua_init() error", status); |
| 355 | pjsua_destroy(); |
| 356 | return status; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 357 | } |
Nanang Izzuddin | 829ac02 | 2008-05-27 00:24:26 +0000 | [diff] [blame] | 358 | |
| 359 | /* Adjust Speex priority and enable only the narrowband */ |
| 360 | { |
| 361 | pj_str_t codec_id = pj_str("speex/8000"); |
| 362 | pjmedia_codec_mgr_set_codec_priority( |
| 363 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 364 | &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 365 | |
Nanang Izzuddin | 829ac02 | 2008-05-27 00:24:26 +0000 | [diff] [blame] | 366 | codec_id = pj_str("speex/16000"); |
| 367 | pjmedia_codec_mgr_set_codec_priority( |
| 368 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 369 | &codec_id, PJMEDIA_CODEC_PRIO_DISABLED); |
| 370 | |
| 371 | codec_id = pj_str("speex/32000"); |
| 372 | pjmedia_codec_mgr_set_codec_priority( |
| 373 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 374 | &codec_id, PJMEDIA_CODEC_PRIO_DISABLED); |
| 375 | } |
| 376 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 377 | /* Add UDP transport. */ |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 378 | pjsua_transport_config tcfg; |
| 379 | pjsua_transport_id tid; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 380 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 381 | pjsua_transport_config_default(&tcfg); |
| 382 | tcfg.port = SIP_PORT; |
| 383 | status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tcfg, &tid); |
| 384 | if (status != PJ_SUCCESS) { |
Nanang Izzuddin | 940d3f6 | 2009-06-22 14:06:40 +0000 | [diff] [blame] | 385 | pjsua_perror(THIS_FILE, "Error creating UDP transport", status); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 386 | pjsua_destroy(); |
| 387 | return status; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Nanang Izzuddin | 940d3f6 | 2009-06-22 14:06:40 +0000 | [diff] [blame] | 390 | /* Add TCP transport */ |
| 391 | #if ENABLE_SIP_TCP |
| 392 | pjsua_transport_config_default(&tcfg); |
| 393 | tcfg.port = SIP_PORT; |
| 394 | status = pjsua_transport_create(PJSIP_TRANSPORT_TCP, &tcfg, &tid); |
| 395 | if (status != PJ_SUCCESS) { |
| 396 | pjsua_perror(THIS_FILE, "Error creating TCP transport", status); |
| 397 | pjsua_destroy(); |
| 398 | return status; |
| 399 | } |
| 400 | #endif |
| 401 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 402 | /* Add account for the transport */ |
| 403 | pjsua_acc_add_local(tid, PJ_TRUE, &g_acc_id); |
| 404 | |
| 405 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 406 | /* Initialization is done, now start pjsua */ |
| 407 | status = pjsua_start(); |
| 408 | if (status != PJ_SUCCESS) { |
| 409 | pjsua_perror(THIS_FILE, "Error starting pjsua", status); |
| 410 | pjsua_destroy(); |
| 411 | return status; |
| 412 | } |
| 413 | |
| 414 | /* Register to SIP server by creating SIP account. */ |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 415 | if (HAS_SIP_ACCOUNT) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 416 | pjsua_acc_config cfg; |
| 417 | |
| 418 | pjsua_acc_config_default(&cfg); |
| 419 | cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN); |
| 420 | cfg.reg_uri = pj_str("sip:" SIP_DOMAIN); |
| 421 | cfg.cred_count = 1; |
Benny Prijono | 0e2c646 | 2008-02-14 13:39:24 +0000 | [diff] [blame] | 422 | cfg.cred_info[0].realm = pj_str("*"); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 423 | cfg.cred_info[0].scheme = pj_str("digest"); |
| 424 | cfg.cred_info[0].username = pj_str(SIP_USER); |
| 425 | cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 426 | cfg.cred_info[0].data = pj_str(SIP_PASSWD); |
| 427 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 428 | status = pjsua_acc_add(&cfg, PJ_TRUE, &g_acc_id); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 429 | if (status != PJ_SUCCESS) { |
| 430 | pjsua_perror(THIS_FILE, "Error adding account", status); |
| 431 | pjsua_destroy(); |
| 432 | return status; |
| 433 | } |
| 434 | } |
| 435 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 436 | if (SIP_DST_URI) { |
| 437 | pjsua_buddy_config bcfg; |
| 438 | |
| 439 | pjsua_buddy_config_default(&bcfg); |
| 440 | bcfg.uri = pj_str(SIP_DST_URI); |
| 441 | bcfg.subscribe = PJ_FALSE; |
| 442 | |
| 443 | pjsua_buddy_add(&bcfg, &g_buddy_id); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 444 | } |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 445 | return PJ_SUCCESS; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | //////////////////////////////////////////////////////////////////////////// |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 450 | /* |
| 451 | * The interractive console UI |
| 452 | */ |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 453 | #include <e32base.h> |
| 454 | |
| 455 | class ConsoleUI : public CActive |
| 456 | { |
| 457 | public: |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 458 | ConsoleUI(CConsoleBase *con); |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 459 | ~ConsoleUI(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 460 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 461 | // Run console UI |
| 462 | void Run(); |
| 463 | |
| 464 | // Stop |
| 465 | void Stop(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 466 | |
| 467 | protected: |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 468 | // Cancel asynchronous read. |
| 469 | void DoCancel(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 470 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 471 | // Implementation: called when read has completed. |
| 472 | void RunL(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 473 | |
| 474 | private: |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 475 | CConsoleBase *con_; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 479 | ConsoleUI::ConsoleUI(CConsoleBase *con) |
| 480 | : CActive(EPriorityStandard), con_(con) |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 481 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 482 | CActiveScheduler::Add(this); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 485 | ConsoleUI::~ConsoleUI() |
| 486 | { |
| 487 | Stop(); |
| 488 | } |
| 489 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 490 | // Run console UI |
| 491 | void ConsoleUI::Run() |
| 492 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 493 | con_->Read(iStatus); |
| 494 | SetActive(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Stop console UI |
| 498 | void ConsoleUI::Stop() |
| 499 | { |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 500 | Cancel(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // Cancel asynchronous read. |
| 504 | void ConsoleUI::DoCancel() |
| 505 | { |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 506 | con_->ReadCancel(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 509 | static void PrintMainMenu() |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 510 | { |
Nanang Izzuddin | da37ea3 | 2009-03-06 16:24:43 +0000 | [diff] [blame] | 511 | const char *menu = |
| 512 | "\n\n" |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 513 | "Main Menu:\n" |
| 514 | " d Enable/disable codecs\n" |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 515 | " m Call " SIP_DST_URI "\n" |
| 516 | " a Answer call\n" |
| 517 | " g Hangup all calls\n" |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 518 | " t Toggle audio route\n" |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 519 | #if !defined(PJMEDIA_CONF_USE_SWITCH_BOARD) || PJMEDIA_CONF_USE_SWITCH_BOARD==0 |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 520 | " j Toggle loopback audio\n" |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 521 | #endif |
Nanang Izzuddin | 08694ea | 2009-03-10 15:03:04 +0000 | [diff] [blame] | 522 | "up/dn Increase/decrease output volume\n" |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 523 | " s Subscribe " SIP_DST_URI "\n" |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 524 | " S Unsubscribe presence\n" |
| 525 | " o Set account online\n" |
| 526 | " O Set account offline\n" |
Nanang Izzuddin | da37ea3 | 2009-03-06 16:24:43 +0000 | [diff] [blame] | 527 | " w Quit\n"; |
| 528 | |
| 529 | PJ_LOG(3, (THIS_FILE, menu)); |
Benny Prijono | 72a81aa | 2007-05-02 23:06:11 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 532 | static void PrintCodecMenu() |
| 533 | { |
Nanang Izzuddin | da37ea3 | 2009-03-06 16:24:43 +0000 | [diff] [blame] | 534 | const char *menu = |
| 535 | "\n\n" |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 536 | "Codec Menu:\n" |
| 537 | " a Enable all codecs\n" |
Nanang Izzuddin | a377597 | 2009-03-03 18:25:55 +0000 | [diff] [blame] | 538 | #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 539 | " d Enable only AMR\n" |
Nanang Izzuddin | a377597 | 2009-03-03 18:25:55 +0000 | [diff] [blame] | 540 | #endif |
| 541 | #if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729 |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 542 | " g Enable only G.729\n" |
Nanang Izzuddin | a377597 | 2009-03-03 18:25:55 +0000 | [diff] [blame] | 543 | #endif |
| 544 | #if PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 545 | " j Enable only iLBC\n" |
| 546 | #endif |
| 547 | " m Enable only Speex\n" |
| 548 | " p Enable only GSM\n" |
| 549 | " t Enable only PCMU\n" |
Nanang Izzuddin | da37ea3 | 2009-03-06 16:24:43 +0000 | [diff] [blame] | 550 | " w Enable only PCMA\n"; |
| 551 | |
| 552 | PJ_LOG(3, (THIS_FILE, menu)); |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void HandleMainMenu(TKeyCode kc) { |
| 556 | switch (kc) { |
| 557 | |
Nanang Izzuddin | 08694ea | 2009-03-10 15:03:04 +0000 | [diff] [blame] | 558 | case EKeyUpArrow: |
| 559 | case EKeyDownArrow: |
| 560 | { |
| 561 | unsigned vol; |
| 562 | pj_status_t status; |
| 563 | |
| 564 | status = pjsua_snd_get_setting( |
| 565 | PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, &vol); |
| 566 | if (status == PJ_SUCCESS) { |
| 567 | if (kc == EKeyUpArrow) |
| 568 | vol = PJ_MIN(100, vol+10); |
| 569 | else |
| 570 | vol = (vol>=10 ? vol-10 : 0); |
| 571 | status = pjsua_snd_set_setting( |
| 572 | PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, |
| 573 | &vol, PJ_TRUE); |
| 574 | } |
| 575 | |
| 576 | if (status == PJ_SUCCESS) { |
| 577 | PJ_LOG(3,(THIS_FILE, "Output volume set to %d", vol)); |
| 578 | } else { |
| 579 | pjsua_perror(THIS_FILE, "Error setting volume", status); |
| 580 | } |
| 581 | } |
| 582 | break; |
| 583 | |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 584 | case 't': |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 585 | { |
Nanang Izzuddin | fcb92d0 | 2009-03-10 13:32:09 +0000 | [diff] [blame] | 586 | pjmedia_aud_dev_route route; |
| 587 | pj_status_t status; |
| 588 | |
| 589 | status = pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, |
| 590 | &route); |
| 591 | |
| 592 | if (status == PJ_SUCCESS) { |
| 593 | if (route == PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER) |
| 594 | route = PJMEDIA_AUD_DEV_ROUTE_EARPIECE; |
| 595 | else |
| 596 | route = PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER; |
Nanang Izzuddin | 452b66b | 2009-02-18 15:23:47 +0000 | [diff] [blame] | 597 | |
Nanang Izzuddin | fcb92d0 | 2009-03-10 13:32:09 +0000 | [diff] [blame] | 598 | status = pjsua_snd_set_setting( |
| 599 | PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, |
| 600 | &route, PJ_TRUE); |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 601 | } |
Nanang Izzuddin | fcb92d0 | 2009-03-10 13:32:09 +0000 | [diff] [blame] | 602 | |
| 603 | if (status != PJ_SUCCESS) |
| 604 | pjsua_perror(THIS_FILE, "Error switch audio route", status); |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 605 | } |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 606 | break; |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 607 | |
| 608 | case 'j': |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 609 | { |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 610 | static pj_bool_t loopback_active = PJ_FALSE; |
| 611 | if (!loopback_active) |
| 612 | pjsua_conf_connect(0, 0); |
| 613 | else |
| 614 | pjsua_conf_disconnect(0, 0); |
| 615 | loopback_active = !loopback_active; |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 616 | } |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 617 | break; |
| 618 | |
| 619 | case 'm': |
| 620 | if (g_call_id != PJSUA_INVALID_ID) { |
| 621 | PJ_LOG(3,(THIS_FILE, "Another call is active")); |
| 622 | break; |
| 623 | } |
| 624 | |
| 625 | if (pjsua_verify_sip_url(SIP_DST_URI) == PJ_SUCCESS) { |
| 626 | pj_str_t dst = pj_str(SIP_DST_URI); |
| 627 | pjsua_call_make_call(g_acc_id, &dst, 0, NULL, |
| 628 | NULL, &g_call_id); |
| 629 | } else { |
| 630 | PJ_LOG(3,(THIS_FILE, "Invalid SIP URI")); |
| 631 | } |
| 632 | break; |
| 633 | case 'a': |
| 634 | if (g_call_id != PJSUA_INVALID_ID) |
| 635 | pjsua_call_answer(g_call_id, 200, NULL, NULL); |
| 636 | break; |
| 637 | case 'g': |
| 638 | pjsua_call_hangup_all(); |
| 639 | break; |
| 640 | case 's': |
| 641 | case 'S': |
| 642 | if (g_buddy_id != PJSUA_INVALID_ID) |
| 643 | pjsua_buddy_subscribe_pres(g_buddy_id, kc=='s'); |
| 644 | break; |
| 645 | case 'o': |
| 646 | case 'O': |
| 647 | pjsua_acc_set_online_status(g_acc_id, kc=='o'); |
| 648 | break; |
| 649 | |
| 650 | default: |
| 651 | PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed", kc, kc)); |
| 652 | break; |
| 653 | } |
| 654 | |
| 655 | PrintMainMenu(); |
| 656 | } |
| 657 | |
| 658 | static void HandleCodecMenu(TKeyCode kc) { |
| 659 | const pj_str_t ID_ALL = {"*", 1}; |
| 660 | pj_str_t codec = {NULL, 0}; |
| 661 | |
| 662 | if (kc == 'a') { |
| 663 | pjsua_codec_set_priority(&ID_ALL, PJMEDIA_CODEC_PRIO_NORMAL); |
| 664 | PJ_LOG(3,(THIS_FILE, "All codecs activated")); |
| 665 | } else { |
| 666 | switch (kc) { |
| 667 | case 'd': |
| 668 | codec = pj_str("AMR"); |
| 669 | break; |
| 670 | case 'g': |
| 671 | codec = pj_str("G729"); |
| 672 | break; |
| 673 | case 'j': |
| 674 | codec = pj_str("ILBC"); |
| 675 | break; |
| 676 | case 'm': |
| 677 | codec = pj_str("SPEEX/8000"); |
| 678 | break; |
| 679 | case 'p': |
| 680 | codec = pj_str("GSM"); |
| 681 | break; |
| 682 | case 't': |
| 683 | codec = pj_str("PCMU"); |
| 684 | break; |
| 685 | case 'w': |
| 686 | codec = pj_str("PCMA"); |
| 687 | break; |
| 688 | default: |
| 689 | PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed", kc, kc)); |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | if (codec.slen) { |
| 694 | pj_status_t status; |
| 695 | |
| 696 | pjsua_codec_set_priority(&ID_ALL, PJMEDIA_CODEC_PRIO_DISABLED); |
| 697 | |
| 698 | status = pjsua_codec_set_priority(&codec, |
| 699 | PJMEDIA_CODEC_PRIO_NORMAL); |
| 700 | if (status == PJ_SUCCESS) |
| 701 | PJ_LOG(3,(THIS_FILE, "%s activated", codec.ptr)); |
| 702 | else |
| 703 | PJ_LOG(3,(THIS_FILE, "Failed activating %s, err=%d", |
| 704 | codec.ptr, status)); |
| 705 | } |
| 706 | } |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // Implementation: called when read has completed. |
| 710 | void ConsoleUI::RunL() |
| 711 | { |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 712 | enum { |
| 713 | MENU_TYPE_MAIN = 0, |
| 714 | MENU_TYPE_CODEC = 1 |
| 715 | }; |
| 716 | static int menu_type = MENU_TYPE_MAIN; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 717 | TKeyCode kc = con_->KeyCode(); |
| 718 | pj_bool_t reschedule = PJ_TRUE; |
| 719 | |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 720 | if (menu_type == MENU_TYPE_MAIN) { |
| 721 | if (kc == 'w') { |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 722 | CActiveScheduler::Stop(); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 723 | reschedule = PJ_FALSE; |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 724 | } else if (kc == 'd') { |
| 725 | menu_type = MENU_TYPE_CODEC; |
| 726 | PrintCodecMenu(); |
| 727 | } else { |
| 728 | HandleMainMenu(kc); |
| 729 | } |
| 730 | } else { |
| 731 | HandleCodecMenu(kc); |
| 732 | |
| 733 | menu_type = MENU_TYPE_MAIN; |
| 734 | PrintMainMenu(); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 735 | } |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 736 | |
| 737 | if (reschedule) |
| 738 | Run(); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 741 | #if 0 |
| 742 | // IP networking related testing |
| 743 | static pj_status_t test_addr(void) |
| 744 | { |
| 745 | int af; |
| 746 | unsigned i, count; |
| 747 | pj_addrinfo ai[8]; |
| 748 | pj_sockaddr ifs[8]; |
| 749 | const pj_str_t *hostname; |
| 750 | pj_hostent he; |
| 751 | pj_status_t status; |
| 752 | |
| 753 | pj_log_set_log_func(&log_writer); |
| 754 | |
| 755 | status = pj_init(); |
| 756 | if (status != PJ_SUCCESS) { |
| 757 | pjsua_perror(THIS_FILE, "pj_init() error", status); |
| 758 | return status; |
| 759 | } |
| 760 | |
| 761 | af = pj_AF_INET(); |
| 762 | |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 763 | #if 0 |
| 764 | pj_in_addr in_addr; |
| 765 | pj_str_t aa = pj_str("1.1.1.1"); |
| 766 | in_addr = pj_inet_addr(&aa); |
| 767 | char *the_addr = pj_inet_ntoa(in_addr); |
| 768 | PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr)); |
| 769 | |
| 770 | aa = pj_str("192.168.0.15"); |
| 771 | in_addr = pj_inet_addr(&aa); |
| 772 | the_addr = pj_inet_ntoa(in_addr); |
| 773 | PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr)); |
| 774 | |
| 775 | aa = pj_str("2.2.2.2"); |
| 776 | in_addr = pj_inet_addr(&aa); |
| 777 | the_addr = pj_inet_ntoa(in_addr); |
| 778 | PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr)); |
| 779 | |
| 780 | return -1; |
| 781 | #endif |
| 782 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 783 | // Hostname |
| 784 | hostname = pj_gethostname(); |
| 785 | if (hostname == NULL) { |
| 786 | status = PJ_ERESOLVE; |
| 787 | pjsua_perror(THIS_FILE, "pj_gethostname() error", status); |
| 788 | goto on_return; |
| 789 | } |
| 790 | |
| 791 | PJ_LOG(3,(THIS_FILE, "Hostname: %.*s", hostname->slen, hostname->ptr)); |
| 792 | |
| 793 | // Gethostbyname |
| 794 | status = pj_gethostbyname(hostname, &he); |
| 795 | if (status != PJ_SUCCESS) { |
| 796 | pjsua_perror(THIS_FILE, "pj_gethostbyname() error", status); |
| 797 | } else { |
| 798 | PJ_LOG(3,(THIS_FILE, "gethostbyname: %s", |
| 799 | pj_inet_ntoa(*(pj_in_addr*)he.h_addr))); |
| 800 | } |
| 801 | |
| 802 | // Getaddrinfo |
| 803 | count = PJ_ARRAY_SIZE(ai); |
| 804 | status = pj_getaddrinfo(af, hostname, &count, ai); |
| 805 | if (status != PJ_SUCCESS) { |
| 806 | pjsua_perror(THIS_FILE, "pj_getaddrinfo() error", status); |
| 807 | } else { |
| 808 | for (i=0; i<count; ++i) { |
| 809 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 810 | PJ_LOG(3,(THIS_FILE, "Addrinfo: %s", |
| 811 | pj_sockaddr_print(&ai[i].ai_addr, ipaddr, sizeof(ipaddr), 2))); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // Enum interface |
| 816 | count = PJ_ARRAY_SIZE(ifs); |
| 817 | status = pj_enum_ip_interface(af, &count, ifs); |
| 818 | if (status != PJ_SUCCESS) { |
| 819 | pjsua_perror(THIS_FILE, "pj_enum_ip_interface() error", status); |
| 820 | } else { |
| 821 | for (i=0; i<count; ++i) { |
| 822 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 823 | PJ_LOG(3,(THIS_FILE, "Interface: %s", |
| 824 | pj_sockaddr_print(&ifs[i], ipaddr, sizeof(ipaddr), 2))); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // Get default iinterface |
| 829 | status = pj_getdefaultipinterface(af, &ifs[0]); |
| 830 | if (status != PJ_SUCCESS) { |
| 831 | pjsua_perror(THIS_FILE, "pj_getdefaultipinterface() error", status); |
| 832 | } else { |
| 833 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 834 | PJ_LOG(3,(THIS_FILE, "Default IP: %s", |
| 835 | pj_sockaddr_print(&ifs[0], ipaddr, sizeof(ipaddr), 2))); |
| 836 | } |
| 837 | |
| 838 | // Get default IP address |
| 839 | status = pj_gethostip(af, &ifs[0]); |
| 840 | if (status != PJ_SUCCESS) { |
| 841 | pjsua_perror(THIS_FILE, "pj_gethostip() error", status); |
| 842 | } else { |
| 843 | char ipaddr[PJ_INET6_ADDRSTRLEN+2]; |
| 844 | PJ_LOG(3,(THIS_FILE, "Host IP: %s", |
| 845 | pj_sockaddr_print(&ifs[0], ipaddr, sizeof(ipaddr), 2))); |
| 846 | } |
| 847 | |
| 848 | status = -1; |
| 849 | |
| 850 | on_return: |
| 851 | pj_shutdown(); |
| 852 | return status; |
| 853 | } |
| 854 | #endif |
| 855 | |
| 856 | |
Benny Prijono | 6b3ccdf | 2008-01-14 11:54:21 +0000 | [diff] [blame] | 857 | #include <es_sock.h> |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 858 | |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 859 | #if 0 |
| 860 | // Force network connection to use the first IAP, |
| 861 | // this is useful for debugging on emulator without GUI. |
| 862 | // Include commdb.lib & apengine.lib in symbian_ua.mmp file |
| 863 | // if this is enabled. |
| 864 | |
| 865 | #include <apdatahandler.h> |
| 866 | |
| 867 | inline void ForceUseFirstIAP() |
| 868 | { |
| 869 | TUint32 rank = 1; |
| 870 | TUint32 bearers; |
| 871 | TUint32 prompt; |
| 872 | TUint32 iap; |
| 873 | |
| 874 | CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP); |
| 875 | CleanupStack::PushL(commDb); |
| 876 | |
| 877 | CApDataHandler* apDataHandler = CApDataHandler::NewLC(*commDb); |
| 878 | |
| 879 | TCommDbConnectionDirection direction = ECommDbConnectionDirectionOutgoing; |
| 880 | apDataHandler->GetPreferredIfDbIapTypeL(rank, direction, bearers, prompt, iap); |
| 881 | prompt = ECommDbDialogPrefDoNotPrompt; |
| 882 | apDataHandler->SetPreferredIfDbIapTypeL(rank, direction, bearers, (TCommDbDialogPref)prompt, iap, ETrue); |
| 883 | CleanupStack::PopAndDestroy(2); // apDataHandler, commDb |
| 884 | } |
| 885 | |
| 886 | static void SelectIAP() |
| 887 | { |
| 888 | ForceUseFirstIAP(); |
| 889 | } |
| 890 | |
| 891 | #else |
| 892 | |
| 893 | static void SelectIAP() |
| 894 | { |
| 895 | } |
| 896 | |
| 897 | #endif |
| 898 | |
| 899 | |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 900 | // Class CConnMon to monitor network connection (RConnection). Whenever |
| 901 | // the connection is down, it will notify PJLIB and restart PJSUA-LIB. |
| 902 | class CConnMon : public CActive { |
| 903 | public: |
| 904 | static CConnMon* NewL(RConnection &conn, RSocketServ &sserver) { |
| 905 | CConnMon *self = new (ELeave) CConnMon(conn, sserver); |
| 906 | CleanupStack::PushL(self); |
| 907 | self->ConstructL(); |
| 908 | CleanupStack::Pop(self); |
| 909 | return self; |
| 910 | } |
| 911 | |
| 912 | void Start() { |
| 913 | conn_.ProgressNotification(nif_progress_, iStatus); |
| 914 | SetActive(); |
| 915 | } |
| 916 | |
| 917 | void Stop() { |
| 918 | Cancel(); |
| 919 | } |
| 920 | |
| 921 | ~CConnMon() { Stop(); } |
| 922 | |
| 923 | private: |
| 924 | CConnMon(RConnection &conn, RSocketServ &sserver) : |
| 925 | CActive(EPriorityHigh), |
| 926 | conn_(conn), |
| 927 | sserver_(sserver) |
| 928 | { |
| 929 | CActiveScheduler::Add(this); |
| 930 | } |
| 931 | |
| 932 | void ConstructL() {} |
| 933 | |
| 934 | void DoCancel() { |
| 935 | conn_.CancelProgressNotification(); |
| 936 | } |
| 937 | |
| 938 | void RunL() { |
| 939 | int stage = nif_progress_().iStage; |
| 940 | |
| 941 | if (stage == KLinkLayerClosed) { |
| 942 | pj_status_t status; |
| 943 | TInt err; |
| 944 | |
| 945 | // Tell pjlib that connection is down. |
| 946 | pj_symbianos_set_connection_status(PJ_FALSE); |
| 947 | |
| 948 | PJ_LOG(3, (THIS_FILE, "RConnection closed, restarting PJSUA..")); |
| 949 | |
| 950 | // Destroy pjsua |
| 951 | pjsua_destroy(); |
| 952 | PJ_LOG(3, (THIS_FILE, "PJSUA destroyed.")); |
| 953 | |
| 954 | // Reopen the connection |
| 955 | err = conn_.Open(sserver_); |
| 956 | if (err == KErrNone) |
| 957 | err = conn_.Start(); |
| 958 | if (err != KErrNone) { |
| 959 | CActiveScheduler::Stop(); |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | // Reinit Symbian OS param before pj_init() |
| 964 | pj_symbianos_params sym_params; |
| 965 | pj_bzero(&sym_params, sizeof(sym_params)); |
| 966 | sym_params.rsocketserv = &sserver_; |
| 967 | sym_params.rconnection = &conn_; |
| 968 | pj_symbianos_set_params(&sym_params); |
| 969 | |
| 970 | // Reinit pjsua |
| 971 | status = app_startup(); |
| 972 | if (status != PJ_SUCCESS) { |
| 973 | pjsua_perror(THIS_FILE, "app_startup() error", status); |
| 974 | CActiveScheduler::Stop(); |
| 975 | return; |
| 976 | } |
| 977 | |
| 978 | PJ_LOG(3, (THIS_FILE, "PJSUA restarted.")); |
Benny Prijono | 9920dc3 | 2009-03-12 18:11:37 +0000 | [diff] [blame] | 979 | PrintMainMenu(); |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | Start(); |
| 983 | } |
| 984 | |
| 985 | RConnection& conn_; |
| 986 | RSocketServ& sserver_; |
| 987 | TNifProgressBuf nif_progress_; |
| 988 | }; |
| 989 | |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 990 | //////////////////////////////////////////////////////////////////////////// |
| 991 | int ua_main() |
| 992 | { |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 993 | RSocketServ aSocketServer; |
| 994 | RConnection aConn; |
| 995 | TInt err; |
| 996 | pj_symbianos_params sym_params; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 997 | pj_status_t status; |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 998 | |
| 999 | SelectIAP(); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1000 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 1001 | // Initialize RSocketServ |
| 1002 | if ((err=aSocketServer.Connect()) != KErrNone) |
| 1003 | return PJ_STATUS_FROM_OS(err); |
| 1004 | |
| 1005 | // Open up a connection |
| 1006 | if ((err=aConn.Open(aSocketServer)) != KErrNone) { |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1007 | aSocketServer.Close(); |
| 1008 | return PJ_STATUS_FROM_OS(err); |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | if ((err=aConn.Start()) != KErrNone) { |
| 1012 | aSocketServer.Close(); |
| 1013 | return PJ_STATUS_FROM_OS(err); |
| 1014 | } |
| 1015 | |
| 1016 | // Set Symbian OS parameters in pjlib. |
| 1017 | // This must be done before pj_init() is called. |
| 1018 | pj_bzero(&sym_params, sizeof(sym_params)); |
| 1019 | sym_params.rsocketserv = &aSocketServer; |
| 1020 | sym_params.rconnection = &aConn; |
| 1021 | pj_symbianos_set_params(&sym_params); |
| 1022 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1023 | // Initialize pjsua |
| 1024 | status = app_startup(); |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 1025 | //status = test_addr(); |
| 1026 | if (status != PJ_SUCCESS) { |
| 1027 | aConn.Close(); |
| 1028 | aSocketServer.Close(); |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1029 | return status; |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 1030 | } |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 1031 | |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 1032 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1033 | // Run the UI |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1034 | ConsoleUI *con = new ConsoleUI(console); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1035 | |
| 1036 | con->Run(); |
Nanang Izzuddin | 798bf45 | 2009-02-12 12:35:27 +0000 | [diff] [blame] | 1037 | PrintMainMenu(); |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1038 | |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 1039 | // Init & start connection monitor |
| 1040 | CConnMon *connmon = CConnMon::NewL(aConn, aSocketServer); |
| 1041 | connmon->Start(); |
| 1042 | |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1043 | CActiveScheduler::Start(); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1044 | |
Nanang Izzuddin | 90b8320 | 2009-03-02 15:48:45 +0000 | [diff] [blame] | 1045 | delete connmon; |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1046 | delete con; |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 1047 | |
| 1048 | // Dump memory statistics |
| 1049 | PJ_LOG(3,(THIS_FILE, "Max heap usage: %u.%03uMB", |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1050 | pjsua_var.cp.peak_used_size / 1000000, |
| 1051 | (pjsua_var.cp.peak_used_size % 1000000)/1000)); |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1052 | |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 1053 | // check max stack usage |
| 1054 | #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0 |
| 1055 | pj_thread_t* this_thread = pj_thread_this(); |
| 1056 | if (!this_thread) |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1057 | return status; |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 1058 | |
| 1059 | const char* max_stack_file; |
| 1060 | int max_stack_line; |
| 1061 | status = pj_thread_get_stack_info(this_thread, &max_stack_file, &max_stack_line); |
| 1062 | |
| 1063 | PJ_LOG(3,(THIS_FILE, "Max stack usage: %u at %s:%d", |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1064 | pj_thread_get_stack_max_usage(this_thread), |
| 1065 | max_stack_file, max_stack_line)); |
Benny Prijono | 684c0ad | 2008-01-03 18:50:27 +0000 | [diff] [blame] | 1066 | #endif |
| 1067 | |
Benny Prijono | c71ad43 | 2007-05-04 07:25:19 +0000 | [diff] [blame] | 1068 | // Shutdown pjsua |
| 1069 | pjsua_destroy(); |
| 1070 | |
Benny Prijono | 70c5ba0 | 2007-12-31 11:27:35 +0000 | [diff] [blame] | 1071 | // Close connection and socket server |
| 1072 | aConn.Close(); |
Nanang Izzuddin | 82f7a41 | 2008-12-17 11:36:22 +0000 | [diff] [blame] | 1073 | aSocketServer.Close(); |
Nanang Izzuddin | a940b36 | 2009-02-23 13:53:30 +0000 | [diff] [blame] | 1074 | |
Benny Prijono | 797e338 | 2007-12-01 09:10:07 +0000 | [diff] [blame] | 1075 | return status; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |