blob: 877354998b27ec7ccd701c404df682c0df082e3f [file] [log] [blame]
Benny Prijonoba5926a2007-05-02 11:29:37 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/**
21 * simple_pjsua.c
22 *
23 * This is a very simple but fully featured SIP user agent, with the
24 * following capabilities:
25 * - SIP registration
26 * - Making and receiving call
27 * - Audio/media to sound device.
28 *
29 * Usage:
30 * - To make outgoing call, start simple_pjsua with the URL of remote
31 * destination to contact.
32 * E.g.:
33 * simpleua sip:user@remote
34 *
35 * - Incoming calls will automatically be answered with 200.
36 *
37 * This program will quit once it has completed a single call.
38 */
39
40#include <pjsua-lib/pjsua.h>
41#include "ua.h"
42
43#define THIS_FILE "symbian_ua.cpp"
44
45#define SIP_DOMAIN "colinux"
46#define SIP_USER "bulukucing"
47#define SIP_PASSWD "netura"
48
49
50/* Callback called by the library upon receiving incoming call */
51static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
52 pjsip_rx_data *rdata)
53{
54 pjsua_call_info ci;
55
56 PJ_UNUSED_ARG(acc_id);
57 PJ_UNUSED_ARG(rdata);
58
59 pjsua_call_get_info(call_id, &ci);
60
61 PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
62 (int)ci.remote_info.slen,
63 ci.remote_info.ptr));
64
65 /* Automatically answer incoming calls with 200/OK */
66 pjsua_call_answer(call_id, 200, NULL, NULL);
67}
68
69/* Callback called by the library when call's state has changed */
70static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
71{
72 pjsua_call_info ci;
73
74 PJ_UNUSED_ARG(e);
75
76 pjsua_call_get_info(call_id, &ci);
77 PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
78 (int)ci.state_text.slen,
79 ci.state_text.ptr));
80}
81
82/* Callback called by the library when call's media state has changed */
83static void on_call_media_state(pjsua_call_id call_id)
84{
85 pjsua_call_info ci;
86
87 pjsua_call_get_info(call_id, &ci);
88
89 if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
90 // When media is active, connect call to sound device.
91 pjsua_conf_connect(ci.conf_slot, 0);
92 pjsua_conf_connect(0, ci.conf_slot);
93 }
94}
95
96
97/* Logging callback */
98static void log_writer(int level, const char *buf, unsigned len)
99{
100 wchar_t buf16[PJ_LOG_MAX_SIZE];
101
102 PJ_UNUSED_ARG(level);
103
104 pj_ansi_to_unicode(buf, len, buf16, PJ_ARRAY_SIZE(buf16));
105
106 TPtrC16 aBuf((const TUint16*)buf16, (TInt)len);
107 console->Write(aBuf);
108}
109
110/*
111 * app_startup()
112 *
113 * url may contain URL to call.
114 */
115static pj_status_t app_startup(char *url)
116{
117 pjsua_acc_id acc_id;
118 pj_status_t status;
119
120 /* Redirect log before pjsua_init() */
121 pj_log_set_log_func((void (*)(int,const char*,int)) &log_writer);
122
123 /* Create pjsua first! */
124 status = pjsua_create();
125 if (status != PJ_SUCCESS) {
126 pjsua_perror(THIS_FILE, "pjsua_create() error", status);
127 return status;
128 }
129
130 /* If argument is specified, it's got to be a valid SIP URL */
131 if (url) {
132 status = pjsua_verify_sip_url(url);
133 if (status != PJ_SUCCESS) {
134 pjsua_perror(THIS_FILE, "Invalid URL", status);
135 return status;
136 }
137 }
138
139 /* Init pjsua */
140 {
141 pjsua_config cfg;
142 pjsua_logging_config log_cfg;
143 pjsua_media_config med_cfg;
144
145 pjsua_config_default(&cfg);
146 cfg.thread_cnt = 0; // Disable threading on Symbian
147 cfg.cb.on_incoming_call = &on_incoming_call;
148 cfg.cb.on_call_media_state = &on_call_media_state;
149 cfg.cb.on_call_state = &on_call_state;
150
151 pjsua_logging_config_default(&log_cfg);
152 log_cfg.console_level = 4;
153 log_cfg.cb = &log_writer;
154
155 pjsua_media_config_default(&med_cfg);
156 med_cfg.thread_cnt = 0; // Disable threading on Symbian
157 med_cfg.has_ioqueue = PJ_FALSE;
158 med_cfg.ec_tail_len = 0;
159
160 status = pjsua_init(&cfg, &log_cfg, &med_cfg);
161 if (status != PJ_SUCCESS) {
162 pjsua_perror(THIS_FILE, "pjsua_init() error", status);
163 pjsua_destroy();
164 return status;
165 }
166 }
167
168 /* Add UDP transport. */
169 {
170 pjsua_transport_config cfg;
171
172 pjsua_transport_config_default(&cfg);
173 cfg.port = 5060;
174 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
175 if (status != PJ_SUCCESS) {
176 pjsua_perror(THIS_FILE, "Error creating transport", status);
177 pjsua_destroy();
178 return status;
179 }
180 }
181
182 /* Initialization is done, now start pjsua */
183 status = pjsua_start();
184 if (status != PJ_SUCCESS) {
185 pjsua_perror(THIS_FILE, "Error starting pjsua", status);
186 pjsua_destroy();
187 return status;
188 }
189
190 /* Register to SIP server by creating SIP account. */
191 {
192 pjsua_acc_config cfg;
193
194 pjsua_acc_config_default(&cfg);
195 cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
196 cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
197 cfg.cred_count = 1;
198 cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);
199 cfg.cred_info[0].scheme = pj_str("digest");
200 cfg.cred_info[0].username = pj_str(SIP_USER);
201 cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
202 cfg.cred_info[0].data = pj_str(SIP_PASSWD);
203
204 status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
205 if (status != PJ_SUCCESS) {
206 pjsua_perror(THIS_FILE, "Error adding account", status);
207 pjsua_destroy();
208 return status;
209 }
210 }
211
212 /* If URL is specified, make call to the URL. */
213 if (url != NULL) {
214 pj_str_t uri = pj_str(url);
215 status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);
216 if (status != PJ_SUCCESS) {
217 pjsua_perror(THIS_FILE, "Error making call", status);
218 pjsua_destroy();
219 return status;
220 }
221
222 }
223
224 return PJ_SUCCESS;
225}
226
227
228////////////////////////////////////////////////////////////////////////////
229#include <e32base.h>
230
231class ConsoleUI : public CActive
232{
233public:
234 ConsoleUI(CActiveSchedulerWait *asw, CConsoleBase *con);
235
236 // Run console UI
237 void Run();
238
239 // Stop
240 void Stop();
241
242protected:
243 // Cancel asynchronous read.
244 void DoCancel();
245
246 // Implementation: called when read has completed.
247 void RunL();
248
249private:
250 CActiveSchedulerWait *asw_;
251 CConsoleBase *con_;
252};
253
254
255ConsoleUI::ConsoleUI(CActiveSchedulerWait *asw, CConsoleBase *con)
256: CActive(EPriorityStandard), asw_(asw), con_(con)
257{
258 CActiveScheduler::Add(this);
259}
260
261// Run console UI
262void ConsoleUI::Run()
263{
264 con_->Read(iStatus);
265 SetActive();
266}
267
268// Stop console UI
269void ConsoleUI::Stop()
270{
271 DoCancel();
272}
273
274// Cancel asynchronous read.
275void ConsoleUI::DoCancel()
276{
277 con_->ReadCancel();
278}
279
280// Implementation: called when read has completed.
281void ConsoleUI::RunL()
282{
283 TKeyCode kc = con_->KeyCode();
284
285 switch (kc) {
286 case 'q':
287 asw_->AsyncStop();
288 break;
289 default:
290 PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed",
291 kc, kc));
292 Run();
293 break;
294 }
295}
296
297////////////////////////////////////////////////////////////////////////////
298int ua_main()
299{
300 pj_status_t status;
301
302 // Initialize pjsua
303 status = app_startup(NULL);
304 if (status != PJ_SUCCESS)
305 return status;
306
307
308 // Run the UI
309 CActiveSchedulerWait *asw = new CActiveSchedulerWait;
310 ConsoleUI *con = new ConsoleUI(asw, console);
311
312 con->Run();
313 asw->Start();
314
315 delete con;
316 delete asw;
317
318 // Shutdown pjsua
319 pjsua_destroy();
320
321 return 0;
322}
323