blob: 51e0c050100d2b5500845464c7f08bd727d31878 [file] [log] [blame]
Benny Prijonoba5926a2007-05-02 11:29:37 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono844653c2008-12-23 17:27:53 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoba5926a2007-05-02 11:29:37 +00005 *
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 Prijonoba5926a2007-05-02 11:29:37 +000020#include <pjsua-lib/pjsua.h>
Benny Prijono72a81aa2007-05-02 23:06:11 +000021#include <pjsua-lib/pjsua_internal.h>
Nanang Izzuddina940b362009-02-23 13:53:30 +000022//#include <pjmedia/symbian_sound_aps.h>
Benny Prijonoba5926a2007-05-02 11:29:37 +000023#include "ua.h"
24
25#define THIS_FILE "symbian_ua.cpp"
Nanang Izzuddinda37ea32009-03-06 16:24:43 +000026#define CON_LOG_LEVEL 3 // console log level
27#define FILE_LOG_LEVEL 4 // logfile log level
Benny Prijonoba5926a2007-05-02 11:29:37 +000028
Benny Prijono72a81aa2007-05-02 23:06:11 +000029//
Benny Prijonoc71ad432007-05-04 07:25:19 +000030// Basic config.
31//
32#define SIP_PORT 5060
33
34
35//
Benny Prijonob2c96822007-05-03 13:31:21 +000036// Destination URI (to make call, or to subscribe presence)
37//
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000038#define SIP_DST_URI "<sip:100@pjsip.lab>"
Benny Prijonob2c96822007-05-03 13:31:21 +000039
40//
Benny Prijono72a81aa2007-05-02 23:06:11 +000041// Account
42//
Nanang Izzuddin82f7a412008-12-17 11:36:22 +000043#define HAS_SIP_ACCOUNT 0 // 1 to enable registration
Benny Prijono0e2c6462008-02-14 13:39:24 +000044#define SIP_DOMAIN "pjsip.lab"
45#define SIP_USER "400"
46#define SIP_PASSWD "400"
Benny Prijonoba5926a2007-05-02 11:29:37 +000047
Benny Prijono72a81aa2007-05-02 23:06:11 +000048//
49// Outbound proxy for all accounts
50//
51#define SIP_PROXY NULL
Benny Prijono4b55e0a2008-12-18 13:22:52 +000052//#define SIP_PROXY "<sip:192.168.0.8;lr>"
Benny Prijono897f9f82007-05-03 19:56:21 +000053
Nanang Izzuddin940d3f62009-06-22 14:06:40 +000054//
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000055// SIP transports
Nanang Izzuddin940d3f62009-06-22 14:06:40 +000056//
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000057#define ENABLE_SIP_UDP 1
58#define ENABLE_SIP_TCP 0 // experimental
59#define ENABLE_SIP_TLS 0 // experimental
Benny Prijono897f9f82007-05-03 19:56:21 +000060
61//
62// Configure nameserver if DNS SRV is to be used with both SIP
63// or STUN (for STUN see other settings below)
64//
Nanang Izzuddin82f7a412008-12-17 11:36:22 +000065#define NAMESERVER NULL
66//#define NAMESERVER "192.168.0.2"
Benny Prijono897f9f82007-05-03 19:56:21 +000067
68//
69// STUN server
Benny Prijono684c0ad2008-01-03 18:50:27 +000070#if 0
Benny Prijono897f9f82007-05-03 19:56:21 +000071 // Use this to have the STUN server resolved normally
72# define STUN_DOMAIN NULL
Benny Prijono4b55e0a2008-12-18 13:22:52 +000073# define STUN_SERVER "stun.pjsip.org"
Benny Prijono897f9f82007-05-03 19:56:21 +000074#elif 0
75 // Use this to have the STUN server resolved with DNS SRV
Benny Prijono4b55e0a2008-12-18 13:22:52 +000076# define STUN_DOMAIN "pjsip.org"
Benny Prijono897f9f82007-05-03 19:56:21 +000077# define STUN_SERVER NULL
78#else
79 // Use this to disable STUN
80# define STUN_DOMAIN NULL
81# define STUN_SERVER NULL
82#endif
83
84//
85// Use ICE?
86//
Benny Prijono70c5ba02007-12-31 11:27:35 +000087#define USE_ICE 1
Benny Prijono72a81aa2007-05-02 23:06:11 +000088
Nanang Izzuddin6a6392f2008-06-02 18:30:15 +000089//
90// Use SRTP?
91//
92#define USE_SRTP PJSUA_DEFAULT_USE_SRTP
Benny Prijono72a81aa2007-05-02 23:06:11 +000093
Benny Prijonob2c96822007-05-03 13:31:21 +000094//
Benny Prijonoa25bc9d2009-11-09 08:51:34 +000095// Set QoS on transports? Yes!
96// As an example, we set SIP transports DSCP value to CS3 (DSCP
97// value 24 or 0x18), for no reason, and tag RTP/RTCP packets
98// with VOICE type.
99//
100#define SIP_QOS_DSCP 0x18
101#define RTP_QOS_TYPE PJ_QOS_TYPE_VOICE
102
103
104//
Benny Prijonob2c96822007-05-03 13:31:21 +0000105// Globals
106//
107static pjsua_acc_id g_acc_id = PJSUA_INVALID_ID;
108static pjsua_call_id g_call_id = PJSUA_INVALID_ID;
109static pjsua_buddy_id g_buddy_id = PJSUA_INVALID_ID;
Benny Prijono72a81aa2007-05-02 23:06:11 +0000110
Benny Prijonoba5926a2007-05-02 11:29:37 +0000111
112/* Callback called by the library upon receiving incoming call */
113static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
114 pjsip_rx_data *rdata)
115{
116 pjsua_call_info ci;
117
118 PJ_UNUSED_ARG(acc_id);
119 PJ_UNUSED_ARG(rdata);
120
Benny Prijonob2c96822007-05-03 13:31:21 +0000121 if (g_call_id != PJSUA_INVALID_ID) {
122 pjsua_call_answer(call_id, PJSIP_SC_BUSY_HERE, NULL, NULL);
123 return;
124 }
125
Benny Prijonoba5926a2007-05-02 11:29:37 +0000126 pjsua_call_get_info(call_id, &ci);
127
128 PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
129 (int)ci.remote_info.slen,
130 ci.remote_info.ptr));
131
Benny Prijonob2c96822007-05-03 13:31:21 +0000132 g_call_id = call_id;
133
Benny Prijono897f9f82007-05-03 19:56:21 +0000134 /* Automatically answer incoming calls with 180/Ringing */
135 pjsua_call_answer(call_id, 180, NULL, NULL);
Benny Prijonoba5926a2007-05-02 11:29:37 +0000136}
137
138/* Callback called by the library when call's state has changed */
139static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
140{
141 pjsua_call_info ci;
142
143 PJ_UNUSED_ARG(e);
144
145 pjsua_call_get_info(call_id, &ci);
Benny Prijonob2c96822007-05-03 13:31:21 +0000146
147 if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
148 if (call_id == g_call_id)
149 g_call_id = PJSUA_INVALID_ID;
Benny Prijono897f9f82007-05-03 19:56:21 +0000150 } else if (ci.state != PJSIP_INV_STATE_INCOMING) {
Benny Prijonob2c96822007-05-03 13:31:21 +0000151 if (g_call_id == PJSUA_INVALID_ID)
152 g_call_id = call_id;
153 }
154
Benny Prijonoba5926a2007-05-02 11:29:37 +0000155 PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
156 (int)ci.state_text.slen,
157 ci.state_text.ptr));
158}
159
160/* Callback called by the library when call's media state has changed */
161static void on_call_media_state(pjsua_call_id call_id)
162{
163 pjsua_call_info ci;
164
165 pjsua_call_get_info(call_id, &ci);
166
167 if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
168 // When media is active, connect call to sound device.
169 pjsua_conf_connect(ci.conf_slot, 0);
170 pjsua_conf_connect(0, ci.conf_slot);
171 }
172}
173
174
Benny Prijonob2c96822007-05-03 13:31:21 +0000175/* Handler on buddy state changed. */
176static void on_buddy_state(pjsua_buddy_id buddy_id)
177{
178 pjsua_buddy_info info;
179 pjsua_buddy_get_info(buddy_id, &info);
180
181 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
182 (int)info.uri.slen,
183 info.uri.ptr,
184 (int)info.status_text.slen,
185 info.status_text.ptr));
186}
187
188
189/* Incoming IM message (i.e. MESSAGE request)! */
190static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
191 const pj_str_t *to, const pj_str_t *contact,
192 const pj_str_t *mime_type, const pj_str_t *text)
193{
194 /* Note: call index may be -1 */
195 PJ_UNUSED_ARG(call_id);
196 PJ_UNUSED_ARG(to);
197 PJ_UNUSED_ARG(contact);
198 PJ_UNUSED_ARG(mime_type);
199
200 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s",
201 (int)from->slen, from->ptr,
202 (int)text->slen, text->ptr));
203}
204
205
206/* Received typing indication */
207static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
208 const pj_str_t *to, const pj_str_t *contact,
209 pj_bool_t is_typing)
210{
211 PJ_UNUSED_ARG(call_id);
212 PJ_UNUSED_ARG(to);
213 PJ_UNUSED_ARG(contact);
214
215 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
216 (int)from->slen, from->ptr,
217 (is_typing?"is typing..":"has stopped typing")));
218}
219
220
221/* Call transfer request status. */
222static void on_call_transfer_status(pjsua_call_id call_id,
223 int status_code,
224 const pj_str_t *status_text,
225 pj_bool_t final,
226 pj_bool_t *p_cont)
227{
228 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
229 call_id, status_code,
230 (int)status_text->slen, status_text->ptr,
231 (final ? "[final]" : "")));
232
233 if (status_code/100 == 2) {
234 PJ_LOG(3,(THIS_FILE,
235 "Call %d: call transfered successfully, disconnecting call",
236 call_id));
237 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
238 *p_cont = PJ_FALSE;
239 }
240}
241
242
Benny Prijonoaecabfc2007-10-26 05:35:42 +0000243/* NAT detection result */
244static void on_nat_detect(const pj_stun_nat_detect_result *res)
245{
246 if (res->status != PJ_SUCCESS) {
247 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
248 } else {
249 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
250 }
251}
252
Benny Prijonob2c96822007-05-03 13:31:21 +0000253/* Notification that call is being replaced. */
254static void on_call_replaced(pjsua_call_id old_call_id,
255 pjsua_call_id new_call_id)
256{
257 pjsua_call_info old_ci, new_ci;
258
259 pjsua_call_get_info(old_call_id, &old_ci);
260 pjsua_call_get_info(new_call_id, &new_ci);
261
262 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
263 "call %d with %.*s",
264 old_call_id,
265 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
266 new_call_id,
267 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
268}
269
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000270/*
271 * Transport status notification
272 */
Nanang Izzuddin5e69da52010-02-25 11:58:19 +0000273static void on_transport_state(pjsip_transport *tp,
274 pjsip_transport_state state,
275 const pjsip_transport_state_info *info)
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000276{
277 char host_port[128];
278
279 pj_ansi_snprintf(host_port, sizeof(host_port), "[%.*s:%d]",
280 (int)tp->remote_name.host.slen,
281 tp->remote_name.host.ptr,
282 tp->remote_name.port);
283
Nanang Izzuddin5e69da52010-02-25 11:58:19 +0000284 switch (state) {
285 case PJSIP_TP_STATE_CONNECTED:
286 {
287 PJ_LOG(3,(THIS_FILE, "SIP transport %s is connected to %s",
288 tp->type_name, host_port));
289 }
290 break;
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000291
Nanang Izzuddin5e69da52010-02-25 11:58:19 +0000292 case PJSIP_TP_STATE_DISCONNECTED:
293 {
294 char buf[100];
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000295
Nanang Izzuddin5e69da52010-02-25 11:58:19 +0000296 snprintf(buf, sizeof(buf), "SIP transport %s is disconnected from %s",
297 tp->type_name, host_port);
298 pjsua_perror(THIS_FILE, buf, info->status);
299 }
300 break;
301
302 default:
303 break;
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000304 }
305
306#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
307
308 if (!pj_ansi_stricmp(tp->type_name, "tls") && info->ext_info &&
Nanang Izzuddin5e69da52010-02-25 11:58:19 +0000309 state == PJSIP_TP_STATE_CONNECTED)
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000310 {
311 pjsip_tls_state_info *tls_info = (pjsip_tls_state_info*)info->ext_info;
312 pj_ssl_sock_info *ssl_sock_info = (pj_ssl_sock_info*)
313 tls_info->ssl_sock_info;
314 char buf[2048];
315
316 /* Dump server TLS certificate */
317 pj_ssl_cert_info_dump(ssl_sock_info->remote_cert_info, " ",
318 buf, sizeof(buf));
319 PJ_LOG(4,(THIS_FILE, "TLS cert info of %s:\n%s", host_port, buf));
320 }
321
322#endif
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000323}
324
Benny Prijonob2c96822007-05-03 13:31:21 +0000325
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000326//#include<e32debug.h>
327
Benny Prijonoba5926a2007-05-02 11:29:37 +0000328/* Logging callback */
Benny Prijono797e3382007-12-01 09:10:07 +0000329static void log_writer(int level, const char *buf, int len)
Benny Prijonoba5926a2007-05-02 11:29:37 +0000330{
Benny Prijonoc71ad432007-05-04 07:25:19 +0000331 static wchar_t buf16[PJ_LOG_MAX_SIZE];
Benny Prijonoba5926a2007-05-02 11:29:37 +0000332
333 PJ_UNUSED_ARG(level);
334
335 pj_ansi_to_unicode(buf, len, buf16, PJ_ARRAY_SIZE(buf16));
336
337 TPtrC16 aBuf((const TUint16*)buf16, (TInt)len);
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000338 //RDebug::Print(aBuf);
Benny Prijonoba5926a2007-05-02 11:29:37 +0000339 console->Write(aBuf);
Benny Prijono797e3382007-12-01 09:10:07 +0000340
Benny Prijonoba5926a2007-05-02 11:29:37 +0000341}
342
343/*
344 * app_startup()
345 *
346 * url may contain URL to call.
347 */
Benny Prijonob2c96822007-05-03 13:31:21 +0000348static pj_status_t app_startup()
Benny Prijonoba5926a2007-05-02 11:29:37 +0000349{
Benny Prijonoba5926a2007-05-02 11:29:37 +0000350 pj_status_t status;
351
352 /* Redirect log before pjsua_init() */
Benny Prijono70c5ba02007-12-31 11:27:35 +0000353 pj_log_set_log_func(&log_writer);
354
355 /* Set log level */
Nanang Izzuddinda37ea32009-03-06 16:24:43 +0000356 pj_log_set_level(CON_LOG_LEVEL);
Benny Prijonoba5926a2007-05-02 11:29:37 +0000357
358 /* Create pjsua first! */
359 status = pjsua_create();
360 if (status != PJ_SUCCESS) {
361 pjsua_perror(THIS_FILE, "pjsua_create() error", status);
362 return status;
363 }
364
Benny Prijonoba5926a2007-05-02 11:29:37 +0000365 /* Init pjsua */
Benny Prijonoc71ad432007-05-04 07:25:19 +0000366 pjsua_config cfg;
367 pjsua_logging_config log_cfg;
368 pjsua_media_config med_cfg;
Benny Prijonoba5926a2007-05-02 11:29:37 +0000369
Benny Prijonoc71ad432007-05-04 07:25:19 +0000370 pjsua_config_default(&cfg);
371 cfg.max_calls = 2;
372 cfg.thread_cnt = 0; // Disable threading on Symbian
Nanang Izzuddin6a6392f2008-06-02 18:30:15 +0000373 cfg.use_srtp = USE_SRTP;
374 cfg.srtp_secure_signaling = 0;
375
Benny Prijonoc71ad432007-05-04 07:25:19 +0000376 cfg.cb.on_incoming_call = &on_incoming_call;
377 cfg.cb.on_call_media_state = &on_call_media_state;
378 cfg.cb.on_call_state = &on_call_state;
379 cfg.cb.on_buddy_state = &on_buddy_state;
380 cfg.cb.on_pager = &on_pager;
381 cfg.cb.on_typing = &on_typing;
382 cfg.cb.on_call_transfer_status = &on_call_transfer_status;
383 cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijonoaecabfc2007-10-26 05:35:42 +0000384 cfg.cb.on_nat_detect = &on_nat_detect;
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000385 cfg.cb.on_transport_state = &on_transport_state;
Benny Prijonoaecabfc2007-10-26 05:35:42 +0000386
Benny Prijonoc71ad432007-05-04 07:25:19 +0000387 if (SIP_PROXY) {
388 cfg.outbound_proxy_cnt = 1;
389 cfg.outbound_proxy[0] = pj_str(SIP_PROXY);
390 }
391
392 if (NAMESERVER) {
393 cfg.nameserver_count = 1;
394 cfg.nameserver[0] = pj_str(NAMESERVER);
395 }
396
397 if (NAMESERVER && STUN_DOMAIN) {
398 cfg.stun_domain = pj_str(STUN_DOMAIN);
399 } else if (STUN_SERVER) {
400 cfg.stun_host = pj_str(STUN_SERVER);
401 }
402
403
404 pjsua_logging_config_default(&log_cfg);
Nanang Izzuddinda37ea32009-03-06 16:24:43 +0000405 log_cfg.level = FILE_LOG_LEVEL;
406 log_cfg.console_level = CON_LOG_LEVEL;
Benny Prijonoc71ad432007-05-04 07:25:19 +0000407 log_cfg.cb = &log_writer;
Nanang Izzuddin83457d52009-02-16 16:29:05 +0000408 log_cfg.log_filename = pj_str("C:\\data\\symbian_ua.log");
Benny Prijonoba5926a2007-05-02 11:29:37 +0000409
Benny Prijonoc71ad432007-05-04 07:25:19 +0000410 pjsua_media_config_default(&med_cfg);
411 med_cfg.thread_cnt = 0; // Disable threading on Symbian
412 med_cfg.has_ioqueue = PJ_FALSE;
413 med_cfg.clock_rate = 8000;
Benny Prijono5df8bb62007-12-28 18:55:02 +0000414 med_cfg.audio_frame_ptime = 40;
Benny Prijonoc71ad432007-05-04 07:25:19 +0000415 med_cfg.ec_tail_len = 0;
416 med_cfg.enable_ice = USE_ICE;
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000417 med_cfg.snd_auto_close_time = 0; // wait for 0 seconds idle before sound dev get auto-closed
Nanang Izzuddina3775972009-03-03 18:25:55 +0000418 //med_cfg.no_vad = PJ_TRUE;
Benny Prijonoc71ad432007-05-04 07:25:19 +0000419
420 status = pjsua_init(&cfg, &log_cfg, &med_cfg);
421 if (status != PJ_SUCCESS) {
422 pjsua_perror(THIS_FILE, "pjsua_init() error", status);
423 pjsua_destroy();
424 return status;
Benny Prijonoba5926a2007-05-02 11:29:37 +0000425 }
Nanang Izzuddin829ac022008-05-27 00:24:26 +0000426
427 /* Adjust Speex priority and enable only the narrowband */
428 {
429 pj_str_t codec_id = pj_str("speex/8000");
430 pjmedia_codec_mgr_set_codec_priority(
431 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
432 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
Benny Prijonoba5926a2007-05-02 11:29:37 +0000433
Nanang Izzuddin829ac022008-05-27 00:24:26 +0000434 codec_id = pj_str("speex/16000");
435 pjmedia_codec_mgr_set_codec_priority(
436 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
437 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
438
439 codec_id = pj_str("speex/32000");
440 pjmedia_codec_mgr_set_codec_priority(
441 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
442 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
443 }
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000444
Nanang Izzuddin829ac022008-05-27 00:24:26 +0000445
Benny Prijonoc71ad432007-05-04 07:25:19 +0000446 pjsua_transport_config tcfg;
447 pjsua_transport_id tid;
Benny Prijonoba5926a2007-05-02 11:29:37 +0000448
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000449#if ENABLE_SIP_UDP
450 /* Add UDP transport. */
Benny Prijonoc71ad432007-05-04 07:25:19 +0000451 pjsua_transport_config_default(&tcfg);
452 tcfg.port = SIP_PORT;
Benny Prijonoa25bc9d2009-11-09 08:51:34 +0000453 if (SIP_QOS_DSCP) {
454 tcfg.qos_params.flags |= PJ_QOS_PARAM_HAS_DSCP;
455 tcfg.qos_params.dscp_val = SIP_QOS_DSCP;
456 }
Benny Prijonoc71ad432007-05-04 07:25:19 +0000457 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tcfg, &tid);
458 if (status != PJ_SUCCESS) {
Nanang Izzuddin940d3f62009-06-22 14:06:40 +0000459 pjsua_perror(THIS_FILE, "Error creating UDP transport", status);
Benny Prijonoc71ad432007-05-04 07:25:19 +0000460 pjsua_destroy();
461 return status;
Benny Prijonoba5926a2007-05-02 11:29:37 +0000462 }
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000463#endif
464
Nanang Izzuddin940d3f62009-06-22 14:06:40 +0000465#if ENABLE_SIP_TCP
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000466 /* Add TCP transport */
Nanang Izzuddin940d3f62009-06-22 14:06:40 +0000467 pjsua_transport_config_default(&tcfg);
468 tcfg.port = SIP_PORT;
Benny Prijonoa25bc9d2009-11-09 08:51:34 +0000469 if (SIP_QOS_DSCP) {
470 tcfg.qos_params.flags |= PJ_QOS_PARAM_HAS_DSCP;
471 tcfg.qos_params.dscp_val = SIP_QOS_DSCP;
472 }
Nanang Izzuddin940d3f62009-06-22 14:06:40 +0000473 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP, &tcfg, &tid);
474 if (status != PJ_SUCCESS) {
475 pjsua_perror(THIS_FILE, "Error creating TCP transport", status);
476 pjsua_destroy();
477 return status;
478 }
479#endif
480
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000481#if ENABLE_SIP_TLS
482 /* Add TLS transport */
483 pjsua_transport_config_default(&tcfg);
484 tcfg.port = SIP_PORT + 1;
Benny Prijonoa25bc9d2009-11-09 08:51:34 +0000485 if (SIP_QOS_DSCP) {
486 tcfg.qos_params.flags |= PJ_QOS_PARAM_HAS_DSCP;
487 tcfg.qos_params.dscp_val = SIP_QOS_DSCP;
488 tcfg.tls_setting.qos_params = tcfg.qos_params;
489 }
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000490 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS, &tcfg, &tid);
491 if (status != PJ_SUCCESS) {
492 pjsua_perror(THIS_FILE, "Error creating TLS transport", status);
493 pjsua_destroy();
494 return status;
495 }
496#endif
497
Benny Prijonoc71ad432007-05-04 07:25:19 +0000498 /* Add account for the transport */
499 pjsua_acc_add_local(tid, PJ_TRUE, &g_acc_id);
500
Nanang Izzuddinf6d2eb62011-07-20 03:05:03 +0000501#if DISABLED_FOR_TICKET_1185
Benny Prijonoa25bc9d2009-11-09 08:51:34 +0000502 /* Create media transports */
503 pjsua_transport_config mtcfg;
504 pjsua_transport_config_default(&mtcfg);
505 mtcfg.port = 4000;
506 mtcfg.qos_type = RTP_QOS_TYPE;
507 status = pjsua_media_transports_create(&mtcfg);
508 if (status != PJ_SUCCESS) {
509 pjsua_perror(THIS_FILE, "Error creating media transports", status);
510 pjsua_destroy();
511 return status;
512 }
Nanang Izzuddinf6d2eb62011-07-20 03:05:03 +0000513#endif
Benny Prijonoa25bc9d2009-11-09 08:51:34 +0000514
Benny Prijonoba5926a2007-05-02 11:29:37 +0000515 /* Initialization is done, now start pjsua */
516 status = pjsua_start();
517 if (status != PJ_SUCCESS) {
518 pjsua_perror(THIS_FILE, "Error starting pjsua", status);
519 pjsua_destroy();
520 return status;
521 }
522
523 /* Register to SIP server by creating SIP account. */
Benny Prijono72a81aa2007-05-02 23:06:11 +0000524 if (HAS_SIP_ACCOUNT) {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000525 pjsua_acc_config cfg;
526
527 pjsua_acc_config_default(&cfg);
528 cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
529 cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
530 cfg.cred_count = 1;
Benny Prijono0e2c6462008-02-14 13:39:24 +0000531 cfg.cred_info[0].realm = pj_str("*");
Benny Prijonoba5926a2007-05-02 11:29:37 +0000532 cfg.cred_info[0].scheme = pj_str("digest");
533 cfg.cred_info[0].username = pj_str(SIP_USER);
534 cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
535 cfg.cred_info[0].data = pj_str(SIP_PASSWD);
536
Benny Prijonob2c96822007-05-03 13:31:21 +0000537 status = pjsua_acc_add(&cfg, PJ_TRUE, &g_acc_id);
Benny Prijonoba5926a2007-05-02 11:29:37 +0000538 if (status != PJ_SUCCESS) {
539 pjsua_perror(THIS_FILE, "Error adding account", status);
540 pjsua_destroy();
541 return status;
542 }
543 }
544
Benny Prijonob2c96822007-05-03 13:31:21 +0000545 if (SIP_DST_URI) {
546 pjsua_buddy_config bcfg;
547
548 pjsua_buddy_config_default(&bcfg);
549 bcfg.uri = pj_str(SIP_DST_URI);
550 bcfg.subscribe = PJ_FALSE;
551
552 pjsua_buddy_add(&bcfg, &g_buddy_id);
Benny Prijonoba5926a2007-05-02 11:29:37 +0000553 }
Benny Prijonoba5926a2007-05-02 11:29:37 +0000554 return PJ_SUCCESS;
555}
556
557
558////////////////////////////////////////////////////////////////////////////
Benny Prijonoc71ad432007-05-04 07:25:19 +0000559/*
560 * The interractive console UI
561 */
Benny Prijonoba5926a2007-05-02 11:29:37 +0000562#include <e32base.h>
563
564class ConsoleUI : public CActive
565{
566public:
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000567 ConsoleUI(CConsoleBase *con);
Nanang Izzuddin90b83202009-03-02 15:48:45 +0000568 ~ConsoleUI();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000569
Benny Prijonoc71ad432007-05-04 07:25:19 +0000570 // Run console UI
571 void Run();
572
573 // Stop
574 void Stop();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000575
576protected:
Benny Prijonoc71ad432007-05-04 07:25:19 +0000577 // Cancel asynchronous read.
578 void DoCancel();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000579
Benny Prijonoc71ad432007-05-04 07:25:19 +0000580 // Implementation: called when read has completed.
581 void RunL();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000582
583private:
Benny Prijonoc71ad432007-05-04 07:25:19 +0000584 CConsoleBase *con_;
Benny Prijonoba5926a2007-05-02 11:29:37 +0000585};
586
587
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000588ConsoleUI::ConsoleUI(CConsoleBase *con)
589: CActive(EPriorityStandard), con_(con)
Benny Prijonoba5926a2007-05-02 11:29:37 +0000590{
Benny Prijonoc71ad432007-05-04 07:25:19 +0000591 CActiveScheduler::Add(this);
Benny Prijonoba5926a2007-05-02 11:29:37 +0000592}
593
Nanang Izzuddin90b83202009-03-02 15:48:45 +0000594ConsoleUI::~ConsoleUI()
595{
596 Stop();
597}
598
Benny Prijonoba5926a2007-05-02 11:29:37 +0000599// Run console UI
600void ConsoleUI::Run()
601{
Benny Prijonoc71ad432007-05-04 07:25:19 +0000602 con_->Read(iStatus);
603 SetActive();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000604}
605
606// Stop console UI
607void ConsoleUI::Stop()
608{
Nanang Izzuddin90b83202009-03-02 15:48:45 +0000609 Cancel();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000610}
611
612// Cancel asynchronous read.
613void ConsoleUI::DoCancel()
614{
Benny Prijonoc71ad432007-05-04 07:25:19 +0000615 con_->ReadCancel();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000616}
617
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000618static void PrintMainMenu()
Benny Prijono72a81aa2007-05-02 23:06:11 +0000619{
Nanang Izzuddinda37ea32009-03-06 16:24:43 +0000620 const char *menu =
621 "\n\n"
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000622 "Main Menu:\n"
623 " d Enable/disable codecs\n"
Benny Prijono70c5ba02007-12-31 11:27:35 +0000624 " m Call " SIP_DST_URI "\n"
625 " a Answer call\n"
626 " g Hangup all calls\n"
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +0000627 " t Toggle audio route\n"
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000628#if !defined(PJMEDIA_CONF_USE_SWITCH_BOARD) || PJMEDIA_CONF_USE_SWITCH_BOARD==0
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +0000629 " j Toggle loopback audio\n"
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000630#endif
Nanang Izzuddin08694ea2009-03-10 15:03:04 +0000631 "up/dn Increase/decrease output volume\n"
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000632 " s Subscribe " SIP_DST_URI "\n"
Benny Prijonoc71ad432007-05-04 07:25:19 +0000633 " S Unsubscribe presence\n"
634 " o Set account online\n"
635 " O Set account offline\n"
Nanang Izzuddinda37ea32009-03-06 16:24:43 +0000636 " w Quit\n";
637
638 PJ_LOG(3, (THIS_FILE, menu));
Benny Prijono72a81aa2007-05-02 23:06:11 +0000639}
640
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000641static void PrintCodecMenu()
642{
Nanang Izzuddinda37ea32009-03-06 16:24:43 +0000643 const char *menu =
644 "\n\n"
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000645 "Codec Menu:\n"
646 " a Enable all codecs\n"
Nanang Izzuddin9608db42010-08-06 03:24:15 +0000647#if PJMEDIA_HAS_PASSTHROUGH_CODECS && PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000648 " d Enable only AMR\n"
Nanang Izzuddina3775972009-03-03 18:25:55 +0000649#endif
Nanang Izzuddin9608db42010-08-06 03:24:15 +0000650#if PJMEDIA_HAS_PASSTHROUGH_CODECS && PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000651 " g Enable only G.729\n"
Nanang Izzuddina3775972009-03-03 18:25:55 +0000652#endif
Nanang Izzuddin9608db42010-08-06 03:24:15 +0000653#if PJMEDIA_HAS_PASSTHROUGH_CODECS && PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000654 " j Enable only iLBC\n"
655#endif
656 " m Enable only Speex\n"
657 " p Enable only GSM\n"
658 " t Enable only PCMU\n"
Nanang Izzuddinda37ea32009-03-06 16:24:43 +0000659 " w Enable only PCMA\n";
660
661 PJ_LOG(3, (THIS_FILE, menu));
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000662}
663
664static void HandleMainMenu(TKeyCode kc) {
665 switch (kc) {
666
Nanang Izzuddin08694ea2009-03-10 15:03:04 +0000667 case EKeyUpArrow:
668 case EKeyDownArrow:
669 {
670 unsigned vol;
671 pj_status_t status;
672
673 status = pjsua_snd_get_setting(
674 PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, &vol);
675 if (status == PJ_SUCCESS) {
676 if (kc == EKeyUpArrow)
677 vol = PJ_MIN(100, vol+10);
678 else
679 vol = (vol>=10 ? vol-10 : 0);
680 status = pjsua_snd_set_setting(
681 PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
682 &vol, PJ_TRUE);
683 }
684
685 if (status == PJ_SUCCESS) {
686 PJ_LOG(3,(THIS_FILE, "Output volume set to %d", vol));
687 } else {
688 pjsua_perror(THIS_FILE, "Error setting volume", status);
689 }
690 }
691 break;
692
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000693 case 't':
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +0000694 {
Nanang Izzuddinfcb92d02009-03-10 13:32:09 +0000695 pjmedia_aud_dev_route route;
696 pj_status_t status;
697
698 status = pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE,
699 &route);
700
701 if (status == PJ_SUCCESS) {
702 if (route == PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER)
703 route = PJMEDIA_AUD_DEV_ROUTE_EARPIECE;
704 else
705 route = PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER;
Nanang Izzuddin452b66b2009-02-18 15:23:47 +0000706
Nanang Izzuddinfcb92d02009-03-10 13:32:09 +0000707 status = pjsua_snd_set_setting(
708 PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE,
709 &route, PJ_TRUE);
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +0000710 }
Nanang Izzuddinfcb92d02009-03-10 13:32:09 +0000711
712 if (status != PJ_SUCCESS)
713 pjsua_perror(THIS_FILE, "Error switch audio route", status);
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +0000714 }
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000715 break;
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000716
717 case 'j':
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +0000718 {
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000719 static pj_bool_t loopback_active = PJ_FALSE;
720 if (!loopback_active)
721 pjsua_conf_connect(0, 0);
722 else
723 pjsua_conf_disconnect(0, 0);
724 loopback_active = !loopback_active;
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +0000725 }
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000726 break;
727
728 case 'm':
729 if (g_call_id != PJSUA_INVALID_ID) {
730 PJ_LOG(3,(THIS_FILE, "Another call is active"));
731 break;
732 }
733
Benny Prijonoc7545782010-09-28 07:43:18 +0000734 if (pjsua_verify_url(SIP_DST_URI) == PJ_SUCCESS) {
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000735 pj_str_t dst = pj_str(SIP_DST_URI);
736 pjsua_call_make_call(g_acc_id, &dst, 0, NULL,
737 NULL, &g_call_id);
738 } else {
739 PJ_LOG(3,(THIS_FILE, "Invalid SIP URI"));
740 }
741 break;
742 case 'a':
743 if (g_call_id != PJSUA_INVALID_ID)
744 pjsua_call_answer(g_call_id, 200, NULL, NULL);
745 break;
746 case 'g':
747 pjsua_call_hangup_all();
748 break;
749 case 's':
750 case 'S':
751 if (g_buddy_id != PJSUA_INVALID_ID)
752 pjsua_buddy_subscribe_pres(g_buddy_id, kc=='s');
753 break;
754 case 'o':
755 case 'O':
756 pjsua_acc_set_online_status(g_acc_id, kc=='o');
757 break;
758
759 default:
760 PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed", kc, kc));
761 break;
762 }
763
764 PrintMainMenu();
765}
766
767static void HandleCodecMenu(TKeyCode kc) {
768 const pj_str_t ID_ALL = {"*", 1};
769 pj_str_t codec = {NULL, 0};
770
771 if (kc == 'a') {
772 pjsua_codec_set_priority(&ID_ALL, PJMEDIA_CODEC_PRIO_NORMAL);
773 PJ_LOG(3,(THIS_FILE, "All codecs activated"));
774 } else {
775 switch (kc) {
776 case 'd':
777 codec = pj_str("AMR");
778 break;
779 case 'g':
780 codec = pj_str("G729");
781 break;
782 case 'j':
783 codec = pj_str("ILBC");
784 break;
785 case 'm':
786 codec = pj_str("SPEEX/8000");
787 break;
788 case 'p':
789 codec = pj_str("GSM");
790 break;
791 case 't':
792 codec = pj_str("PCMU");
793 break;
794 case 'w':
795 codec = pj_str("PCMA");
796 break;
797 default:
798 PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed", kc, kc));
799 break;
800 }
801
802 if (codec.slen) {
803 pj_status_t status;
804
805 pjsua_codec_set_priority(&ID_ALL, PJMEDIA_CODEC_PRIO_DISABLED);
806
807 status = pjsua_codec_set_priority(&codec,
808 PJMEDIA_CODEC_PRIO_NORMAL);
809 if (status == PJ_SUCCESS)
810 PJ_LOG(3,(THIS_FILE, "%s activated", codec.ptr));
811 else
812 PJ_LOG(3,(THIS_FILE, "Failed activating %s, err=%d",
813 codec.ptr, status));
814 }
815 }
Benny Prijonoba5926a2007-05-02 11:29:37 +0000816}
817
818// Implementation: called when read has completed.
819void ConsoleUI::RunL()
820{
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000821 enum {
822 MENU_TYPE_MAIN = 0,
823 MENU_TYPE_CODEC = 1
824 };
825 static int menu_type = MENU_TYPE_MAIN;
Benny Prijonoc71ad432007-05-04 07:25:19 +0000826 TKeyCode kc = con_->KeyCode();
827 pj_bool_t reschedule = PJ_TRUE;
828
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000829 if (menu_type == MENU_TYPE_MAIN) {
830 if (kc == 'w') {
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000831 CActiveScheduler::Stop();
Benny Prijonoc71ad432007-05-04 07:25:19 +0000832 reschedule = PJ_FALSE;
Nanang Izzuddin798bf452009-02-12 12:35:27 +0000833 } else if (kc == 'd') {
834 menu_type = MENU_TYPE_CODEC;
835 PrintCodecMenu();
836 } else {
837 HandleMainMenu(kc);
838 }
839 } else {
840 HandleCodecMenu(kc);
841
842 menu_type = MENU_TYPE_MAIN;
843 PrintMainMenu();
Benny Prijonoc71ad432007-05-04 07:25:19 +0000844 }
Benny Prijonoc71ad432007-05-04 07:25:19 +0000845
846 if (reschedule)
847 Run();
Benny Prijonoba5926a2007-05-02 11:29:37 +0000848}
849
Benny Prijono70c5ba02007-12-31 11:27:35 +0000850#if 0
851// IP networking related testing
852static pj_status_t test_addr(void)
853{
854 int af;
855 unsigned i, count;
856 pj_addrinfo ai[8];
857 pj_sockaddr ifs[8];
858 const pj_str_t *hostname;
859 pj_hostent he;
860 pj_status_t status;
861
862 pj_log_set_log_func(&log_writer);
863
864 status = pj_init();
865 if (status != PJ_SUCCESS) {
866 pjsua_perror(THIS_FILE, "pj_init() error", status);
867 return status;
868 }
869
870 af = pj_AF_INET();
871
Benny Prijono684c0ad2008-01-03 18:50:27 +0000872#if 0
873 pj_in_addr in_addr;
874 pj_str_t aa = pj_str("1.1.1.1");
875 in_addr = pj_inet_addr(&aa);
876 char *the_addr = pj_inet_ntoa(in_addr);
877 PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr));
878
879 aa = pj_str("192.168.0.15");
880 in_addr = pj_inet_addr(&aa);
881 the_addr = pj_inet_ntoa(in_addr);
882 PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr));
883
884 aa = pj_str("2.2.2.2");
885 in_addr = pj_inet_addr(&aa);
886 the_addr = pj_inet_ntoa(in_addr);
887 PJ_LOG(3,(THIS_FILE, "IP addr=%s", the_addr));
888
889 return -1;
890#endif
891
Benny Prijono70c5ba02007-12-31 11:27:35 +0000892 // Hostname
893 hostname = pj_gethostname();
894 if (hostname == NULL) {
895 status = PJ_ERESOLVE;
896 pjsua_perror(THIS_FILE, "pj_gethostname() error", status);
897 goto on_return;
898 }
899
900 PJ_LOG(3,(THIS_FILE, "Hostname: %.*s", hostname->slen, hostname->ptr));
901
902 // Gethostbyname
903 status = pj_gethostbyname(hostname, &he);
904 if (status != PJ_SUCCESS) {
905 pjsua_perror(THIS_FILE, "pj_gethostbyname() error", status);
906 } else {
907 PJ_LOG(3,(THIS_FILE, "gethostbyname: %s",
908 pj_inet_ntoa(*(pj_in_addr*)he.h_addr)));
909 }
910
911 // Getaddrinfo
912 count = PJ_ARRAY_SIZE(ai);
913 status = pj_getaddrinfo(af, hostname, &count, ai);
914 if (status != PJ_SUCCESS) {
915 pjsua_perror(THIS_FILE, "pj_getaddrinfo() error", status);
916 } else {
917 for (i=0; i<count; ++i) {
918 char ipaddr[PJ_INET6_ADDRSTRLEN+2];
919 PJ_LOG(3,(THIS_FILE, "Addrinfo: %s",
920 pj_sockaddr_print(&ai[i].ai_addr, ipaddr, sizeof(ipaddr), 2)));
921 }
922 }
923
924 // Enum interface
925 count = PJ_ARRAY_SIZE(ifs);
926 status = pj_enum_ip_interface(af, &count, ifs);
927 if (status != PJ_SUCCESS) {
928 pjsua_perror(THIS_FILE, "pj_enum_ip_interface() error", status);
929 } else {
930 for (i=0; i<count; ++i) {
931 char ipaddr[PJ_INET6_ADDRSTRLEN+2];
932 PJ_LOG(3,(THIS_FILE, "Interface: %s",
933 pj_sockaddr_print(&ifs[i], ipaddr, sizeof(ipaddr), 2)));
934 }
935 }
936
937 // Get default iinterface
938 status = pj_getdefaultipinterface(af, &ifs[0]);
939 if (status != PJ_SUCCESS) {
940 pjsua_perror(THIS_FILE, "pj_getdefaultipinterface() error", status);
941 } else {
942 char ipaddr[PJ_INET6_ADDRSTRLEN+2];
943 PJ_LOG(3,(THIS_FILE, "Default IP: %s",
944 pj_sockaddr_print(&ifs[0], ipaddr, sizeof(ipaddr), 2)));
945 }
946
947 // Get default IP address
948 status = pj_gethostip(af, &ifs[0]);
949 if (status != PJ_SUCCESS) {
950 pjsua_perror(THIS_FILE, "pj_gethostip() error", status);
951 } else {
952 char ipaddr[PJ_INET6_ADDRSTRLEN+2];
953 PJ_LOG(3,(THIS_FILE, "Host IP: %s",
954 pj_sockaddr_print(&ifs[0], ipaddr, sizeof(ipaddr), 2)));
955 }
956
957 status = -1;
958
959on_return:
960 pj_shutdown();
961 return status;
962}
963#endif
964
965
Benny Prijono6b3ccdf2008-01-14 11:54:21 +0000966#include <es_sock.h>
Benny Prijono897f9f82007-05-03 19:56:21 +0000967
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000968#if 0
969// Force network connection to use the first IAP,
970// this is useful for debugging on emulator without GUI.
971// Include commdb.lib & apengine.lib in symbian_ua.mmp file
972// if this is enabled.
973
974#include <apdatahandler.h>
975
976inline void ForceUseFirstIAP()
977{
978 TUint32 rank = 1;
979 TUint32 bearers;
980 TUint32 prompt;
981 TUint32 iap;
982
983 CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
984 CleanupStack::PushL(commDb);
985
986 CApDataHandler* apDataHandler = CApDataHandler::NewLC(*commDb);
987
988 TCommDbConnectionDirection direction = ECommDbConnectionDirectionOutgoing;
989 apDataHandler->GetPreferredIfDbIapTypeL(rank, direction, bearers, prompt, iap);
990 prompt = ECommDbDialogPrefDoNotPrompt;
991 apDataHandler->SetPreferredIfDbIapTypeL(rank, direction, bearers, (TCommDbDialogPref)prompt, iap, ETrue);
992 CleanupStack::PopAndDestroy(2); // apDataHandler, commDb
993}
994
995static void SelectIAP()
996{
997 ForceUseFirstIAP();
998}
999
1000#else
1001
1002static void SelectIAP()
1003{
1004}
1005
1006#endif
1007
1008
Nanang Izzuddin90b83202009-03-02 15:48:45 +00001009// Class CConnMon to monitor network connection (RConnection). Whenever
1010// the connection is down, it will notify PJLIB and restart PJSUA-LIB.
1011class CConnMon : public CActive {
1012public:
1013 static CConnMon* NewL(RConnection &conn, RSocketServ &sserver) {
1014 CConnMon *self = new (ELeave) CConnMon(conn, sserver);
1015 CleanupStack::PushL(self);
1016 self->ConstructL();
1017 CleanupStack::Pop(self);
1018 return self;
1019 }
1020
1021 void Start() {
1022 conn_.ProgressNotification(nif_progress_, iStatus);
1023 SetActive();
1024 }
1025
1026 void Stop() {
1027 Cancel();
1028 }
1029
1030 ~CConnMon() { Stop(); }
1031
1032private:
1033 CConnMon(RConnection &conn, RSocketServ &sserver) :
1034 CActive(EPriorityHigh),
1035 conn_(conn),
1036 sserver_(sserver)
1037 {
1038 CActiveScheduler::Add(this);
1039 }
1040
1041 void ConstructL() {}
1042
1043 void DoCancel() {
1044 conn_.CancelProgressNotification();
1045 }
1046
1047 void RunL() {
1048 int stage = nif_progress_().iStage;
1049
1050 if (stage == KLinkLayerClosed) {
1051 pj_status_t status;
1052 TInt err;
1053
1054 // Tell pjlib that connection is down.
1055 pj_symbianos_set_connection_status(PJ_FALSE);
1056
1057 PJ_LOG(3, (THIS_FILE, "RConnection closed, restarting PJSUA.."));
1058
1059 // Destroy pjsua
1060 pjsua_destroy();
1061 PJ_LOG(3, (THIS_FILE, "PJSUA destroyed."));
1062
1063 // Reopen the connection
1064 err = conn_.Open(sserver_);
1065 if (err == KErrNone)
1066 err = conn_.Start();
1067 if (err != KErrNone) {
1068 CActiveScheduler::Stop();
1069 return;
1070 }
1071
1072 // Reinit Symbian OS param before pj_init()
1073 pj_symbianos_params sym_params;
1074 pj_bzero(&sym_params, sizeof(sym_params));
1075 sym_params.rsocketserv = &sserver_;
1076 sym_params.rconnection = &conn_;
1077 pj_symbianos_set_params(&sym_params);
1078
1079 // Reinit pjsua
1080 status = app_startup();
1081 if (status != PJ_SUCCESS) {
1082 pjsua_perror(THIS_FILE, "app_startup() error", status);
1083 CActiveScheduler::Stop();
1084 return;
1085 }
1086
1087 PJ_LOG(3, (THIS_FILE, "PJSUA restarted."));
Benny Prijono9920dc32009-03-12 18:11:37 +00001088 PrintMainMenu();
Nanang Izzuddin90b83202009-03-02 15:48:45 +00001089 }
1090
1091 Start();
1092 }
1093
1094 RConnection& conn_;
1095 RSocketServ& sserver_;
1096 TNifProgressBuf nif_progress_;
1097};
1098
Benny Prijonoba5926a2007-05-02 11:29:37 +00001099////////////////////////////////////////////////////////////////////////////
1100int ua_main()
1101{
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001102 RSocketServ aSocketServer;
1103 RConnection aConn;
1104 TInt err;
1105 pj_symbianos_params sym_params;
Benny Prijonoc71ad432007-05-04 07:25:19 +00001106 pj_status_t status;
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001107
1108 SelectIAP();
Benny Prijonoc71ad432007-05-04 07:25:19 +00001109
Benny Prijono70c5ba02007-12-31 11:27:35 +00001110 // Initialize RSocketServ
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +00001111 if ((err=aSocketServer.Connect(32)) != KErrNone)
Benny Prijono70c5ba02007-12-31 11:27:35 +00001112 return PJ_STATUS_FROM_OS(err);
1113
1114 // Open up a connection
1115 if ((err=aConn.Open(aSocketServer)) != KErrNone) {
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001116 aSocketServer.Close();
1117 return PJ_STATUS_FROM_OS(err);
Benny Prijono70c5ba02007-12-31 11:27:35 +00001118 }
1119
1120 if ((err=aConn.Start()) != KErrNone) {
1121 aSocketServer.Close();
1122 return PJ_STATUS_FROM_OS(err);
1123 }
1124
1125 // Set Symbian OS parameters in pjlib.
1126 // This must be done before pj_init() is called.
1127 pj_bzero(&sym_params, sizeof(sym_params));
1128 sym_params.rsocketserv = &aSocketServer;
1129 sym_params.rconnection = &aConn;
1130 pj_symbianos_set_params(&sym_params);
1131
Benny Prijonoc71ad432007-05-04 07:25:19 +00001132 // Initialize pjsua
1133 status = app_startup();
Benny Prijono70c5ba02007-12-31 11:27:35 +00001134 //status = test_addr();
1135 if (status != PJ_SUCCESS) {
1136 aConn.Close();
1137 aSocketServer.Close();
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001138 return status;
Benny Prijono70c5ba02007-12-31 11:27:35 +00001139 }
Benny Prijono897f9f82007-05-03 19:56:21 +00001140
Nanang Izzuddin90b83202009-03-02 15:48:45 +00001141
Benny Prijonoc71ad432007-05-04 07:25:19 +00001142 // Run the UI
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001143 ConsoleUI *con = new ConsoleUI(console);
Benny Prijonoc71ad432007-05-04 07:25:19 +00001144
1145 con->Run();
Nanang Izzuddin798bf452009-02-12 12:35:27 +00001146 PrintMainMenu();
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001147
Nanang Izzuddin90b83202009-03-02 15:48:45 +00001148 // Init & start connection monitor
1149 CConnMon *connmon = CConnMon::NewL(aConn, aSocketServer);
1150 connmon->Start();
1151
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001152 CActiveScheduler::Start();
Benny Prijonoc71ad432007-05-04 07:25:19 +00001153
Nanang Izzuddin90b83202009-03-02 15:48:45 +00001154 delete connmon;
Benny Prijonoc71ad432007-05-04 07:25:19 +00001155 delete con;
Benny Prijono684c0ad2008-01-03 18:50:27 +00001156
1157 // Dump memory statistics
1158 PJ_LOG(3,(THIS_FILE, "Max heap usage: %u.%03uMB",
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001159 pjsua_var.cp.peak_used_size / 1000000,
1160 (pjsua_var.cp.peak_used_size % 1000000)/1000));
Benny Prijonoc71ad432007-05-04 07:25:19 +00001161
Benny Prijono684c0ad2008-01-03 18:50:27 +00001162 // check max stack usage
1163#if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0
1164 pj_thread_t* this_thread = pj_thread_this();
1165 if (!this_thread)
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001166 return status;
Benny Prijono684c0ad2008-01-03 18:50:27 +00001167
1168 const char* max_stack_file;
1169 int max_stack_line;
1170 status = pj_thread_get_stack_info(this_thread, &max_stack_file, &max_stack_line);
1171
1172 PJ_LOG(3,(THIS_FILE, "Max stack usage: %u at %s:%d",
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001173 pj_thread_get_stack_max_usage(this_thread),
1174 max_stack_file, max_stack_line));
Benny Prijono684c0ad2008-01-03 18:50:27 +00001175#endif
1176
Benny Prijonoc71ad432007-05-04 07:25:19 +00001177 // Shutdown pjsua
1178 pjsua_destroy();
1179
Benny Prijono70c5ba02007-12-31 11:27:35 +00001180 // Close connection and socket server
1181 aConn.Close();
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001182 aSocketServer.Close();
Nanang Izzuddina940b362009-02-23 13:53:30 +00001183
Benny Prijono797e3382007-12-01 09:10:07 +00001184 return status;
Benny Prijonoba5926a2007-05-02 11:29:37 +00001185}
1186