1
/* $Id: ua.cpp 1793 2008-02-14 13:39:24Z bennylp $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjsua-lib/pjsua.h>
21
#include <pjsua-lib/pjsua_internal.h>
23
#include "symbian_ua.h"
25
#define THIS_FILE "symbian_ua.cpp"
30
#define USE_SRTP PJSUA_DEFAULT_USE_SRTP
32
static RSocketServ aSocketServer;
33
static RConnection aConn;
35
static pjsua_acc_id g_acc_id = PJSUA_INVALID_ID;
36
static pjsua_call_id g_call_id = PJSUA_INVALID_ID;
37
static pjsua_buddy_id g_buddy_id = PJSUA_INVALID_ID;
39
static symbian_ua_info_cb_t g_cb = {NULL, NULL, NULL, NULL, NULL};
41
static void log_writer(int level, const char *buf, int len)
43
static wchar_t buf16[PJ_LOG_MAX_SIZE];
50
pj_ansi_to_unicode(buf, len, buf16, PJ_ARRAY_SIZE(buf16));
54
static void on_reg_state(pjsua_acc_id acc_id)
56
pjsua_acc_info acc_info;
59
status = pjsua_acc_get_info(acc_id, &acc_info);
60
if (status != PJ_SUCCESS)
63
if (acc_info.status == 200) {
64
if (acc_info.expires) {
65
PJ_LOG(3,(THIS_FILE, "Registration success!"));
66
if (g_cb.on_reg_state) g_cb.on_reg_state(true);
68
PJ_LOG(3,(THIS_FILE, "Unregistration success!"));
69
if (g_cb.on_unreg_state) g_cb.on_unreg_state(true);
72
if (acc_info.expires) {
73
PJ_LOG(3,(THIS_FILE, "Registration failed!"));
74
if (g_cb.on_reg_state) g_cb.on_reg_state(false);
76
PJ_LOG(3,(THIS_FILE, "Unregistration failed!"));
77
if (g_cb.on_unreg_state) g_cb.on_unreg_state(false);
82
/* Callback called by the library upon receiving incoming call */
83
static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
88
PJ_UNUSED_ARG(acc_id);
91
if (g_call_id != PJSUA_INVALID_ID) {
92
pjsua_call_answer(call_id, PJSIP_SC_BUSY_HERE, NULL, NULL);
96
pjsua_call_get_info(call_id, &ci);
98
PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
99
(int)ci.remote_info.slen,
100
ci.remote_info.ptr));
104
/* Automatically answer incoming calls with 180/Ringing */
105
pjsua_call_answer(call_id, 180, NULL, NULL);
107
if (g_cb.on_incoming_call) {
108
static wchar_t disp[256];
109
static wchar_t uri[PJSIP_MAX_URL_SIZE];
111
pj_ansi_to_unicode(ci.remote_info.ptr, ci.remote_info.slen,
112
disp, PJ_ARRAY_SIZE(disp));
113
pj_ansi_to_unicode(ci.remote_contact.ptr, ci.remote_contact.slen,
114
uri, PJ_ARRAY_SIZE(uri));
116
g_cb.on_incoming_call(disp, uri);
120
/* Callback called by the library when call's state has changed */
121
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
127
pjsua_call_get_info(call_id, &ci);
129
if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
130
if (call_id == g_call_id)
131
g_call_id = PJSUA_INVALID_ID;
132
if (g_cb.on_call_end) {
133
static wchar_t reason[256];
134
pj_ansi_to_unicode(ci.last_status_text.ptr, ci.last_status_text.slen,
135
reason, PJ_ARRAY_SIZE(reason));
136
g_cb.on_call_end(reason);
139
} else if (ci.state != PJSIP_INV_STATE_INCOMING) {
140
if (g_call_id == PJSUA_INVALID_ID)
144
PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
145
(int)ci.state_text.slen,
149
/* Callback called by the library when call's media state has changed */
150
static void on_call_media_state(pjsua_call_id call_id)
154
pjsua_call_get_info(call_id, &ci);
156
if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
157
// When media is active, connect call to sound device.
158
pjsua_conf_connect(ci.conf_slot, 0);
159
pjsua_conf_connect(0, ci.conf_slot);
164
/* Handler on buddy state changed. */
165
static void on_buddy_state(pjsua_buddy_id buddy_id)
167
pjsua_buddy_info info;
168
pjsua_buddy_get_info(buddy_id, &info);
170
PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
173
(int)info.status_text.slen,
174
info.status_text.ptr));
178
/* Incoming IM message (i.e. MESSAGE request)! */
179
static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
180
const pj_str_t *to, const pj_str_t *contact,
181
const pj_str_t *mime_type, const pj_str_t *text)
183
/* Note: call index may be -1 */
184
PJ_UNUSED_ARG(call_id);
186
PJ_UNUSED_ARG(contact);
187
PJ_UNUSED_ARG(mime_type);
189
PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s",
190
(int)from->slen, from->ptr,
191
(int)text->slen, text->ptr));
195
/* Received typing indication */
196
static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
197
const pj_str_t *to, const pj_str_t *contact,
200
PJ_UNUSED_ARG(call_id);
202
PJ_UNUSED_ARG(contact);
204
PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
205
(int)from->slen, from->ptr,
206
(is_typing?"is typing..":"has stopped typing")));
210
/* Call transfer request status. */
211
static void on_call_transfer_status(pjsua_call_id call_id,
213
const pj_str_t *status_text,
217
PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
218
call_id, status_code,
219
(int)status_text->slen, status_text->ptr,
220
(final ? "[final]" : "")));
222
if (status_code/100 == 2) {
224
"Call %d: call transfered successfully, disconnecting call",
226
pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
232
/* NAT detection result */
233
static void on_nat_detect(const pj_stun_nat_detect_result *res)
235
if (res->status != PJ_SUCCESS) {
236
pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
238
PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
242
/* Notification that call is being replaced. */
243
static void on_call_replaced(pjsua_call_id old_call_id,
244
pjsua_call_id new_call_id)
246
pjsua_call_info old_ci, new_ci;
248
pjsua_call_get_info(old_call_id, &old_ci);
249
pjsua_call_get_info(new_call_id, &new_ci);
251
PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
254
(int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
256
(int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
259
int symbian_ua_init()
262
pj_symbianos_params sym_params;
265
// Initialize RSocketServ
266
if ((err=aSocketServer.Connect(32)) != KErrNone)
267
return PJ_STATUS_FROM_OS(err);
269
// Open up a connection
270
if ((err=aConn.Open(aSocketServer)) != KErrNone) {
271
aSocketServer.Close();
272
return PJ_STATUS_FROM_OS(err);
275
if ((err=aConn.Start()) != KErrNone) {
277
aSocketServer.Close();
278
return PJ_STATUS_FROM_OS(err);
281
// Set Symbian OS parameters in pjlib.
282
// This must be done before pj_init() is called.
283
pj_bzero(&sym_params, sizeof(sym_params));
284
sym_params.rsocketserv = &aSocketServer;
285
sym_params.rconnection = &aConn;
286
pj_symbianos_set_params(&sym_params);
288
/* Redirect log before pjsua_init() */
289
pj_log_set_log_func(&log_writer);
292
pj_log_set_level(LOG_LEVEL);
294
/* Create pjsua first! */
295
status = pjsua_create();
296
if (status != PJ_SUCCESS) {
297
pjsua_perror(THIS_FILE, "pjsua_create() error", status);
304
pjsua_config_default(&cfg);
306
cfg.thread_cnt = 0; // Disable threading on Symbian
307
cfg.use_srtp = USE_SRTP;
308
cfg.srtp_secure_signaling = 0;
310
cfg.cb.on_incoming_call = &on_incoming_call;
311
cfg.cb.on_call_media_state = &on_call_media_state;
312
cfg.cb.on_call_state = &on_call_state;
313
cfg.cb.on_buddy_state = &on_buddy_state;
314
cfg.cb.on_pager = &on_pager;
315
cfg.cb.on_typing = &on_typing;
316
cfg.cb.on_call_transfer_status = &on_call_transfer_status;
317
cfg.cb.on_call_replaced = &on_call_replaced;
318
cfg.cb.on_nat_detect = &on_nat_detect;
319
cfg.cb.on_reg_state = &on_reg_state;
321
pjsua_media_config med_cfg;
323
pjsua_media_config_default(&med_cfg);
324
med_cfg.thread_cnt = 0; // Disable threading on Symbian
325
med_cfg.has_ioqueue = PJ_FALSE;
326
med_cfg.clock_rate = 8000;
327
#if defined(PJMEDIA_SYM_SND_USE_APS) && (PJMEDIA_SYM_SND_USE_APS==1)
328
med_cfg.audio_frame_ptime = 20;
330
med_cfg.audio_frame_ptime = 40;
332
med_cfg.ec_tail_len = 0;
333
med_cfg.enable_ice = USE_ICE;
334
med_cfg.snd_auto_close_time = 5; // wait for 5 seconds idle before sound dev get auto-closed
336
pjsua_logging_config log_cfg;
338
pjsua_logging_config_default(&log_cfg);
339
log_cfg.console_level = LOG_LEVEL;
340
log_cfg.cb = &log_writer;
343
status = pjsua_init(&cfg, &log_cfg, &med_cfg);
344
if (status != PJ_SUCCESS) {
345
pjsua_perror(THIS_FILE, "pjsua_init() error", status);
350
/* Add UDP transport. */
351
pjsua_transport_config tcfg;
352
pjsua_transport_id tid;
354
pjsua_transport_config_default(&tcfg);
355
tcfg.port = SIP_PORT;
356
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tcfg, &tid);
357
if (status != PJ_SUCCESS) {
358
pjsua_perror(THIS_FILE, "Error creating transport", status);
363
/* Add account for the transport */
364
pjsua_acc_add_local(tid, PJ_TRUE, &g_acc_id);
366
/* Initialization is done, now start pjsua */
367
status = pjsua_start();
368
if (status != PJ_SUCCESS) {
369
pjsua_perror(THIS_FILE, "Error starting pjsua", status);
374
/* Adjust Speex priority and enable only the narrowband */
376
pj_str_t codec_id = pj_str("speex/8000");
377
pjmedia_codec_mgr_set_codec_priority(
378
pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
379
&codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
381
codec_id = pj_str("speex/16000");
382
pjmedia_codec_mgr_set_codec_priority(
383
pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
384
&codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
386
codec_id = pj_str("speex/32000");
387
pjmedia_codec_mgr_set_codec_priority(
388
pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
389
&codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
396
int symbian_ua_destroy()
401
// Close connection and socket server
403
aSocketServer.Close();
410
void symbian_ua_set_info_callback(const symbian_ua_info_cb_t *cb)
415
pj_bzero(&g_cb, sizeof(g_cb));
418
int symbian_ua_set_account(const char *domain, const char *username,
419
const char *password,
420
bool use_srtp, bool use_ice)
424
PJ_ASSERT_RETURN(username && password && domain, PJ_EINVAL);
425
PJ_UNUSED_ARG(use_srtp);
426
PJ_UNUSED_ARG(use_ice);
428
if (domain[0] == 0) {
429
pjsua_acc_info acc_info;
432
status = pjsua_acc_get_info(g_acc_id, &acc_info);
433
if (status != PJ_SUCCESS)
436
if (acc_info.status == 200) {
437
PJ_LOG(3,(THIS_FILE, "Unregistering.."));
438
pjsua_acc_set_registration(g_acc_id, PJ_FALSE);
444
if (pjsua_acc_get_count() > 1) {
445
status = pjsua_acc_del(g_acc_id);
446
if (status != PJ_SUCCESS) {
447
pjsua_perror(THIS_FILE, "Error removing account", status);
453
pjsua_acc_config cfg;
454
char tmp_id[PJSIP_MAX_URL_SIZE];
455
char tmp_reg_uri[PJSIP_MAX_URL_SIZE];
457
if (!pj_ansi_strnicmp(domain, "sip:", 4)) {
461
pjsua_acc_config_default(&cfg);
462
pj_ansi_sprintf(tmp_id, "sip:%s@%s", username, domain);
463
cfg.id = pj_str(tmp_id);
464
pj_ansi_sprintf(tmp_reg_uri, "sip:%s", domain);
465
cfg.reg_uri = pj_str(tmp_reg_uri);
467
cfg.cred_info[0].realm = pj_str("*");
468
cfg.cred_info[0].scheme = pj_str("digest");
469
cfg.cred_info[0].username = pj_str((char*)username);
470
cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
471
cfg.cred_info[0].data = pj_str((char*)password);
473
status = pjsua_acc_add(&cfg, PJ_TRUE, &g_acc_id);
474
if (status != PJ_SUCCESS) {
475
pjsua_perror(THIS_FILE, "Error setting account", status);
483
int symbian_ua_makecall(const char* dest_url)
485
if (pjsua_verify_url(dest_url) == PJ_SUCCESS) {
486
pj_str_t dst = pj_str((char*)dest_url);
487
pjsua_call_make_call(g_acc_id, &dst, 0, NULL,
496
int symbian_ua_endcall()
498
pjsua_call_hangup_all();
503
bool symbian_ua_anycall()
505
return (pjsua_call_get_count()>0);
508
int symbian_ua_answercall()
510
PJ_ASSERT_RETURN (g_call_id != PJSUA_INVALID_ID, PJ_EINVAL);
512
return pjsua_call_answer(g_call_id, 200, NULL, NULL);