blob: 8be930f7186a050472439eb3b01c54f5da05b7cd [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +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 */
20#include <pjsua-lib/pjsua.h>
21
22
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000023#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000024#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000025
Benny Prijonoe909eac2006-07-27 22:04:56 +000026//#define STEREO_DEMO
Benny Prijono40860c32008-09-04 13:55:33 +000027//#define TRANSPORT_ADAPTER_SAMPLE
Benny Prijonoeebe9af2006-06-13 22:57:13 +000028
Benny Prijono4f966892008-06-14 22:43:56 +000029/* Ringtones US UK */
30#define RINGBACK_FREQ1 440 /* 400 */
31#define RINGBACK_FREQ2 480 /* 450 */
32#define RINGBACK_ON 2000 /* 400 */
33#define RINGBACK_OFF 4000 /* 200 */
34#define RINGBACK_CNT 1 /* 2 */
35#define RINGBACK_INTERVAL 4000 /* 2000 */
Benny Prijono91d20f42008-06-14 19:42:37 +000036
Benny Prijono4f966892008-06-14 22:43:56 +000037#define RING_FREQ1 800
38#define RING_FREQ2 640
39#define RING_ON 200
40#define RING_OFF 100
41#define RING_CNT 3
42#define RING_INTERVAL 3000
Benny Prijono91d20f42008-06-14 19:42:37 +000043
44
Benny Prijono804ff0a2006-09-14 11:17:48 +000045/* Call specific data */
46struct call_data
47{
48 pj_timer_entry timer;
Benny Prijono91d20f42008-06-14 19:42:37 +000049 pj_bool_t ringback_on;
50 pj_bool_t ring_on;
Benny Prijono804ff0a2006-09-14 11:17:48 +000051};
52
Benny Prijonoeebe9af2006-06-13 22:57:13 +000053
54/* Pjsua application data */
55static struct app_config
56{
57 pjsua_config cfg;
58 pjsua_logging_config log_cfg;
59 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000060 pj_bool_t no_refersub;
Benny Prijono8972bf02009-04-05 18:30:45 +000061 pj_bool_t ipv6;
Benny Prijonoe93e2872006-06-28 16:46:49 +000062 pj_bool_t no_tcp;
63 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000064 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000065 pjsua_transport_config udp_cfg;
66 pjsua_transport_config rtp_cfg;
Benny Prijono5e51a4e2008-11-27 00:06:46 +000067 pjsip_redirect_op redir_op;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000068
69 unsigned acc_cnt;
70 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
71
72 unsigned buddy_cnt;
73 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
74
Benny Prijono804ff0a2006-09-14 11:17:48 +000075 struct call_data call_data[PJSUA_MAX_CALLS];
76
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077 pj_pool_t *pool;
78 /* Compatibility with older pjsua */
79
80 unsigned codec_cnt;
81 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000082 unsigned codec_dis_cnt;
83 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000084 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000085 unsigned wav_count;
86 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000087 unsigned tone_count;
88 pjmedia_tone_desc tones[32];
89 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000090 pjsua_player_id wav_id;
91 pjsua_conf_port_id wav_port;
92 pj_bool_t auto_play;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +000093 pj_bool_t auto_play_hangup;
94 pj_timer_entry auto_hangup_timer;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000095 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000096 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000097 pj_str_t rec_file;
98 pj_bool_t auto_rec;
99 pjsua_recorder_id rec_id;
100 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000101 unsigned auto_answer;
102 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +0000103
104#ifdef STEREO_DEMO
105 pjmedia_snd_port *snd;
106#endif
107
Benny Prijono6dd967c2006-12-26 02:27:14 +0000108 float mic_level,
109 speaker_level;
110
Benny Prijono4e5d5512007-03-06 18:11:30 +0000111 int capture_dev, playback_dev;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000112 unsigned capture_lat, playback_lat;
Benny Prijono91d20f42008-06-14 19:42:37 +0000113
114 pj_bool_t no_tones;
115 int ringback_slot;
116 int ringback_cnt;
117 pjmedia_port *ringback_port;
118 int ring_slot;
119 int ring_cnt;
120 pjmedia_port *ring_port;
121
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000122} app_config;
123
124
Benny Prijono21b9ad92006-08-15 13:11:22 +0000125//static pjsua_acc_id current_acc;
126#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +0000127static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoebc32c32008-06-12 13:30:39 +0000128static pj_bool_t cmd_echo;
129static int stdout_refresh = -1;
130static const char *stdout_refresh_text = "STDOUT_REFRESH";
131static pj_bool_t stdout_refresh_quit = PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000132static pj_str_t uri_arg;
133
Nanang Izzuddin660eee82008-07-17 14:54:03 +0000134static char some_buf[1024 * 3];
Benny Prijono6eddd872008-03-21 13:46:08 +0000135
Benny Prijono594e4c52006-09-14 18:51:01 +0000136#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +0000137static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +0000138#endif
Benny Prijono40860c32008-09-04 13:55:33 +0000139#ifdef TRANSPORT_ADAPTER_SAMPLE
140static pj_status_t transport_adapter_sample(void);
141#endif
Benny Prijono8972bf02009-04-05 18:30:45 +0000142static pj_status_t create_ipv6_media_transports(void);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000143pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000144
Benny Prijono91d20f42008-06-14 19:42:37 +0000145static void ringback_start(pjsua_call_id call_id);
146static void ring_start(pjsua_call_id call_id);
147static void ring_stop(pjsua_call_id call_id);
148
149
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000150/*****************************************************************************
151 * Configuration manipulation
152 */
153
154/* Show usage */
155static void usage(void)
156{
157 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000158 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000159 puts ("");
160 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000161 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000162 puts (" --help Display this help screen");
163 puts (" --version Display version info");
164 puts ("");
165 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000166 puts (" --log-file=fname Log to filename (default stderr)");
167 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
168 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000169 puts (" --color Use colorful logging (default yes on Win32)");
170 puts (" --no-color Disable colorful logging");
Benny Prijonob7944862008-07-19 20:53:49 +0000171 puts (" --light-bg Use dark colors for light background (default is dark bg)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000172
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173 puts ("");
174 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000175 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000176#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000177 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
178 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000179#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000180 puts (" --registrar=url Set the URL of registrar server");
181 puts (" --id=url Set the URL of local ID (used in From header)");
182 puts (" --contact=url Optionally override the Contact information");
Benny Prijono30fe4852008-12-10 16:54:16 +0000183 puts (" --contact-params=S Append the specified parameters S in Contact URI");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000184 puts (" --proxy=url Optional URL of proxy server to visit");
185 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000186 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000187 puts (" --realm=string Set realm");
188 puts (" --username=string Set authentication username");
189 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000190 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000191 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000192 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
193 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000194 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000195 puts ("");
196 puts ("SIP Account Control:");
197 puts (" --next-account Add more account");
198 puts ("");
199 puts ("Transport Options:");
Benny Prijono8972bf02009-04-05 18:30:45 +0000200#if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
201 puts (" --ipv6 Use IPv6 instead for SIP and media.");
202#endif
Benny Prijonoe93e2872006-06-28 16:46:49 +0000203 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
204 puts (" TCP and UDP transports on the specified port, unless");
205 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000206 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
207 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijono8972bf02009-04-05 18:30:45 +0000208 puts (" --bound-addr=IP Bind transports to this IP interface");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000209 puts (" --no-tcp Disable TCP transport.");
210 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000211 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
212 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000213 puts (" --outbound=url Set the URL of global outbound proxy server");
214 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000215 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000216 puts ("");
217 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000218 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000219 puts (" --tls-ca-file Specify TLS CA file (default=none)");
220 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
221 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
222 puts (" --tls-password Specify TLS password to private key file (default=none)");
223 puts (" --tls-verify-server Verify server's certificate (default=no)");
224 puts (" --tls-verify-client Verify client's certificate (default=no)");
225 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
Benny Prijonoe10db842008-07-01 15:31:59 +0000226 puts (" --tls-srv-name Specify TLS server name for multi-hosting server (optional)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000227
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228 puts ("");
229 puts ("Media Options:");
230 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000231 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000232 puts (" --clock-rate=N Override conference bridge clock rate");
233 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000234 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000235 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000236 puts (" --play-file=file Register WAV file in conference bridge.");
237 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000238 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
239 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
240 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000241 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000242 puts (" --auto-play Automatically play the file (to incoming calls only)");
243 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000244 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000245 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
246 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000247 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000248 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000249 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000250 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000251 puts (" --ec-opt=OPT Select echo canceller algorithm (0=default, ");
252 puts (" 1=speex, 2=suppressor)");
Nanang Izzuddin11951c32009-03-12 23:04:21 +0000253 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 30)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000254 puts (" --capture-dev=id Audio capture device ID (default=-1)");
255 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000256 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000257 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000258 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
259 puts (" Specify N=-1 (default) to disable this feature.");
260 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000261 puts (" --no-tones Disable audible tones");
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000262 puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000263
Benny Prijonof76e1392008-06-06 14:51:48 +0000264 puts ("");
265 puts ("Media Transport Options:");
266 puts (" --use-ice Enable ICE (default:no)");
Benny Prijono329d6382009-05-29 13:04:03 +0000267 puts (" --ice-regular Use ICE regular nomination (default: aggressive)");
268 puts (" --ice-max-hosts=N Set maximum number of ICE host candidates");
Benny Prijono551af422008-08-07 09:55:52 +0000269 puts (" --ice-no-rtcp Disable RTCP component in ICE (default: no)");
Benny Prijonof76e1392008-06-06 14:51:48 +0000270 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
271 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
272 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
273 puts (" --use-turn Enable TURN relay with ICE (default:no)");
274 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
275 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
276 puts (" --turn-user TURN username");
277 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000278
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000279 puts ("");
280 puts ("Buddy List (can be more than one):");
281 puts (" --add-buddy url Add the specified URL to the buddy list.");
282 puts ("");
283 puts ("User Agent options:");
284 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
285 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000286 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000287 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000288 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000289 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono91d06b62008-09-20 12:16:56 +0000290 puts (" --no-force-lr Allow strict-route to be used (i.e. do not force lr)");
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000291 puts (" --accept-redirect=N Specify how to handle call redirect (3xx) response.");
292 puts (" 0: reject, 1: follow automatically (default), 2: ask");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000293
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000294 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000295 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
296 puts ("");
297
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000298 fflush(stdout);
299}
300
301
302/* Set default config. */
303static void default_config(struct app_config *cfg)
304{
Benny Prijono56315612006-07-18 14:39:40 +0000305 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000306 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000307
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000308 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000309 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000310 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
311
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000312 pjsua_logging_config_default(&cfg->log_cfg);
313 pjsua_media_config_default(&cfg->media_cfg);
314 pjsua_transport_config_default(&cfg->udp_cfg);
315 cfg->udp_cfg.port = 5060;
316 pjsua_transport_config_default(&cfg->rtp_cfg);
317 cfg->rtp_cfg.port = 4000;
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000318 cfg->redir_op = PJSIP_REDIRECT_ACCEPT;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000319 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000320 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000321 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000322 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000323 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000324 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000325 cfg->capture_dev = PJSUA_INVALID_ID;
326 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000327 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
328 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000329 cfg->ringback_slot = PJSUA_INVALID_ID;
330 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000331
332 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
333 pjsua_acc_config_default(&cfg->acc_cfg[i]);
334
335 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
336 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000337}
338
339
340/*
341 * Read command arguments from config file.
342 */
343static int read_config_file(pj_pool_t *pool, const char *filename,
344 int *app_argc, char ***app_argv)
345{
346 int i;
347 FILE *fhnd;
348 char line[200];
349 int argc = 0;
350 char **argv;
351 enum { MAX_ARGS = 64 };
352
353 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
354 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
355 argv[argc++] = *app_argv[0];
356
357 /* Open config file. */
358 fhnd = fopen(filename, "rt");
359 if (!fhnd) {
360 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
361 fflush(stdout);
362 return -1;
363 }
364
365 /* Scan tokens in the file. */
366 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000367 char *token;
368 char *p;
369 const char *whitespace = " \t\r\n";
370 char cDelimiter;
371 int len, token_len;
372
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000373 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000374
375 // Trim ending newlines
376 len = strlen(line);
377 if (line[len-1]=='\n')
378 line[--len] = '\0';
379 if (line[len-1]=='\r')
380 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381
Benny Prijonobf5b4692007-06-28 03:20:17 +0000382 if (len==0) continue;
383
384 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
385 // first, scan whitespaces
386 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
387
388 if (*p == '\0') // are we done yet?
389 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390
Benny Prijonobf5b4692007-06-28 03:20:17 +0000391 if (*p == '"' || *p == '\'') { // is token a quoted string
392 cDelimiter = *p++; // save quote delimiter
393 token = p;
394
395 while (*p != '\0' && *p != cDelimiter) p++;
396
397 if (*p == '\0') // found end of the line, but,
398 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000399
Benny Prijonobf5b4692007-06-28 03:20:17 +0000400 } else { // token's not a quoted string
401 token = p;
402
403 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
404
405 cDelimiter = *p;
406 }
407
408 *p = '\0';
409 token_len = p-token;
410
411 if (token_len > 0) {
412 if (*token == '#')
413 break; // ignore remainder of line
414
415 argv[argc] = pj_pool_alloc(pool, token_len + 1);
416 pj_memcpy(argv[argc], token, token_len + 1);
417 ++argc;
418 }
419
420 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000421 }
422 }
423
424 /* Copy arguments from command line */
425 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
426 argv[argc++] = (*app_argv)[i];
427
428 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
429 PJ_LOG(1,(THIS_FILE,
430 "Too many arguments specified in cmd line/config file"));
431 fflush(stdout);
432 fclose(fhnd);
433 return -1;
434 }
435
436 fclose(fhnd);
437
438 /* Assign the new command line back to the original command line. */
439 *app_argc = argc;
440 *app_argv = argv;
441 return 0;
442
443}
444
445static int my_atoi(const char *cs)
446{
447 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000448
449 pj_cstr(&s, cs);
450 if (cs[0] == '-') {
451 s.ptr++, s.slen--;
452 return 0 - (int)pj_strtoul(&s);
453 } else if (cs[0] == '+') {
454 s.ptr++, s.slen--;
455 return pj_strtoul(&s);
456 } else {
457 return pj_strtoul(&s);
458 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000459}
460
461
462/* Parse arguments. */
463static pj_status_t parse_args(int argc, char *argv[],
464 struct app_config *cfg,
465 pj_str_t *uri_to_call)
466{
467 int c;
468 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000469 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonob7944862008-07-19 20:53:49 +0000470 OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000471 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000472 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
473 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono8972bf02009-04-05 18:30:45 +0000474 OPT_BOUND_ADDR, OPT_CONTACT_PARAMS,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000475 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000476 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000477 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000478 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000479 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
Benny Prijono329d6382009-05-29 13:04:03 +0000480 OPT_USE_ICE, OPT_ICE_REGULAR, OPT_USE_SRTP, OPT_SRTP_SECURE,
481 OPT_USE_TURN, OPT_ICE_MAX_HOSTS, OPT_ICE_NO_RTCP, OPT_TURN_SRV,
Benny Prijono551af422008-08-07 09:55:52 +0000482 OPT_TURN_TCP, OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000483 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
484 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000485 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000486 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL, OPT_EC_OPT,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000487 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000488 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000489 OPT_NOREFERSUB, OPT_ACCEPT_REDIRECT,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000490 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
491 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000492 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000493 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000494 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
Benny Prijono8972bf02009-04-05 18:30:45 +0000495 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT, OPT_IPV6,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000496#ifdef _IONBF
497 OPT_STDOUT_NO_BUF,
498#endif
Benny Prijono91d06b62008-09-20 12:16:56 +0000499 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC,
500 OPT_NO_FORCE_LR
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000501 };
502 struct pj_getopt_option long_options[] = {
503 { "config-file",1, 0, OPT_CONFIG_FILE},
504 { "log-file", 1, 0, OPT_LOG_FILE},
505 { "log-level", 1, 0, OPT_LOG_LEVEL},
506 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
Benny Prijonod6e362a2008-07-19 17:53:47 +0000507 { "color", 0, 0, OPT_COLOR},
508 { "no-color", 0, 0, OPT_NO_COLOR},
Benny Prijonob7944862008-07-19 20:53:49 +0000509 { "light-bg", 0, 0, OPT_LIGHT_BG},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510 { "help", 0, 0, OPT_HELP},
511 { "version", 0, 0, OPT_VERSION},
512 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000513 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000514 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000515 { "null-audio", 0, 0, OPT_NULL_AUDIO},
516 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000517 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijono8972bf02009-04-05 18:30:45 +0000518 { "bound-addr", 1, 0, OPT_BOUND_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000519 { "no-tcp", 0, 0, OPT_NO_TCP},
520 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000521 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000522 { "proxy", 1, 0, OPT_PROXY},
523 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
524 { "registrar", 1, 0, OPT_REGISTRAR},
525 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000526 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000527 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000528 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 { "id", 1, 0, OPT_ID},
530 { "contact", 1, 0, OPT_CONTACT},
Benny Prijono30fe4852008-12-10 16:54:16 +0000531 { "contact-params",1,0, OPT_CONTACT_PARAMS},
Benny Prijonofce28542007-12-09 15:41:10 +0000532 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
533 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000534 { "accept-redirect", 1, 0, OPT_ACCEPT_REDIRECT},
Benny Prijono91d06b62008-09-20 12:16:56 +0000535 { "no-force-lr",0, 0, OPT_NO_FORCE_LR},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000536 { "realm", 1, 0, OPT_REALM},
537 { "username", 1, 0, OPT_USERNAME},
538 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000539 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000540 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000541 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
543 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
544 { "no-presence", 0, 0, OPT_NO_PRESENCE},
545 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000546 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000547 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000548 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
550 { "auto-conf", 0, 0, OPT_AUTO_CONF},
551 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000552 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000553 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000554 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000555
Benny Prijonoc97608e2007-03-23 16:34:20 +0000556 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijono329d6382009-05-29 13:04:03 +0000557 { "ice-regular",0, 0, OPT_ICE_REGULAR},
Benny Prijonof76e1392008-06-06 14:51:48 +0000558 { "use-turn", 0, 0, OPT_USE_TURN},
Benny Prijono329d6382009-05-29 13:04:03 +0000559 { "ice-max-hosts",1, 0, OPT_ICE_MAX_HOSTS},
Benny Prijono551af422008-08-07 09:55:52 +0000560 { "ice-no-rtcp",0, 0, OPT_ICE_NO_RTCP},
Benny Prijonof76e1392008-06-06 14:51:48 +0000561 { "turn-srv", 1, 0, OPT_TURN_SRV},
562 { "turn-tcp", 0, 0, OPT_TURN_TCP},
563 { "turn-user", 1, 0, OPT_TURN_USER},
564 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
565
Benny Prijonod8179652008-01-23 20:39:07 +0000566#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
567 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000568 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000569#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000570 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000571 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000572 { "complexity", 1, 0, OPT_COMPLEXITY},
573 { "quality", 1, 0, OPT_QUALITY},
574 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000575 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000576 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000577 { "ec-opt", 1, 0, OPT_EC_OPT},
Benny Prijono00cae612006-07-31 15:19:36 +0000578 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
579 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
580 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000581 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000582 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000583 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000584 { "duration", 1, 0, OPT_DURATION},
585 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000586 { "use-tls", 0, 0, OPT_USE_TLS},
587 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000588 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
589 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000590 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000591 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
592 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
593 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000594 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000595 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
596 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000597 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
598 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000599 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
600 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000601#ifdef _IONBF
602 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
603#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000604 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000605 { "no-tones", 0, 0, OPT_NO_TONES},
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000606 { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
Benny Prijono8972bf02009-04-05 18:30:45 +0000607#if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
608 { "ipv6", 0, 0, OPT_IPV6},
609#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000610 { NULL, 0, 0, 0}
611 };
612 pj_status_t status;
613 pjsua_acc_config *cur_acc;
614 char *config_file = NULL;
615 unsigned i;
616
617 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000618 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000619 while ((c=pj_getopt_long(argc, argv, "", long_options,
620 &option_index)) != -1)
621 {
622 switch (c) {
623 case OPT_CONFIG_FILE:
624 config_file = pj_optarg;
625 break;
626 }
627 if (config_file)
628 break;
629 }
630
631 if (config_file) {
632 status = read_config_file(app_config.pool, config_file, &argc, &argv);
633 if (status != 0)
634 return status;
635 }
636
637 cfg->acc_cnt = 0;
638 cur_acc = &cfg->acc_cfg[0];
639
640
641 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
642 * read from config file.
643 */
644 pj_optind = 0;
645 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000646 pj_str_t tmp;
647 long lval;
648
649 switch (c) {
650
Benny Prijono6f137482006-06-15 11:31:36 +0000651 case OPT_CONFIG_FILE:
652 /* Ignore as this has been processed before */
653 break;
654
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000655 case OPT_LOG_FILE:
656 cfg->log_cfg.log_filename = pj_str(pj_optarg);
657 break;
658
659 case OPT_LOG_LEVEL:
660 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
661 if (c < 0 || c > 6) {
662 PJ_LOG(1,(THIS_FILE,
663 "Error: expecting integer value 0-6 "
664 "for --log-level"));
665 return PJ_EINVAL;
666 }
667 cfg->log_cfg.level = c;
668 pj_log_set_level( c );
669 break;
670
671 case OPT_APP_LOG_LEVEL:
672 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
673 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
674 PJ_LOG(1,(THIS_FILE,
675 "Error: expecting integer value 0-6 "
676 "for --app-log-level"));
677 return PJ_EINVAL;
678 }
679 break;
680
Benny Prijonod6e362a2008-07-19 17:53:47 +0000681 case OPT_COLOR:
682 cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
683 break;
684
685 case OPT_NO_COLOR:
686 cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
687 break;
688
Benny Prijonob7944862008-07-19 20:53:49 +0000689 case OPT_LIGHT_BG:
690 pj_log_set_color(1, PJ_TERM_COLOR_R);
691 pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
692 pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
693 pj_log_set_color(4, 0);
694 pj_log_set_color(5, 0);
695 pj_log_set_color(77, 0);
696 break;
697
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000698 case OPT_HELP:
699 usage();
700 return PJ_EINVAL;
701
702 case OPT_VERSION: /* version */
703 pj_dump_config();
704 return PJ_EINVAL;
705
706 case OPT_NULL_AUDIO:
707 cfg->null_audio = PJ_TRUE;
708 break;
709
710 case OPT_CLOCK_RATE:
711 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000712 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000713 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000714 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000715 return PJ_EINVAL;
716 }
717 cfg->media_cfg.clock_rate = lval;
718 break;
719
Benny Prijonof3758ee2008-02-26 15:32:16 +0000720 case OPT_SND_CLOCK_RATE:
721 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000722 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000723 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000724 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000725 return PJ_EINVAL;
726 }
727 cfg->media_cfg.snd_clock_rate = lval;
728 break;
729
Benny Prijono7d60d052008-03-29 12:24:20 +0000730 case OPT_STEREO:
731 cfg->media_cfg.channel_count = 2;
732 break;
733
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000734 case OPT_LOCAL_PORT: /* local-port */
735 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000736 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000737 PJ_LOG(1,(THIS_FILE,
738 "Error: expecting integer value for "
739 "--local-port"));
740 return PJ_EINVAL;
741 }
742 cfg->udp_cfg.port = (pj_uint16_t)lval;
743 break;
744
Benny Prijono0a5cad82006-09-26 13:21:02 +0000745 case OPT_IP_ADDR: /* ip-addr */
746 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
747 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
748 break;
749
Benny Prijono8972bf02009-04-05 18:30:45 +0000750 case OPT_BOUND_ADDR: /* bound-addr */
751 cfg->udp_cfg.bound_addr = pj_str(pj_optarg);
752 cfg->rtp_cfg.bound_addr = pj_str(pj_optarg);
753 break;
754
Benny Prijonoe93e2872006-06-28 16:46:49 +0000755 case OPT_NO_UDP: /* no-udp */
756 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000757 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
758 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000759 }
760
761 cfg->no_udp = PJ_TRUE;
762 break;
763
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000764 case OPT_NOREFERSUB: /* norefersub */
765 cfg->no_refersub = PJ_TRUE;
766 break;
767
Benny Prijonoe93e2872006-06-28 16:46:49 +0000768 case OPT_NO_TCP: /* no-tcp */
769 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000770 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
771 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000772 }
773
774 cfg->no_tcp = PJ_TRUE;
775 break;
776
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000777 case OPT_PROXY: /* proxy */
778 if (pjsua_verify_sip_url(pj_optarg) != 0) {
779 PJ_LOG(1,(THIS_FILE,
780 "Error: invalid SIP URL '%s' "
781 "in proxy argument", pj_optarg));
782 return PJ_EINVAL;
783 }
784 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
785 break;
786
787 case OPT_OUTBOUND_PROXY: /* outbound proxy */
788 if (pjsua_verify_sip_url(pj_optarg) != 0) {
789 PJ_LOG(1,(THIS_FILE,
790 "Error: invalid SIP URL '%s' "
791 "in outbound proxy argument", pj_optarg));
792 return PJ_EINVAL;
793 }
794 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
795 break;
796
797 case OPT_REGISTRAR: /* registrar */
798 if (pjsua_verify_sip_url(pj_optarg) != 0) {
799 PJ_LOG(1,(THIS_FILE,
800 "Error: invalid SIP URL '%s' in "
801 "registrar argument", pj_optarg));
802 return PJ_EINVAL;
803 }
804 cur_acc->reg_uri = pj_str(pj_optarg);
805 break;
806
807 case OPT_REG_TIMEOUT: /* reg-timeout */
808 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
809 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
810 PJ_LOG(1,(THIS_FILE,
811 "Error: invalid value for --reg-timeout "
812 "(expecting 1-3600)"));
813 return PJ_EINVAL;
814 }
815 break;
816
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000817 case OPT_PUBLISH: /* publish */
818 cur_acc->publish_enabled = PJ_TRUE;
819 break;
820
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000821 case OPT_100REL: /** 100rel */
822 cur_acc->require_100rel = PJ_TRUE;
823 cfg->cfg.require_100rel = PJ_TRUE;
824 break;
825
Benny Prijono48ab2b72007-11-08 09:24:30 +0000826 case OPT_USE_IMS: /* Activate IMS settings */
827 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000828 break;
829
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000830 case OPT_ID: /* id */
831 if (pjsua_verify_sip_url(pj_optarg) != 0) {
832 PJ_LOG(1,(THIS_FILE,
833 "Error: invalid SIP URL '%s' "
834 "in local id argument", pj_optarg));
835 return PJ_EINVAL;
836 }
837 cur_acc->id = pj_str(pj_optarg);
838 break;
839
840 case OPT_CONTACT: /* contact */
841 if (pjsua_verify_sip_url(pj_optarg) != 0) {
842 PJ_LOG(1,(THIS_FILE,
843 "Error: invalid SIP URL '%s' "
844 "in contact argument", pj_optarg));
845 return PJ_EINVAL;
846 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000847 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000848 break;
849
Benny Prijono30fe4852008-12-10 16:54:16 +0000850 case OPT_CONTACT_PARAMS:
851 cur_acc->contact_params = pj_str(pj_optarg);
852 break;
853
Benny Prijonofce28542007-12-09 15:41:10 +0000854 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000855 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000856 break;
857
858 case OPT_USE_COMPACT_FORM:
859 /* enable compact form - from Ticket #342 */
860 {
861 extern pj_bool_t pjsip_use_compact_form;
862 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
863 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
864
865 pjsip_use_compact_form = PJ_TRUE;
866 /* do not transmit Allow header */
867 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
868 /* Do not include rtpmap for static payload types (<96) */
869 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
870 }
871 break;
872
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000873 case OPT_ACCEPT_REDIRECT:
874 cfg->redir_op = my_atoi(pj_optarg);
875 if (cfg->redir_op<0 || cfg->redir_op>PJSIP_REDIRECT_STOP) {
876 PJ_LOG(1,(THIS_FILE,
877 "Error: accept-redirect value '%s' ", pj_optarg));
878 return PJ_EINVAL;
879 }
880 break;
881
Benny Prijono91d06b62008-09-20 12:16:56 +0000882 case OPT_NO_FORCE_LR:
883 cfg->cfg.force_lr = PJ_FALSE;
884 break;
885
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000886 case OPT_NEXT_ACCOUNT: /* Add more account. */
887 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000888 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 break;
890
891 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000892 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000893 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000894 break;
895
896 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000897 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000898 break;
899
900 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000901 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
902 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000903#if PJSIP_HAS_DIGEST_AKA_AUTH
904 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
905 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
906 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
907#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000908 break;
909
910 case OPT_NEXT_CRED: /* next credential */
911 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000912 break;
913
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000914 case OPT_NAMESERVER: /* nameserver */
915 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
916 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
917 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
918 return PJ_ETOOMANY;
919 }
920 break;
921
Benny Prijonoebbf6892007-03-24 17:37:25 +0000922 case OPT_STUN_DOMAIN: /* STUN domain */
923 cfg->cfg.stun_domain = pj_str(pj_optarg);
924 break;
925
Benny Prijonoc97608e2007-03-23 16:34:20 +0000926 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000927 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000928 break;
929
930 case OPT_ADD_BUDDY: /* Add to buddy list. */
931 if (pjsua_verify_sip_url(pj_optarg) != 0) {
932 PJ_LOG(1,(THIS_FILE,
933 "Error: invalid URL '%s' in "
934 "--add-buddy option", pj_optarg));
935 return -1;
936 }
937 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
938 PJ_LOG(1,(THIS_FILE,
939 "Error: too many buddies in buddy list."));
940 return -1;
941 }
942 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
943 cfg->buddy_cnt++;
944 break;
945
946 case OPT_AUTO_PLAY:
947 cfg->auto_play = 1;
948 break;
949
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000950 case OPT_AUTO_PLAY_HANGUP:
951 cfg->auto_play_hangup = 1;
952 break;
953
Benny Prijono1ebd6142006-10-19 15:48:02 +0000954 case OPT_AUTO_REC:
955 cfg->auto_rec = 1;
956 break;
957
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000958 case OPT_AUTO_LOOP:
959 cfg->auto_loop = 1;
960 break;
961
Benny Prijono7ca96da2006-08-07 12:11:40 +0000962 case OPT_AUTO_CONF:
963 cfg->auto_conf = 1;
964 break;
965
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000966 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000967 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000968 break;
969
Benny Prijono4af234b2007-01-24 02:02:09 +0000970 case OPT_PLAY_TONE:
971 {
972 int f1, f2, on, off;
973 int n;
974
975 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
976 if (n != 4) {
977 puts("Expecting f1,f2,on,off in --play-tone");
978 return -1;
979 }
980
981 cfg->tones[cfg->tone_count].freq1 = (short)f1;
982 cfg->tones[cfg->tone_count].freq2 = (short)f2;
983 cfg->tones[cfg->tone_count].on_msec = (short)on;
984 cfg->tones[cfg->tone_count].off_msec = (short)off;
985 ++cfg->tone_count;
986 }
987 break;
988
Benny Prijono1ebd6142006-10-19 15:48:02 +0000989 case OPT_REC_FILE:
990 cfg->rec_file = pj_str(pj_optarg);
991 break;
992
Benny Prijonoc97608e2007-03-23 16:34:20 +0000993 case OPT_USE_ICE:
994 cfg->media_cfg.enable_ice = PJ_TRUE;
995 break;
996
Benny Prijono329d6382009-05-29 13:04:03 +0000997 case OPT_ICE_REGULAR:
998 cfg->media_cfg.ice_opt.aggressive = PJ_FALSE;
999 break;
1000
Benny Prijonof76e1392008-06-06 14:51:48 +00001001 case OPT_USE_TURN:
1002 cfg->media_cfg.enable_turn = PJ_TRUE;
1003 break;
1004
Benny Prijono329d6382009-05-29 13:04:03 +00001005 case OPT_ICE_MAX_HOSTS:
1006 cfg->media_cfg.ice_max_host_cands = my_atoi(pj_optarg);
Benny Prijonof76e1392008-06-06 14:51:48 +00001007 break;
1008
Benny Prijono551af422008-08-07 09:55:52 +00001009 case OPT_ICE_NO_RTCP:
1010 cfg->media_cfg.ice_no_rtcp = PJ_TRUE;
1011 break;
1012
Benny Prijonof76e1392008-06-06 14:51:48 +00001013 case OPT_TURN_SRV:
1014 cfg->media_cfg.turn_server = pj_str(pj_optarg);
1015 break;
1016
1017 case OPT_TURN_TCP:
1018 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
1019 break;
1020
1021 case OPT_TURN_USER:
1022 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
1023 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
1024 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
1025 break;
1026
1027 case OPT_TURN_PASSWD:
1028 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1029 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
1030 break;
1031
Benny Prijonod8179652008-01-23 20:39:07 +00001032#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1033 case OPT_USE_SRTP:
1034 app_config.cfg.use_srtp = my_atoi(pj_optarg);
1035 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
1036 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
1037 return -1;
1038 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001039 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +00001040 break;
Benny Prijonof6508982008-01-25 09:02:33 +00001041 case OPT_SRTP_SECURE:
1042 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
1043 if (!pj_isdigit(*pj_optarg) ||
1044 app_config.cfg.srtp_secure_signaling > 2)
1045 {
1046 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
1047 return -1;
1048 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001049 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +00001050 break;
Benny Prijonod8179652008-01-23 20:39:07 +00001051#endif
1052
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001053 case OPT_RTP_PORT:
1054 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +00001055 if (cfg->rtp_cfg.port == 0) {
1056 enum { START_PORT=4000 };
1057 unsigned range;
1058
1059 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
1060 cfg->rtp_cfg.port = START_PORT +
1061 ((pj_rand() % range) & 0xFFFE);
1062 }
1063
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001064 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
1065 PJ_LOG(1,(THIS_FILE,
1066 "Error: rtp-port argument value "
1067 "(expecting 1-65535"));
1068 return -1;
1069 }
1070 break;
1071
Benny Prijonofce28542007-12-09 15:41:10 +00001072 case OPT_DIS_CODEC:
1073 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
1074 break;
1075
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001076 case OPT_ADD_CODEC:
1077 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
1078 break;
1079
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001080 /* These options were no longer valid after new pjsua */
1081 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001082 case OPT_COMPLEXITY:
1083 cfg->complexity = my_atoi(pj_optarg);
1084 if (cfg->complexity < 0 || cfg->complexity > 10) {
1085 PJ_LOG(1,(THIS_FILE,
1086 "Error: invalid --complexity (expecting 0-10"));
1087 return -1;
1088 }
1089 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001090 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001091
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001092 case OPT_DURATION:
1093 cfg->duration = my_atoi(pj_optarg);
1094 break;
1095
Benny Prijonof521eb02006-08-06 23:07:25 +00001096 case OPT_THREAD_CNT:
1097 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1098 if (cfg->cfg.thread_cnt > 128) {
1099 PJ_LOG(1,(THIS_FILE,
1100 "Error: invalid --thread-cnt option"));
1101 return -1;
1102 }
1103 break;
1104
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001105 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001106 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1107 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001108 PJ_LOG(1,(THIS_FILE,
1109 "Error: invalid --ptime option"));
1110 return -1;
1111 }
1112 break;
1113
Benny Prijono0a12f002006-07-26 17:05:39 +00001114 case OPT_NO_VAD:
1115 cfg->media_cfg.no_vad = PJ_TRUE;
1116 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001117
Benny Prijonod79f25c2006-08-02 19:41:37 +00001118 case OPT_EC_TAIL:
1119 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1120 if (cfg->media_cfg.ec_tail_len > 1000) {
1121 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1122 "is too big"));
1123 return -1;
1124 }
1125 break;
1126
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001127 case OPT_EC_OPT:
1128 cfg->media_cfg.ec_options = my_atoi(pj_optarg);
1129 break;
1130
Benny Prijono0498d902006-06-19 14:49:14 +00001131 case OPT_QUALITY:
1132 cfg->media_cfg.quality = my_atoi(pj_optarg);
1133 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1134 PJ_LOG(1,(THIS_FILE,
1135 "Error: invalid --quality (expecting 0-10"));
1136 return -1;
1137 }
1138 break;
1139
Benny Prijono00cae612006-07-31 15:19:36 +00001140 case OPT_ILBC_MODE:
1141 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1142 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1143 PJ_LOG(1,(THIS_FILE,
1144 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1145 return -1;
1146 }
1147 break;
1148
1149 case OPT_RX_DROP_PCT:
1150 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1151 if (cfg->media_cfg.rx_drop_pct > 100) {
1152 PJ_LOG(1,(THIS_FILE,
1153 "Error: invalid --rx-drop-pct (expecting <= 100"));
1154 return -1;
1155 }
1156 break;
1157
1158 case OPT_TX_DROP_PCT:
1159 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1160 if (cfg->media_cfg.tx_drop_pct > 100) {
1161 PJ_LOG(1,(THIS_FILE,
1162 "Error: invalid --tx-drop-pct (expecting <= 100"));
1163 return -1;
1164 }
1165 break;
1166
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001167 case OPT_AUTO_ANSWER:
1168 cfg->auto_answer = my_atoi(pj_optarg);
1169 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1170 PJ_LOG(1,(THIS_FILE,
1171 "Error: invalid code in --auto-answer "
1172 "(expecting 100-699"));
1173 return -1;
1174 }
1175 break;
1176
1177 case OPT_MAX_CALLS:
1178 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001179 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001180 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1181 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001182 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001183 return -1;
1184 }
1185 break;
1186
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001187 case OPT_USE_TLS:
1188 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001189#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1190 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1191 return -1;
1192#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001193 break;
1194
1195 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001196 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1197#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1198 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1199 return -1;
1200#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001201 break;
1202
Benny Prijonof3bbc132006-12-25 06:43:59 +00001203 case OPT_TLS_CERT_FILE:
1204 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1205#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1206 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1207 return -1;
1208#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001209 break;
1210
Benny Prijonof3bbc132006-12-25 06:43:59 +00001211 case OPT_TLS_PRIV_FILE:
1212 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1213 break;
1214
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001215 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001216 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1217#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1218 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1219 return -1;
1220#endif
1221 break;
1222
1223 case OPT_TLS_VERIFY_SERVER:
1224 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1225 break;
1226
1227 case OPT_TLS_VERIFY_CLIENT:
1228 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1229 break;
1230
1231 case OPT_TLS_NEG_TIMEOUT:
1232 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001233 break;
1234
Benny Prijonoe10db842008-07-01 15:31:59 +00001235 case OPT_TLS_SRV_NAME:
1236 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1237 break;
1238
Benny Prijono4e5d5512007-03-06 18:11:30 +00001239 case OPT_CAPTURE_DEV:
1240 cfg->capture_dev = atoi(pj_optarg);
1241 break;
1242
1243 case OPT_PLAYBACK_DEV:
1244 cfg->playback_dev = atoi(pj_optarg);
1245 break;
1246
Benny Prijonoebc32c32008-06-12 13:30:39 +00001247 case OPT_STDOUT_REFRESH:
1248 stdout_refresh = atoi(pj_optarg);
1249 break;
1250
1251 case OPT_STDOUT_REFRESH_TEXT:
1252 stdout_refresh_text = pj_optarg;
1253 break;
1254
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001255#ifdef _IONBF
1256 case OPT_STDOUT_NO_BUF:
1257 setvbuf(stdout, NULL, _IONBF, 0);
1258 break;
1259#endif
1260
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001261 case OPT_CAPTURE_LAT:
1262 cfg->capture_lat = atoi(pj_optarg);
1263 break;
1264
1265 case OPT_PLAYBACK_LAT:
1266 cfg->playback_lat = atoi(pj_optarg);
1267 break;
1268
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001269 case OPT_SND_AUTO_CLOSE:
1270 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1271 break;
1272
Benny Prijono91d20f42008-06-14 19:42:37 +00001273 case OPT_NO_TONES:
1274 cfg->no_tones = PJ_TRUE;
1275 break;
1276
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001277 case OPT_JB_MAX_SIZE:
1278 cfg->media_cfg.jb_max = atoi(pj_optarg);
1279 break;
1280
Benny Prijono8972bf02009-04-05 18:30:45 +00001281#if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
1282 case OPT_IPV6:
1283 cfg->ipv6 = PJ_TRUE;
1284 break;
1285#endif
1286
Benny Prijono22a300a2006-06-14 20:04:55 +00001287 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001288 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001289 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001290 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001291 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001292 }
1293 }
1294
1295 if (pj_optind != argc) {
1296 pj_str_t uri_arg;
1297
1298 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1299 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1300 return -1;
1301 }
1302 uri_arg = pj_str(argv[pj_optind]);
1303 if (uri_to_call)
1304 *uri_to_call = uri_arg;
1305 pj_optind++;
1306
1307 /* Add URI to call to buddy list if it's not already there */
1308 for (i=0; i<cfg->buddy_cnt; ++i) {
1309 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1310 break;
1311 }
1312 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1313 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1314 }
1315
1316 } else {
1317 if (uri_to_call)
1318 uri_to_call->slen = 0;
1319 }
1320
1321 if (pj_optind != argc) {
1322 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1323 return PJ_EINVAL;
1324 }
1325
Benny Prijono56315612006-07-18 14:39:40 +00001326 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1327 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001328
1329 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001330 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1331
1332 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001333 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001334 acfg->cred_count++;
1335 }
1336
1337 /* When IMS mode is enabled for the account, verify that settings
1338 * are okay.
1339 */
1340 /* For now we check if IMS mode is activated by looking if
1341 * initial_auth is set.
1342 */
1343 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1344 /* Realm must point to the real domain */
1345 if (*acfg->cred_info[0].realm.ptr=='*') {
1346 PJ_LOG(1,(THIS_FILE,
1347 "Error: cannot use '*' as realm with IMS"));
1348 return PJ_EINVAL;
1349 }
1350
1351 /* Username for authentication must be in a@b format */
1352 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1353 PJ_LOG(1,(THIS_FILE,
1354 "Error: Username for authentication must "
1355 "be in user@domain format with IMS"));
1356 return PJ_EINVAL;
1357 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001358 }
1359 }
1360
1361
1362 return PJ_SUCCESS;
1363}
1364
1365
1366/*
1367 * Save account settings
1368 */
1369static void write_account_settings(int acc_index, pj_str_t *result)
1370{
1371 unsigned i;
1372 char line[128];
1373 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1374
1375
1376 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1377 pj_strcat2(result, line);
1378
1379
1380 /* Identity */
1381 if (acc_cfg->id.slen) {
1382 pj_ansi_sprintf(line, "--id %.*s\n",
1383 (int)acc_cfg->id.slen,
1384 acc_cfg->id.ptr);
1385 pj_strcat2(result, line);
1386 }
1387
1388 /* Registrar server */
1389 if (acc_cfg->reg_uri.slen) {
1390 pj_ansi_sprintf(line, "--registrar %.*s\n",
1391 (int)acc_cfg->reg_uri.slen,
1392 acc_cfg->reg_uri.ptr);
1393 pj_strcat2(result, line);
1394
1395 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1396 acc_cfg->reg_timeout);
1397 pj_strcat2(result, line);
1398 }
1399
1400 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001401 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001402 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001403 (int)acc_cfg->force_contact.slen,
1404 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001405 pj_strcat2(result, line);
1406 }
1407
Benny Prijono30fe4852008-12-10 16:54:16 +00001408 /* Contact parameters */
1409 if (acc_cfg->contact_params.slen) {
1410 pj_ansi_sprintf(line, "--contact-params %.*s\n",
1411 (int)acc_cfg->contact_params.slen,
1412 acc_cfg->contact_params.ptr);
1413 pj_strcat2(result, line);
1414 }
1415
Benny Prijonofce28542007-12-09 15:41:10 +00001416 /* */
Benny Prijono7f630432008-09-24 16:52:41 +00001417 if (acc_cfg->allow_contact_rewrite!=1)
Benny Prijonofce28542007-12-09 15:41:10 +00001418 {
Benny Prijono251e02a2008-07-25 10:05:55 +00001419 pj_ansi_sprintf(line, "--auto-update-nat %i\n",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001420 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001421 pj_strcat2(result, line);
1422 }
1423
Benny Prijonod8179652008-01-23 20:39:07 +00001424#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1425 /* SRTP */
1426 if (acc_cfg->use_srtp) {
1427 pj_ansi_sprintf(line, "--use-srtp %i\n",
1428 (int)acc_cfg->use_srtp);
1429 pj_strcat2(result, line);
1430 }
1431#endif
1432
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001433 /* Proxy */
1434 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1435 pj_ansi_sprintf(line, "--proxy %.*s\n",
1436 (int)acc_cfg->proxy[i].slen,
1437 acc_cfg->proxy[i].ptr);
1438 pj_strcat2(result, line);
1439 }
1440
1441 /* Credentials */
1442 for (i=0; i<acc_cfg->cred_count; ++i) {
1443 if (acc_cfg->cred_info[i].realm.slen) {
1444 pj_ansi_sprintf(line, "--realm %.*s\n",
1445 (int)acc_cfg->cred_info[i].realm.slen,
1446 acc_cfg->cred_info[i].realm.ptr);
1447 pj_strcat2(result, line);
1448 }
1449
1450 if (acc_cfg->cred_info[i].username.slen) {
1451 pj_ansi_sprintf(line, "--username %.*s\n",
1452 (int)acc_cfg->cred_info[i].username.slen,
1453 acc_cfg->cred_info[i].username.ptr);
1454 pj_strcat2(result, line);
1455 }
1456
1457 if (acc_cfg->cred_info[i].data.slen) {
1458 pj_ansi_sprintf(line, "--password %.*s\n",
1459 (int)acc_cfg->cred_info[i].data.slen,
1460 acc_cfg->cred_info[i].data.ptr);
1461 pj_strcat2(result, line);
1462 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001463
1464 if (i != acc_cfg->cred_count - 1)
1465 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001466 }
1467
1468}
1469
1470
1471/*
1472 * Write settings.
1473 */
1474static int write_settings(const struct app_config *config,
1475 char *buf, pj_size_t max)
1476{
1477 unsigned acc_index;
1478 unsigned i;
1479 pj_str_t cfg;
1480 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001481 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001482
1483 PJ_UNUSED_ARG(max);
1484
1485 cfg.ptr = buf;
1486 cfg.slen = 0;
1487
1488 /* Logging. */
1489 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1490 pj_ansi_sprintf(line, "--log-level %d\n",
1491 config->log_cfg.level);
1492 pj_strcat2(&cfg, line);
1493
1494 pj_ansi_sprintf(line, "--app-log-level %d\n",
1495 config->log_cfg.console_level);
1496 pj_strcat2(&cfg, line);
1497
1498 if (config->log_cfg.log_filename.slen) {
1499 pj_ansi_sprintf(line, "--log-file %.*s\n",
1500 (int)config->log_cfg.log_filename.slen,
1501 config->log_cfg.log_filename.ptr);
1502 pj_strcat2(&cfg, line);
1503 }
1504
1505
1506 /* Save account settings. */
1507 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1508
1509 write_account_settings(acc_index, &cfg);
1510
1511 if (acc_index < config->acc_cnt-1)
1512 pj_strcat2(&cfg, "--next-account\n");
1513 }
1514
1515
1516 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1517
Benny Prijono98d8c952009-02-06 08:13:43 +00001518 /* Nameservers */
1519 for (i=0; i<config->cfg.nameserver_count; ++i) {
1520 pj_ansi_sprintf(line, "--nameserver %.*s\n",
1521 (int)config->cfg.nameserver[i].slen,
1522 config->cfg.nameserver[i].ptr);
1523 pj_strcat2(&cfg, line);
1524 }
1525
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001526 /* Outbound proxy */
1527 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1528 pj_ansi_sprintf(line, "--outbound %.*s\n",
1529 (int)config->cfg.outbound_proxy[i].slen,
1530 config->cfg.outbound_proxy[i].ptr);
1531 pj_strcat2(&cfg, line);
1532 }
1533
Benny Prijono8972bf02009-04-05 18:30:45 +00001534 /* Transport options */
1535 if (config->ipv6) {
1536 pj_strcat2(&cfg, "--ipv6\n");
1537 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001538
1539 /* UDP Transport. */
1540 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1541 pj_strcat2(&cfg, line);
1542
Benny Prijono0a5cad82006-09-26 13:21:02 +00001543 /* IP address, if any. */
1544 if (config->udp_cfg.public_addr.slen) {
1545 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1546 (int)config->udp_cfg.public_addr.slen,
1547 config->udp_cfg.public_addr.ptr);
1548 pj_strcat2(&cfg, line);
1549 }
1550
Benny Prijono8972bf02009-04-05 18:30:45 +00001551 /* Bound IP address, if any. */
1552 if (config->udp_cfg.bound_addr.slen) {
1553 pj_ansi_sprintf(line, "--bound-addr %.*s\n",
1554 (int)config->udp_cfg.bound_addr.slen,
1555 config->udp_cfg.bound_addr.ptr);
1556 pj_strcat2(&cfg, line);
1557 }
1558
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001559 /* No TCP ? */
1560 if (config->no_tcp) {
1561 pj_strcat2(&cfg, "--no-tcp\n");
1562 }
1563
1564 /* No UDP ? */
1565 if (config->no_udp) {
1566 pj_strcat2(&cfg, "--no-udp\n");
1567 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001568
1569 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001570 if (config->cfg.stun_domain.slen) {
1571 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1572 (int)config->cfg.stun_domain.slen,
1573 config->cfg.stun_domain.ptr);
1574 pj_strcat2(&cfg, line);
1575 }
1576 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001577 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001578 (int)config->cfg.stun_host.slen,
1579 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001580 pj_strcat2(&cfg, line);
1581 }
1582
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001583 /* TLS */
1584 if (config->use_tls)
1585 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001586 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001587 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001588 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1589 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001590 pj_strcat2(&cfg, line);
1591 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001592 if (config->udp_cfg.tls_setting.cert_file.slen) {
1593 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1594 (int)config->udp_cfg.tls_setting.cert_file.slen,
1595 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001596 pj_strcat2(&cfg, line);
1597 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001598 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1599 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1600 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1601 config->udp_cfg.tls_setting.privkey_file.ptr);
1602 pj_strcat2(&cfg, line);
1603 }
1604
1605 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001606 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001607 (int)config->udp_cfg.tls_setting.password.slen,
1608 config->udp_cfg.tls_setting.password.ptr);
1609 pj_strcat2(&cfg, line);
1610 }
1611
Benny Prijonoe10db842008-07-01 15:31:59 +00001612 if (config->udp_cfg.tls_setting.server_name.slen) {
1613 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1614 (int)config->udp_cfg.tls_setting.server_name.slen,
1615 config->udp_cfg.tls_setting.server_name.ptr);
1616 pj_strcat2(&cfg, line);
1617 }
1618
Benny Prijonof3bbc132006-12-25 06:43:59 +00001619 if (config->udp_cfg.tls_setting.verify_server)
1620 pj_strcat2(&cfg, "--tls-verify-server\n");
1621
1622 if (config->udp_cfg.tls_setting.verify_client)
1623 pj_strcat2(&cfg, "--tls-verify-client\n");
1624
1625 if (config->udp_cfg.tls_setting.timeout.sec) {
1626 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001627 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001628 pj_strcat2(&cfg, line);
1629 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001630
1631 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1632
Benny Prijonof6508982008-01-25 09:02:33 +00001633 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001634#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001635 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1636 pj_ansi_sprintf(line, "--use-srtp %d\n",
1637 app_config.cfg.use_srtp);
1638 pj_strcat2(&cfg, line);
1639 }
1640 if (app_config.cfg.srtp_secure_signaling !=
1641 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1642 {
1643 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1644 app_config.cfg.srtp_secure_signaling);
1645 pj_strcat2(&cfg, line);
1646 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001647#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001648
Benny Prijonof76e1392008-06-06 14:51:48 +00001649 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001650 if (config->media_cfg.enable_ice)
1651 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001652
Benny Prijono329d6382009-05-29 13:04:03 +00001653 if (config->media_cfg.ice_opt.aggressive == PJ_FALSE)
1654 pj_strcat2(&cfg, "--ice-regular\n");
1655
Benny Prijonof76e1392008-06-06 14:51:48 +00001656 if (config->media_cfg.enable_turn)
1657 pj_strcat2(&cfg, "--use-turn\n");
1658
Benny Prijono329d6382009-05-29 13:04:03 +00001659 if (config->media_cfg.ice_max_host_cands >= 0) {
1660 pj_ansi_sprintf(line, "--ice_max_host_cands %d\n",
1661 config->media_cfg.ice_max_host_cands);
1662 pj_strcat2(&cfg, line);
1663 }
Benny Prijonof76e1392008-06-06 14:51:48 +00001664
Benny Prijono551af422008-08-07 09:55:52 +00001665 if (config->media_cfg.ice_no_rtcp)
1666 pj_strcat2(&cfg, "--ice-no-rtcp\n");
1667
Benny Prijonof76e1392008-06-06 14:51:48 +00001668 if (config->media_cfg.turn_server.slen) {
1669 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1670 (int)config->media_cfg.turn_server.slen,
1671 config->media_cfg.turn_server.ptr);
1672 pj_strcat2(&cfg, line);
1673 }
1674
1675 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1676 pj_strcat2(&cfg, "--turn-tcp\n");
1677
1678 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1679 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1680 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1681 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1682 pj_strcat2(&cfg, line);
1683 }
1684
1685 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1686 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1687 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1688 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1689 pj_strcat2(&cfg, line);
1690 }
1691
1692 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001693 if (config->null_audio)
1694 pj_strcat2(&cfg, "--null-audio\n");
1695 if (config->auto_play)
1696 pj_strcat2(&cfg, "--auto-play\n");
1697 if (config->auto_loop)
1698 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001699 if (config->auto_conf)
1700 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001701 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001702 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001703 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001704 pj_strcat2(&cfg, line);
1705 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001706 for (i=0; i<config->tone_count; ++i) {
1707 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1708 config->tones[i].freq1, config->tones[i].freq2,
1709 config->tones[i].on_msec, config->tones[i].off_msec);
1710 pj_strcat2(&cfg, line);
1711 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001712 if (config->rec_file.slen) {
1713 pj_ansi_sprintf(line, "--rec-file %s\n",
1714 config->rec_file.ptr);
1715 pj_strcat2(&cfg, line);
1716 }
1717 if (config->auto_rec)
1718 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001719 if (config->capture_dev != PJSUA_INVALID_ID) {
1720 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1721 pj_strcat2(&cfg, line);
1722 }
1723 if (config->playback_dev != PJSUA_INVALID_ID) {
1724 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1725 pj_strcat2(&cfg, line);
1726 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001727 if (config->media_cfg.snd_auto_close_time != -1) {
1728 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1729 config->media_cfg.snd_auto_close_time);
1730 pj_strcat2(&cfg, line);
1731 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001732 if (config->no_tones) {
1733 pj_strcat2(&cfg, "--no-tones\n");
1734 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001735 if (config->media_cfg.jb_max != -1) {
1736 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1737 config->media_cfg.jb_max);
1738 pj_strcat2(&cfg, line);
1739 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001740
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001741 /* Sound device latency */
1742 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1743 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1744 pj_strcat2(&cfg, line);
1745 }
1746 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1747 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1748 pj_strcat2(&cfg, line);
1749 }
1750
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001751 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001752 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001753 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001754 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001755 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001756 } else {
1757 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1758 config->media_cfg.clock_rate);
1759 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001760 }
Benny Prijono70972992006-08-05 11:13:58 +00001761
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001762 if (config->media_cfg.snd_clock_rate &&
1763 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1764 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001765 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1766 config->media_cfg.snd_clock_rate);
1767 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001768 }
1769
Benny Prijono7d60d052008-03-29 12:24:20 +00001770 /* Stereo mode. */
1771 if (config->media_cfg.channel_count == 2) {
1772 pj_ansi_sprintf(line, "--stereo\n");
1773 pj_strcat2(&cfg, line);
1774 }
1775
Benny Prijono70972992006-08-05 11:13:58 +00001776 /* quality */
1777 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001778 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001779 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001780 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001781 } else {
1782 pj_ansi_sprintf(line, "#using default --quality %d\n",
1783 config->media_cfg.quality);
1784 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001785 }
Benny Prijono0498d902006-06-19 14:49:14 +00001786
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001787
1788 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001789 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001790 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001791 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001792 pj_strcat2(&cfg, line);
1793 }
1794
Benny Prijono70972992006-08-05 11:13:58 +00001795 /* no-vad */
1796 if (config->media_cfg.no_vad) {
1797 pj_strcat2(&cfg, "--no-vad\n");
1798 }
1799
1800 /* ec-tail */
1801 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1802 pj_ansi_sprintf(line, "--ec-tail %d\n",
1803 config->media_cfg.ec_tail_len);
1804 pj_strcat2(&cfg, line);
1805 } else {
1806 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1807 config->media_cfg.ec_tail_len);
1808 pj_strcat2(&cfg, line);
1809 }
1810
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001811 /* ec-opt */
1812 if (config->media_cfg.ec_options != 0) {
1813 pj_ansi_sprintf(line, "--ec-opt %d\n",
1814 config->media_cfg.ec_options);
1815 pj_strcat2(&cfg, line);
1816 }
Benny Prijono70972992006-08-05 11:13:58 +00001817
1818 /* ilbc-mode */
1819 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1820 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1821 config->media_cfg.ilbc_mode);
1822 pj_strcat2(&cfg, line);
1823 } else {
1824 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1825 config->media_cfg.ilbc_mode);
1826 pj_strcat2(&cfg, line);
1827 }
1828
1829 /* RTP drop */
1830 if (config->media_cfg.tx_drop_pct) {
1831 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1832 config->media_cfg.tx_drop_pct);
1833 pj_strcat2(&cfg, line);
1834
1835 }
1836 if (config->media_cfg.rx_drop_pct) {
1837 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1838 config->media_cfg.rx_drop_pct);
1839 pj_strcat2(&cfg, line);
1840
1841 }
1842
1843
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001844 /* Start RTP port. */
1845 pj_ansi_sprintf(line, "--rtp-port %d\n",
1846 config->rtp_cfg.port);
1847 pj_strcat2(&cfg, line);
1848
1849 /* Add codec. */
1850 for (i=0; i<config->codec_cnt; ++i) {
1851 pj_ansi_sprintf(line, "--add-codec %s\n",
1852 config->codec_arg[i].ptr);
1853 pj_strcat2(&cfg, line);
1854 }
Benny Prijonofce28542007-12-09 15:41:10 +00001855 /* Disable codec */
1856 for (i=0; i<config->codec_dis_cnt; ++i) {
1857 pj_ansi_sprintf(line, "--dis-codec %s\n",
1858 config->codec_dis[i].ptr);
1859 pj_strcat2(&cfg, line);
1860 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001861
1862 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1863
1864 /* Auto-answer. */
1865 if (config->auto_answer != 0) {
1866 pj_ansi_sprintf(line, "--auto-answer %d\n",
1867 config->auto_answer);
1868 pj_strcat2(&cfg, line);
1869 }
1870
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001871 /* accept-redirect */
1872 if (config->redir_op != PJSIP_REDIRECT_ACCEPT) {
1873 pj_ansi_sprintf(line, "--accept-redirect %d\n",
1874 config->redir_op);
1875 pj_strcat2(&cfg, line);
1876 }
1877
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001878 /* Max calls. */
1879 pj_ansi_sprintf(line, "--max-calls %d\n",
1880 config->cfg.max_calls);
1881 pj_strcat2(&cfg, line);
1882
1883 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001884 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001885 pj_ansi_sprintf(line, "--duration %d\n",
1886 config->duration);
1887 pj_strcat2(&cfg, line);
1888 }
1889
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001890 /* norefersub ? */
1891 if (config->no_refersub) {
1892 pj_strcat2(&cfg, "--norefersub\n");
1893 }
1894
Benny Prijonofce28542007-12-09 15:41:10 +00001895 if (pjsip_use_compact_form)
1896 {
1897 pj_strcat2(&cfg, "--use-compact-form\n");
1898 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001899
Benny Prijono329d6382009-05-29 13:04:03 +00001900 if (!config->cfg.force_lr) {
Benny Prijono91d06b62008-09-20 12:16:56 +00001901 pj_strcat2(&cfg, "--no-force-lr\n");
1902 }
1903
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001904 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1905
1906 /* Add buddies. */
1907 for (i=0; i<config->buddy_cnt; ++i) {
1908 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1909 (int)config->buddy_cfg[i].uri.slen,
1910 config->buddy_cfg[i].uri.ptr);
1911 pj_strcat2(&cfg, line);
1912 }
1913
1914
1915 *(cfg.ptr + cfg.slen) = '\0';
1916 return cfg.slen;
1917}
1918
1919
1920/*
1921 * Dump application states.
1922 */
1923static void app_dump(pj_bool_t detail)
1924{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001925 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001926}
1927
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001928/*
1929 * Print log of call states. Since call states may be too long for logger,
1930 * printing it is a bit tricky, it should be printed part by part as long
1931 * as the logger can accept.
1932 */
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001933static void log_call_dump(int call_id)
1934{
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001935 unsigned call_dump_len;
1936 unsigned part_len;
1937 unsigned part_idx;
1938 unsigned log_decor;
1939
1940 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1941 sizeof(some_buf), " ");
1942 call_dump_len = strlen(some_buf);
1943
1944 log_decor = pj_log_get_decor();
1945 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1946 PJ_LOG(3,(THIS_FILE, "\n"));
1947 pj_log_set_decor(0);
1948
1949 part_idx = 0;
1950 part_len = PJ_LOG_MAX_SIZE-80;
1951 while (part_idx < call_dump_len) {
1952 char p_orig, *p;
1953
1954 p = &some_buf[part_idx];
1955 if (part_idx + part_len > call_dump_len)
1956 part_len = call_dump_len - part_idx;
1957 p_orig = p[part_len];
1958 p[part_len] = '\0';
1959 PJ_LOG(3,(THIS_FILE, "%s", p));
1960 p[part_len] = p_orig;
1961 part_idx += part_len;
1962 }
1963 pj_log_set_decor(log_decor);
1964}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001965
1966/*****************************************************************************
1967 * Console application
1968 */
1969
Benny Prijono91d20f42008-06-14 19:42:37 +00001970static void ringback_start(pjsua_call_id call_id)
1971{
1972 if (app_config.no_tones)
1973 return;
1974
1975 if (app_config.call_data[call_id].ringback_on)
1976 return;
1977
1978 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1979
1980 if (++app_config.ringback_cnt==1 &&
1981 app_config.ringback_slot!=PJSUA_INVALID_ID)
1982 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001983 pjsua_conf_connect(app_config.ringback_slot, 0);
1984 }
1985}
1986
1987static void ring_stop(pjsua_call_id call_id)
1988{
1989 if (app_config.no_tones)
1990 return;
1991
1992 if (app_config.call_data[call_id].ringback_on) {
1993 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1994
1995 pj_assert(app_config.ringback_cnt>0);
1996 if (--app_config.ringback_cnt == 0 &&
1997 app_config.ringback_slot!=PJSUA_INVALID_ID)
1998 {
1999 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00002000 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00002001 }
2002 }
2003
2004 if (app_config.call_data[call_id].ring_on) {
2005 app_config.call_data[call_id].ring_on = PJ_FALSE;
2006
2007 pj_assert(app_config.ring_cnt>0);
2008 if (--app_config.ring_cnt == 0 &&
2009 app_config.ring_slot!=PJSUA_INVALID_ID)
2010 {
2011 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00002012 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00002013 }
2014 }
2015}
2016
2017static void ring_start(pjsua_call_id call_id)
2018{
2019 if (app_config.no_tones)
2020 return;
2021
2022 if (app_config.call_data[call_id].ring_on)
2023 return;
2024
2025 app_config.call_data[call_id].ring_on = PJ_TRUE;
2026
2027 if (++app_config.ring_cnt==1 &&
2028 app_config.ring_slot!=PJSUA_INVALID_ID)
2029 {
Benny Prijono91d20f42008-06-14 19:42:37 +00002030 pjsua_conf_connect(app_config.ring_slot, 0);
2031 }
2032}
2033
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002034/*
2035 * Find next call when current call is disconnected or when user
2036 * press ']'
2037 */
2038static pj_bool_t find_next_call(void)
2039{
2040 int i, max;
2041
2042 max = pjsua_call_get_max_count();
2043 for (i=current_call+1; i<max; ++i) {
2044 if (pjsua_call_is_active(i)) {
2045 current_call = i;
2046 return PJ_TRUE;
2047 }
2048 }
2049
2050 for (i=0; i<current_call; ++i) {
2051 if (pjsua_call_is_active(i)) {
2052 current_call = i;
2053 return PJ_TRUE;
2054 }
2055 }
2056
2057 current_call = PJSUA_INVALID_ID;
2058 return PJ_FALSE;
2059}
2060
2061
2062/*
2063 * Find previous call when user press '['
2064 */
2065static pj_bool_t find_prev_call(void)
2066{
2067 int i, max;
2068
2069 max = pjsua_call_get_max_count();
2070 for (i=current_call-1; i>=0; --i) {
2071 if (pjsua_call_is_active(i)) {
2072 current_call = i;
2073 return PJ_TRUE;
2074 }
2075 }
2076
2077 for (i=max-1; i>current_call; --i) {
2078 if (pjsua_call_is_active(i)) {
2079 current_call = i;
2080 return PJ_TRUE;
2081 }
2082 }
2083
2084 current_call = PJSUA_INVALID_ID;
2085 return PJ_FALSE;
2086}
2087
2088
Benny Prijono804ff0a2006-09-14 11:17:48 +00002089/* Callback from timer when the maximum call duration has been
2090 * exceeded.
2091 */
2092static void call_timeout_callback(pj_timer_heap_t *timer_heap,
2093 struct pj_timer_entry *entry)
2094{
2095 pjsua_call_id call_id = entry->id;
2096 pjsua_msg_data msg_data;
2097 pjsip_generic_string_hdr warn;
2098 pj_str_t hname = pj_str("Warning");
2099 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
2100
2101 PJ_UNUSED_ARG(timer_heap);
2102
Benny Prijono148c9dd2006-09-19 13:37:53 +00002103 if (call_id == PJSUA_INVALID_ID) {
2104 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
2105 return;
2106 }
2107
Benny Prijono804ff0a2006-09-14 11:17:48 +00002108 /* Add warning header */
2109 pjsua_msg_data_init(&msg_data);
2110 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
2111 pj_list_push_back(&msg_data.hdr_list, &warn);
2112
2113 /* Call duration has been exceeded; disconnect the call */
2114 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
2115 "for call %d, disconnecting the call",
2116 app_config.duration, call_id));
2117 entry->id = PJSUA_INVALID_ID;
2118 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
2119}
2120
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002121
2122/*
2123 * Handler when invite state has changed.
2124 */
2125static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
2126{
2127 pjsua_call_info call_info;
2128
2129 PJ_UNUSED_ARG(e);
2130
2131 pjsua_call_get_info(call_id, &call_info);
2132
2133 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2134
Benny Prijono91d20f42008-06-14 19:42:37 +00002135 /* Stop all ringback for this call */
2136 ring_stop(call_id);
2137
Benny Prijono804ff0a2006-09-14 11:17:48 +00002138 /* Cancel duration timer, if any */
2139 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2140 struct call_data *cd = &app_config.call_data[call_id];
2141 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2142
2143 cd->timer.id = PJSUA_INVALID_ID;
2144 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2145 }
2146
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002147 /* Rewind play file when hangup automatically,
2148 * since file is not looped
2149 */
2150 if (app_config.auto_play_hangup)
2151 pjsua_player_set_pos(app_config.wav_id, 0);
2152
2153
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002154 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2155 call_id,
2156 call_info.last_status,
2157 call_info.last_status_text.ptr));
2158
2159 if (call_id == current_call) {
2160 find_next_call();
2161 }
2162
Benny Prijono4be63b52006-11-25 14:50:25 +00002163 /* Dump media state upon disconnected */
2164 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002165 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002166 "Call %d disconnected, dumping media stats..",
2167 call_id));
2168 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002169 }
2170
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002171 } else {
2172
Benny Prijono804ff0a2006-09-14 11:17:48 +00002173 if (app_config.duration!=NO_LIMIT &&
2174 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2175 {
2176 /* Schedule timer to hangup call after the specified duration */
2177 struct call_data *cd = &app_config.call_data[call_id];
2178 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2179 pj_time_val delay;
2180
2181 cd->timer.id = call_id;
2182 delay.sec = app_config.duration;
2183 delay.msec = 0;
2184 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2185 }
2186
Benny Prijono4be63b52006-11-25 14:50:25 +00002187 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2188 int code;
2189 pj_str_t reason;
2190 pjsip_msg *msg;
2191
2192 /* This can only occur because of TX or RX message */
2193 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2194
2195 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2196 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2197 } else {
2198 msg = e->body.tsx_state.src.tdata->msg;
2199 }
2200
2201 code = msg->line.status.code;
2202 reason = msg->line.status.reason;
2203
Benny Prijono91d20f42008-06-14 19:42:37 +00002204 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2205 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2206 msg->body == NULL &&
2207 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2208 {
2209 ringback_start(call_id);
2210 }
2211
Benny Prijono4be63b52006-11-25 14:50:25 +00002212 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2213 call_id, call_info.state_text.ptr,
2214 code, (int)reason.slen, reason.ptr));
2215 } else {
2216 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2217 call_id,
2218 call_info.state_text.ptr));
2219 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002220
2221 if (current_call==PJSUA_INVALID_ID)
2222 current_call = call_id;
2223
2224 }
2225}
2226
2227
2228/**
2229 * Handler when there is incoming call.
2230 */
2231static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2232 pjsip_rx_data *rdata)
2233{
2234 pjsua_call_info call_info;
2235
2236 PJ_UNUSED_ARG(acc_id);
2237 PJ_UNUSED_ARG(rdata);
2238
2239 pjsua_call_get_info(call_id, &call_info);
2240
Benny Prijono91d20f42008-06-14 19:42:37 +00002241 /* Start ringback */
2242 ring_start(call_id);
2243
Benny Prijono2285e7e2008-12-17 14:28:18 +00002244 if (current_call==PJSUA_INVALID_ID)
2245 current_call = call_id;
2246
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002247 if (app_config.auto_answer > 0) {
2248 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2249 }
2250
2251 if (app_config.auto_answer < 200) {
2252 PJ_LOG(3,(THIS_FILE,
2253 "Incoming call for account %d!\n"
2254 "From: %s\n"
2255 "To: %s\n"
2256 "Press a to answer or h to reject call",
2257 acc_id,
2258 call_info.remote_info.ptr,
2259 call_info.local_info.ptr));
2260 }
2261}
2262
2263
2264/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002265 * Handler when a transaction within a call has changed state.
2266 */
2267static void on_call_tsx_state(pjsua_call_id call_id,
2268 pjsip_transaction *tsx,
2269 pjsip_event *e)
2270{
2271 const pjsip_method info_method =
2272 {
2273 PJSIP_OTHER_METHOD,
2274 { "INFO", 4 }
2275 };
2276
2277 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2278 /*
2279 * Handle INFO method.
2280 */
2281 if (tsx->role == PJSIP_ROLE_UAC &&
2282 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002283 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2284 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002285 {
2286 /* Status of outgoing INFO request */
2287 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2288 PJ_LOG(4,(THIS_FILE,
2289 "Call %d: DTMF sent successfully with INFO",
2290 call_id));
2291 } else if (tsx->status_code >= 300) {
2292 PJ_LOG(4,(THIS_FILE,
2293 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2294 call_id,
2295 tsx->status_code,
2296 (int)tsx->status_text.slen,
2297 tsx->status_text.ptr));
2298 }
2299 } else if (tsx->role == PJSIP_ROLE_UAS &&
2300 tsx->state == PJSIP_TSX_STATE_TRYING)
2301 {
2302 /* Answer incoming INFO with 200/OK */
2303 pjsip_rx_data *rdata;
2304 pjsip_tx_data *tdata;
2305 pj_status_t status;
2306
2307 rdata = e->body.tsx_state.src.rdata;
2308
2309 if (rdata->msg_info.msg->body) {
2310 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2311 200, NULL, &tdata);
2312 if (status == PJ_SUCCESS)
2313 status = pjsip_tsx_send_msg(tsx, tdata);
2314
2315 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2316 call_id,
2317 (int)rdata->msg_info.msg->body->len,
2318 rdata->msg_info.msg->body->data));
2319 } else {
2320 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2321 400, NULL, &tdata);
2322 if (status == PJ_SUCCESS)
2323 status = pjsip_tsx_send_msg(tsx, tdata);
2324 }
2325 }
2326 }
2327}
2328
2329
2330/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002331 * Callback on media state changed event.
2332 * The action may connect the call to sound device, to file, or
2333 * to loop the call.
2334 */
2335static void on_call_media_state(pjsua_call_id call_id)
2336{
2337 pjsua_call_info call_info;
2338
2339 pjsua_call_get_info(call_id, &call_info);
2340
Benny Prijono91d20f42008-06-14 19:42:37 +00002341 /* Stop ringback */
2342 ring_stop(call_id);
2343
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002344 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
2345 * otherwise we should NOT connect the ports.
2346 */
2347 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE ||
2348 call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
2349 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002350 pj_bool_t connect_sound = PJ_TRUE;
2351
2352 /* Loopback sound, if desired */
2353 if (app_config.auto_loop) {
2354 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2355 connect_sound = PJ_FALSE;
Benny Prijonof6c77f42008-08-13 13:52:19 +00002356 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00002357
Benny Prijonof6c77f42008-08-13 13:52:19 +00002358 /* Automatically record conversation, if desired */
2359 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2360 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002361 }
2362
2363 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002364 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2365 app_config.wav_port != PJSUA_INVALID_ID)
2366 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002367 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2368 connect_sound = PJ_FALSE;
2369 }
2370
Benny Prijono7ca96da2006-08-07 12:11:40 +00002371 /* Put call in conference with other calls, if desired */
2372 if (app_config.auto_conf) {
2373 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002374 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002375 unsigned i;
2376
2377 /* Get all calls, and establish media connection between
2378 * this call and other calls.
2379 */
2380 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002381
Benny Prijono7ca96da2006-08-07 12:11:40 +00002382 for (i=0; i<call_cnt; ++i) {
2383 if (call_ids[i] == call_id)
2384 continue;
2385
2386 if (!pjsua_call_has_media(call_ids[i]))
2387 continue;
2388
2389 pjsua_conf_connect(call_info.conf_slot,
2390 pjsua_call_get_conf_port(call_ids[i]));
2391 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2392 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002393
2394 /* Automatically record conversation, if desired */
2395 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2396 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2397 app_config.rec_port);
2398 }
2399
Benny Prijono7ca96da2006-08-07 12:11:40 +00002400 }
2401
2402 /* Also connect call to local sound device */
2403 connect_sound = PJ_TRUE;
2404 }
2405
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002406 /* Otherwise connect to sound device */
2407 if (connect_sound) {
2408 pjsua_conf_connect(call_info.conf_slot, 0);
2409 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002410
2411 /* Automatically record conversation, if desired */
2412 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2413 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2414 pjsua_conf_connect(0, app_config.rec_port);
2415 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002416 }
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002417 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002418
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002419 /* Handle media status */
2420 switch (call_info.media_status) {
2421 case PJSUA_CALL_MEDIA_ACTIVE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002422 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002423 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002424
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002425 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002426 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2427 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002428 break;
2429
2430 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002431 PJ_LOG(3,(THIS_FILE,
2432 "Media for call %d is suspended (hold) by remote",
2433 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002434 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002435
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002436 case PJSUA_CALL_MEDIA_ERROR:
2437 PJ_LOG(3,(THIS_FILE,
Benny Prijono096c56c2007-09-15 08:30:16 +00002438 "Media has reported error, disconnecting call"));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002439 {
2440 pj_str_t reason = pj_str("ICE negotiation failed");
2441 pjsua_call_hangup(call_id, 500, &reason, NULL);
2442 }
2443 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002444
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002445 case PJSUA_CALL_MEDIA_NONE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002446 PJ_LOG(3,(THIS_FILE,
2447 "Media for call %d is inactive",
2448 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002449 break;
2450
2451 default:
2452 pj_assert(!"Unhandled media status");
2453 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002454 }
2455}
2456
Benny Prijono0875ae82006-12-26 00:11:48 +00002457/*
2458 * DTMF callback.
2459 */
2460static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2461{
2462 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2463}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002464
2465/*
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002466 * Redirection handler.
2467 */
Benny Prijono08a48b82008-11-27 12:42:07 +00002468static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
2469 const pjsip_uri *target,
2470 const pjsip_event *e)
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002471{
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002472 PJ_UNUSED_ARG(e);
2473
Benny Prijono08a48b82008-11-27 12:42:07 +00002474 if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002475 char uristr[PJSIP_MAX_URL_SIZE];
2476 int len;
2477
2478 len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
2479 sizeof(uristr));
2480 if (len < 1) {
2481 pj_ansi_strcpy(uristr, "--URI too long--");
2482 }
2483
2484 PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
2485 "Press 'Ra' to accept, 'Rr' to reject, or 'Rd' to "
2486 "disconnect.",
2487 call_id, len, uristr));
2488 }
Benny Prijono08a48b82008-11-27 12:42:07 +00002489
2490 return app_config.redir_op;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002491}
2492
2493/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002494 * Handler registration status has changed.
2495 */
2496static void on_reg_state(pjsua_acc_id acc_id)
2497{
2498 PJ_UNUSED_ARG(acc_id);
2499
2500 // Log already written.
2501}
2502
2503
2504/*
Benny Prijono563cd7a2008-08-19 20:02:15 +00002505 * Handler for incoming presence subscription request
2506 */
2507static void on_incoming_subscribe(pjsua_acc_id acc_id,
2508 pjsua_srv_pres *srv_pres,
2509 pjsua_buddy_id buddy_id,
2510 const pj_str_t *from,
2511 pjsip_rx_data *rdata,
2512 pjsip_status_code *code,
2513 pj_str_t *reason,
2514 pjsua_msg_data *msg_data)
2515{
2516 /* Just accept the request (the default behavior) */
2517 PJ_UNUSED_ARG(acc_id);
2518 PJ_UNUSED_ARG(srv_pres);
2519 PJ_UNUSED_ARG(buddy_id);
2520 PJ_UNUSED_ARG(from);
2521 PJ_UNUSED_ARG(rdata);
2522 PJ_UNUSED_ARG(code);
2523 PJ_UNUSED_ARG(reason);
2524 PJ_UNUSED_ARG(msg_data);
2525}
2526
2527
2528/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002529 * Handler on buddy state changed.
2530 */
2531static void on_buddy_state(pjsua_buddy_id buddy_id)
2532{
2533 pjsua_buddy_info info;
2534 pjsua_buddy_get_info(buddy_id, &info);
2535
2536 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2537 (int)info.uri.slen,
2538 info.uri.ptr,
2539 (int)info.status_text.slen,
2540 info.status_text.ptr));
2541}
2542
2543
2544/**
2545 * Incoming IM message (i.e. MESSAGE request)!
2546 */
2547static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2548 const pj_str_t *to, const pj_str_t *contact,
2549 const pj_str_t *mime_type, const pj_str_t *text)
2550{
2551 /* Note: call index may be -1 */
2552 PJ_UNUSED_ARG(call_id);
2553 PJ_UNUSED_ARG(to);
2554 PJ_UNUSED_ARG(contact);
2555 PJ_UNUSED_ARG(mime_type);
2556
Benny Prijonof4b538d2007-05-14 16:45:20 +00002557 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002558 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002559 (int)text->slen, text->ptr,
2560 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002561}
2562
2563
2564/**
2565 * Received typing indication
2566 */
2567static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2568 const pj_str_t *to, const pj_str_t *contact,
2569 pj_bool_t is_typing)
2570{
2571 PJ_UNUSED_ARG(call_id);
2572 PJ_UNUSED_ARG(to);
2573 PJ_UNUSED_ARG(contact);
2574
2575 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2576 (int)from->slen, from->ptr,
2577 (is_typing?"is typing..":"has stopped typing")));
2578}
2579
2580
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002581/**
2582 * Call transfer request status.
2583 */
2584static void on_call_transfer_status(pjsua_call_id call_id,
2585 int status_code,
2586 const pj_str_t *status_text,
2587 pj_bool_t final,
2588 pj_bool_t *p_cont)
2589{
2590 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2591 call_id, status_code,
2592 (int)status_text->slen, status_text->ptr,
2593 (final ? "[final]" : "")));
2594
2595 if (status_code/100 == 2) {
2596 PJ_LOG(3,(THIS_FILE,
2597 "Call %d: call transfered successfully, disconnecting call",
2598 call_id));
2599 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2600 *p_cont = PJ_FALSE;
2601 }
2602}
2603
2604
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002605/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002606 * Notification that call is being replaced.
2607 */
2608static void on_call_replaced(pjsua_call_id old_call_id,
2609 pjsua_call_id new_call_id)
2610{
2611 pjsua_call_info old_ci, new_ci;
2612
2613 pjsua_call_get_info(old_call_id, &old_ci);
2614 pjsua_call_get_info(new_call_id, &new_ci);
2615
2616 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2617 "call %d with %.*s",
2618 old_call_id,
2619 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2620 new_call_id,
2621 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2622}
2623
2624
2625/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002626 * NAT type detection callback.
2627 */
2628static void on_nat_detect(const pj_stun_nat_detect_result *res)
2629{
2630 if (res->status != PJ_SUCCESS) {
2631 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2632 } else {
2633 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2634 }
2635}
2636
2637
2638/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002639 * Print buddy list.
2640 */
2641static void print_buddy_list(void)
2642{
2643 pjsua_buddy_id ids[64];
2644 int i;
2645 unsigned count = PJ_ARRAY_SIZE(ids);
2646
2647 puts("Buddy list:");
2648
2649 pjsua_enum_buddies(ids, &count);
2650
2651 if (count == 0)
2652 puts(" -none-");
2653 else {
2654 for (i=0; i<(int)count; ++i) {
2655 pjsua_buddy_info info;
2656
2657 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2658 continue;
2659
Benny Prijono4461c7d2007-08-25 13:36:15 +00002660 printf(" [%2d] <%.*s> %.*s\n",
2661 ids[i]+1,
2662 (int)info.status_text.slen,
2663 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002664 (int)info.uri.slen,
2665 info.uri.ptr);
2666 }
2667 }
2668 puts("");
2669}
2670
2671
2672/*
2673 * Print account status.
2674 */
2675static void print_acc_status(int acc_id)
2676{
2677 char buf[80];
2678 pjsua_acc_info info;
2679
2680 pjsua_acc_get_info(acc_id, &info);
2681
2682 if (!info.has_registration) {
2683 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2684 (int)info.status_text.slen,
2685 info.status_text.ptr);
2686
2687 } else {
2688 pj_ansi_snprintf(buf, sizeof(buf),
2689 "%d/%.*s (expires=%d)",
2690 info.status,
2691 (int)info.status_text.slen,
2692 info.status_text.ptr,
2693 info.expires);
2694
2695 }
2696
2697 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2698 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002699 printf(" Online status: %.*s\n",
2700 (int)info.online_status_text.slen,
2701 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002702}
2703
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002704/* Playfile done notification, set timer to hangup calls */
2705pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2706{
2707 pj_time_val delay;
2708
2709 PJ_UNUSED_ARG(port);
2710 PJ_UNUSED_ARG(usr_data);
2711
2712 /* Just rewind WAV when it is played outside of call */
2713 if (pjsua_call_get_count() == 0) {
2714 pjsua_player_set_pos(app_config.wav_id, 0);
2715 return PJ_SUCCESS;
2716 }
2717
2718 /* Timer is already active */
2719 if (app_config.auto_hangup_timer.id == 1)
2720 return PJ_SUCCESS;
2721
2722 app_config.auto_hangup_timer.id = 1;
2723 delay.sec = 0;
2724 delay.msec = 200; /* Give 200 ms before hangup */
2725 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2726 &app_config.auto_hangup_timer,
2727 &delay);
2728
2729 return PJ_SUCCESS;
2730}
2731
2732/* Auto hangup timer callback */
2733static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2734 struct pj_timer_entry *entry)
2735{
2736 PJ_UNUSED_ARG(timer_heap);
2737 PJ_UNUSED_ARG(entry);
2738
2739 app_config.auto_hangup_timer.id = 0;
2740 pjsua_call_hangup_all();
2741}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002742
2743/*
2744 * Show a bit of help.
2745 */
2746static void keystroke_help(void)
2747{
2748 pjsua_acc_id acc_ids[16];
2749 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2750 int i;
2751
2752 printf(">>>>\n");
2753
2754 pjsua_enum_accs(acc_ids, &count);
2755
2756 printf("Account list:\n");
2757 for (i=0; i<(int)count; ++i)
2758 print_acc_status(acc_ids[i]);
2759
2760 print_buddy_list();
2761
2762 //puts("Commands:");
2763 puts("+=============================================================================+");
2764 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2765 puts("| | | |");
2766 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2767 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002768 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2769 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2770 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2771 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002772 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2773 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002774 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002775 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002776 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2777 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2778 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2779 puts("| | V Adjust audio Volume | f Save config |");
2780 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002781 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002782 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002783 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002784
2785 i = pjsua_call_get_count();
2786 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002787
2788 if (current_call != PJSUA_INVALID_ID) {
2789 pjsua_call_info ci;
2790 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2791 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2792 (int)ci.remote_info.slen, ci.remote_info.ptr,
2793 (int)ci.state_text.slen, ci.state_text.ptr);
2794 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002795}
2796
2797
2798/*
2799 * Input simple string
2800 */
2801static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2802{
2803 char *p;
2804
2805 printf("%s (empty to cancel): ", title); fflush(stdout);
Benny Prijono32d267b2009-01-01 22:08:21 +00002806 if (fgets(buf, len, stdin) == NULL)
2807 return PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002808
2809 /* Remove trailing newlines. */
2810 for (p=buf; ; ++p) {
2811 if (*p=='\r' || *p=='\n') *p='\0';
2812 else if (!*p) break;
2813 }
2814
2815 if (!*buf)
2816 return PJ_FALSE;
2817
2818 return PJ_TRUE;
2819}
2820
2821
2822#define NO_NB -2
2823struct input_result
2824{
2825 int nb_result;
2826 char *uri_result;
2827};
2828
2829
2830/*
2831 * Input URL.
2832 */
2833static void ui_input_url(const char *title, char *buf, int len,
2834 struct input_result *result)
2835{
2836 result->nb_result = NO_NB;
2837 result->uri_result = NULL;
2838
2839 print_buddy_list();
2840
2841 printf("Choices:\n"
2842 " 0 For current dialog.\n"
2843 " -1 All %d buddies in buddy list\n"
2844 " [1 -%2d] Select from buddy list\n"
2845 " URL An URL\n"
2846 " <Enter> Empty input (or 'q') to cancel\n"
2847 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2848 printf("%s: ", title);
2849
2850 fflush(stdout);
Benny Prijono32d267b2009-01-01 22:08:21 +00002851 if (fgets(buf, len, stdin) == NULL)
2852 return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002853 len = strlen(buf);
2854
2855 /* Left trim */
2856 while (pj_isspace(*buf)) {
2857 ++buf;
2858 --len;
2859 }
2860
2861 /* Remove trailing newlines */
2862 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2863 buf[--len] = '\0';
2864
2865 if (len == 0 || buf[0]=='q')
2866 return;
2867
2868 if (pj_isdigit(*buf) || *buf=='-') {
2869
2870 int i;
2871
2872 if (*buf=='-')
2873 i = 1;
2874 else
2875 i = 0;
2876
2877 for (; i<len; ++i) {
2878 if (!pj_isdigit(buf[i])) {
2879 puts("Invalid input");
2880 return;
2881 }
2882 }
2883
2884 result->nb_result = my_atoi(buf);
2885
2886 if (result->nb_result >= 0 &&
2887 result->nb_result <= (int)pjsua_get_buddy_count())
2888 {
2889 return;
2890 }
2891 if (result->nb_result == -1)
2892 return;
2893
2894 puts("Invalid input");
2895 result->nb_result = NO_NB;
2896 return;
2897
2898 } else {
2899 pj_status_t status;
2900
2901 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2902 pjsua_perror(THIS_FILE, "Invalid URL", status);
2903 return;
2904 }
2905
2906 result->uri_result = buf;
2907 }
2908}
2909
2910/*
2911 * List the ports in conference bridge
2912 */
2913static void conf_list(void)
2914{
2915 unsigned i, count;
2916 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2917
2918 printf("Conference ports:\n");
2919
2920 count = PJ_ARRAY_SIZE(id);
2921 pjsua_enum_conf_ports(id, &count);
2922
2923 for (i=0; i<count; ++i) {
2924 char txlist[PJSUA_MAX_CALLS*4+10];
2925 unsigned j;
2926 pjsua_conf_port_info info;
2927
2928 pjsua_conf_get_port_info(id[i], &info);
2929
2930 txlist[0] = '\0';
2931 for (j=0; j<info.listener_cnt; ++j) {
2932 char s[10];
2933 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2934 pj_ansi_strcat(txlist, s);
2935 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002936 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002937 info.slot_id,
2938 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002939 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002940 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002941 (int)info.name.slen,
2942 info.name.ptr,
2943 txlist);
2944
2945 }
2946 puts("");
2947}
2948
2949
2950/*
Benny Prijono56315612006-07-18 14:39:40 +00002951 * Send arbitrary request to remote host
2952 */
2953static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2954{
2955 pj_str_t str_method;
2956 pjsip_method method;
2957 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002958 pjsip_endpoint *endpt;
2959 pj_status_t status;
2960
2961 endpt = pjsua_get_pjsip_endpt();
2962
2963 str_method = pj_str(cstr_method);
2964 pjsip_method_init_np(&method, &str_method);
2965
Benny Prijonofff245c2007-04-02 11:44:47 +00002966 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002967
Benny Prijonob988d762007-12-05 04:30:04 +00002968 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002969 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002970 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002971 return;
2972 }
2973}
2974
2975
2976/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002977 * Change extended online status.
2978 */
2979static void change_online_status(void)
2980{
2981 char menuin[32];
2982 pj_bool_t online_status;
2983 pjrpid_element elem;
2984 int i, choice;
2985
2986 enum {
2987 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2988 };
2989
2990 struct opt {
2991 int id;
2992 char *name;
2993 } opts[] = {
2994 { AVAILABLE, "Available" },
2995 { BUSY, "Busy"},
2996 { OTP, "On the phone"},
2997 { IDLE, "Idle"},
2998 { AWAY, "Away"},
2999 { BRB, "Be right back"},
3000 { OFFLINE, "Offline"}
3001 };
3002
3003 printf("\n"
3004 "Choices:\n");
3005 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
3006 printf(" %d %s\n", opts[i].id+1, opts[i].name);
3007 }
3008
3009 if (!simple_input("Select status", menuin, sizeof(menuin)))
3010 return;
3011
3012 choice = atoi(menuin) - 1;
3013 if (choice < 0 || choice >= OPT_MAX) {
3014 puts("Invalid selection");
3015 return;
3016 }
3017
3018 pj_bzero(&elem, sizeof(elem));
3019 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
3020
3021 online_status = PJ_TRUE;
3022
3023 switch (choice) {
3024 case AVAILABLE:
3025 break;
3026 case BUSY:
3027 elem.activity = PJRPID_ACTIVITY_BUSY;
3028 elem.note = pj_str("Busy");
3029 break;
3030 case OTP:
3031 elem.activity = PJRPID_ACTIVITY_BUSY;
3032 elem.note = pj_str("On the phone");
3033 break;
3034 case IDLE:
3035 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
3036 elem.note = pj_str("Idle");
3037 break;
3038 case AWAY:
3039 elem.activity = PJRPID_ACTIVITY_AWAY;
3040 elem.note = pj_str("Away");
3041 break;
3042 case BRB:
3043 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
3044 elem.note = pj_str("Be right back");
3045 break;
3046 case OFFLINE:
3047 online_status = PJ_FALSE;
3048 break;
3049 }
3050
3051 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
3052}
3053
3054
3055/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00003056 * Change codec priorities.
3057 */
3058static void manage_codec_prio(void)
3059{
3060 pjsua_codec_info c[32];
3061 unsigned i, count = PJ_ARRAY_SIZE(c);
3062 char input[32];
3063 char *codec, *prio;
3064 pj_str_t id;
3065 int new_prio;
3066 pj_status_t status;
3067
3068 printf("List of codecs:\n");
3069
3070 pjsua_enum_codecs(c, &count);
3071 for (i=0; i<count; ++i) {
3072 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
3073 c[i].codec_id.ptr);
3074 }
3075
3076 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00003077 puts("Enter codec id and its new priority "
3078 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00003079
Benny Prijono88accae2008-06-26 15:48:14 +00003080 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijono32d267b2009-01-01 22:08:21 +00003081 if (fgets(input, sizeof(input), stdin) == NULL)
3082 return;
Benny Prijonoc08682e2007-10-04 06:17:58 +00003083 if (input[0]=='\r' || input[0]=='\n') {
3084 puts("Done");
3085 return;
3086 }
3087
3088 codec = strtok(input, " \t\r\n");
3089 prio = strtok(NULL, " \r\n");
3090
3091 if (!codec || !prio) {
3092 puts("Invalid input");
3093 return;
3094 }
3095
3096 new_prio = atoi(prio);
3097 if (new_prio < 0)
3098 new_prio = 0;
3099 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
3100 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
3101
3102 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
3103 (pj_uint8_t)new_prio);
3104 if (status != PJ_SUCCESS)
3105 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
3106}
3107
3108
3109/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003110 * Main "user interface" loop.
3111 */
3112void console_app_main(const pj_str_t *uri_to_call)
3113{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003114 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003115 char buf[128];
3116 char text[128];
3117 int i, count;
3118 char *uri;
3119 pj_str_t tmp;
3120 struct input_result result;
3121 pjsua_call_info call_info;
3122 pjsua_acc_info acc_info;
3123
3124
3125 /* If user specifies URI to call, then call the URI */
3126 if (uri_to_call->slen) {
3127 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
3128 }
3129
3130 keystroke_help();
3131
3132 for (;;) {
3133
3134 printf(">>> ");
3135 fflush(stdout);
3136
Benny Prijono990042e2007-01-21 19:36:00 +00003137 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
3138 /*
3139 * Be friendly to users who redirect commands into
3140 * program, when file ends, resume with kbd.
3141 * If exit is desired end script with q for quit
3142 */
3143 /* Reopen stdin/stdout/stderr to /dev/console */
3144#if defined(PJ_WIN32) && PJ_WIN32!=0
3145 if (freopen ("CONIN$", "r", stdin) == NULL) {
3146#else
3147 if (1) {
3148#endif
3149 puts("Cannot switch back to console from file redirection");
3150 menuin[0] = 'q';
3151 menuin[1] = '\0';
3152 } else {
3153 puts("Switched back to console from file redirection");
3154 continue;
3155 }
3156 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003157
Benny Prijonoebc32c32008-06-12 13:30:39 +00003158 if (cmd_echo) {
3159 printf("%s", menuin);
3160 }
3161
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003162 switch (menuin[0]) {
3163
3164 case 'm':
3165 /* Make call! : */
3166 printf("(You currently have %d calls)\n",
3167 pjsua_call_get_count());
3168
3169 uri = NULL;
3170 ui_input_url("Make call", buf, sizeof(buf), &result);
3171 if (result.nb_result != NO_NB) {
3172
3173 if (result.nb_result == -1 || result.nb_result == 0) {
3174 puts("You can't do that with make call!");
3175 continue;
3176 } else {
3177 pjsua_buddy_info binfo;
3178 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003179 tmp.ptr = buf;
3180 pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003181 }
3182
3183 } else if (result.uri_result) {
Benny Prijono45f2f352009-04-02 12:15:23 +00003184 tmp = pj_str(result.uri_result);
3185 } else {
3186 tmp.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003187 }
3188
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003189 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
3190 break;
3191
3192 case 'M':
3193 /* Make multiple calls! : */
3194 printf("(You currently have %d calls)\n",
3195 pjsua_call_get_count());
3196
3197 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
3198 continue;
3199
3200 count = my_atoi(menuin);
3201 if (count < 1)
3202 continue;
3203
3204 ui_input_url("Make call", buf, sizeof(buf), &result);
3205 if (result.nb_result != NO_NB) {
3206 pjsua_buddy_info binfo;
3207 if (result.nb_result == -1 || result.nb_result == 0) {
3208 puts("You can't do that with make call!");
3209 continue;
3210 }
3211 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003212 tmp.ptr = buf;
3213 pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003214 } else {
Benny Prijono45f2f352009-04-02 12:15:23 +00003215 tmp = pj_str(result.uri_result);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003216 }
3217
3218 for (i=0; i<my_atoi(menuin); ++i) {
3219 pj_status_t status;
3220
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003221 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3222 NULL, NULL);
3223 if (status != PJ_SUCCESS)
3224 break;
3225 }
3226 break;
3227
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003228 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003229 i = pjsua_detect_nat_type();
3230 if (i != PJ_SUCCESS)
3231 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003232 break;
3233
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003234 case 'i':
3235 /* Send instant messaeg */
3236
3237 /* i is for call index to send message, if any */
3238 i = -1;
3239
3240 /* Make compiler happy. */
3241 uri = NULL;
3242
3243 /* Input destination. */
3244 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3245 if (result.nb_result != NO_NB) {
3246
3247 if (result.nb_result == -1) {
3248 puts("You can't send broadcast IM like that!");
3249 continue;
3250
3251 } else if (result.nb_result == 0) {
3252
3253 i = current_call;
3254
3255 } else {
3256 pjsua_buddy_info binfo;
3257 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003258 tmp.ptr = buf;
3259 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
3260 uri = buf;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003261 }
3262
3263 } else if (result.uri_result) {
3264 uri = result.uri_result;
3265 }
3266
3267
3268 /* Send typing indication. */
3269 if (i != -1)
3270 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3271 else {
3272 pj_str_t tmp_uri = pj_str(uri);
3273 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3274 }
3275
3276 /* Input the IM . */
3277 if (!simple_input("Message", text, sizeof(text))) {
3278 /*
3279 * Cancelled.
3280 * Send typing notification too, saying we're not typing.
3281 */
3282 if (i != -1)
3283 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3284 else {
3285 pj_str_t tmp_uri = pj_str(uri);
3286 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3287 }
3288 continue;
3289 }
3290
3291 tmp = pj_str(text);
3292
3293 /* Send the IM */
3294 if (i != -1)
3295 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3296 else {
3297 pj_str_t tmp_uri = pj_str(uri);
3298 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3299 }
3300
3301 break;
3302
3303 case 'a':
3304
3305 if (current_call != -1) {
3306 pjsua_call_get_info(current_call, &call_info);
3307 } else {
3308 /* Make compiler happy */
3309 call_info.role = PJSIP_ROLE_UAC;
3310 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3311 }
3312
3313 if (current_call == -1 ||
3314 call_info.role != PJSIP_ROLE_UAS ||
3315 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3316 {
3317 puts("No pending incoming call");
3318 fflush(stdout);
3319 continue;
3320
3321 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003322 int st_code;
3323 char contact[120];
3324 pj_str_t hname = { "Contact", 7 };
3325 pj_str_t hvalue;
3326 pjsip_generic_string_hdr hcontact;
3327 pjsua_msg_data msg_data;
3328
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003329 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3330 continue;
3331
Benny Prijono20d36722007-02-22 14:52:24 +00003332 st_code = my_atoi(buf);
3333 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003334 continue;
3335
Benny Prijono20d36722007-02-22 14:52:24 +00003336 pjsua_msg_data_init(&msg_data);
3337
3338 if (st_code/100 == 3) {
3339 if (!simple_input("Enter URL to be put in Contact",
3340 contact, sizeof(contact)))
3341 continue;
3342 hvalue = pj_str(contact);
3343 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3344
3345 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3346 }
3347
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003348 /*
3349 * Must check again!
3350 * Call may have been disconnected while we're waiting for
3351 * keyboard input.
3352 */
3353 if (current_call == -1) {
3354 puts("Call has been disconnected");
3355 fflush(stdout);
3356 continue;
3357 }
3358
Benny Prijono20d36722007-02-22 14:52:24 +00003359 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003360 }
3361
3362 break;
3363
3364
3365 case 'h':
3366
3367 if (current_call == -1) {
3368 puts("No current call");
3369 fflush(stdout);
3370 continue;
3371
3372 } else if (menuin[1] == 'a') {
3373
3374 /* Hangup all calls */
3375 pjsua_call_hangup_all();
3376
3377 } else {
3378
3379 /* Hangup current calls */
3380 pjsua_call_hangup(current_call, 0, NULL, NULL);
3381 }
3382 break;
3383
3384 case ']':
3385 case '[':
3386 /*
3387 * Cycle next/prev dialog.
3388 */
3389 if (menuin[0] == ']') {
3390 find_next_call();
3391
3392 } else {
3393 find_prev_call();
3394 }
3395
3396 if (current_call != -1) {
3397
3398 pjsua_call_get_info(current_call, &call_info);
3399 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3400 (int)call_info.remote_info.slen,
3401 call_info.remote_info.ptr));
3402
3403 } else {
3404 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3405 }
3406 break;
3407
3408
3409 case '>':
3410 case '<':
3411 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3412 break;
3413
3414 i = my_atoi(buf);
3415 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003416 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003417 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3418 } else {
3419 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3420 }
3421 break;
3422
3423
3424 case '+':
3425 if (menuin[1] == 'b') {
3426
3427 pjsua_buddy_config buddy_cfg;
3428 pjsua_buddy_id buddy_id;
3429 pj_status_t status;
3430
3431 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3432 break;
3433
3434 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3435 printf("Invalid SIP URI '%s'\n", buf);
3436 break;
3437 }
3438
Benny Prijonoac623b32006-07-03 15:19:31 +00003439 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003440
3441 buddy_cfg.uri = pj_str(buf);
3442 buddy_cfg.subscribe = PJ_TRUE;
3443
3444 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3445 if (status == PJ_SUCCESS) {
3446 printf("New buddy '%s' added at index %d\n",
3447 buf, buddy_id+1);
3448 }
3449
3450 } else if (menuin[1] == 'a') {
3451
Benny Prijonofb2b3652007-06-28 07:15:03 +00003452 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3453 pjsua_acc_config acc_cfg;
3454 pj_status_t status;
3455
3456 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3457 break;
3458 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3459 break;
3460 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3461 break;
3462 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3463 break;
3464 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3465 break;
3466
3467 pjsua_acc_config_default(&acc_cfg);
3468 acc_cfg.id = pj_str(id);
3469 acc_cfg.reg_uri = pj_str(registrar);
3470 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003471 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003472 acc_cfg.cred_info[0].realm = pj_str(realm);
3473 acc_cfg.cred_info[0].username = pj_str(uname);
3474 acc_cfg.cred_info[0].data_type = 0;
3475 acc_cfg.cred_info[0].data = pj_str(passwd);
3476
3477 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3478 if (status != PJ_SUCCESS) {
3479 pjsua_perror(THIS_FILE, "Error adding new account", status);
3480 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003481
3482 } else {
3483 printf("Invalid input %s\n", menuin);
3484 }
3485 break;
3486
3487 case '-':
3488 if (menuin[1] == 'b') {
3489 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3490 break;
3491
3492 i = my_atoi(buf) - 1;
3493
3494 if (!pjsua_buddy_is_valid(i)) {
3495 printf("Invalid buddy id %d\n", i);
3496 } else {
3497 pjsua_buddy_del(i);
3498 printf("Buddy %d deleted\n", i);
3499 }
3500
3501 } else if (menuin[1] == 'a') {
3502
3503 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3504 break;
3505
3506 i = my_atoi(buf);
3507
3508 if (!pjsua_acc_is_valid(i)) {
3509 printf("Invalid account id %d\n", i);
3510 } else {
3511 pjsua_acc_del(i);
3512 printf("Account %d deleted\n", i);
3513 }
3514
3515 } else {
3516 printf("Invalid input %s\n", menuin);
3517 }
3518 break;
3519
3520 case 'H':
3521 /*
3522 * Hold call.
3523 */
3524 if (current_call != -1) {
3525
3526 pjsua_call_set_hold(current_call, NULL);
3527
3528 } else {
3529 PJ_LOG(3,(THIS_FILE, "No current call"));
3530 }
3531 break;
3532
3533 case 'v':
3534 /*
3535 * Send re-INVITE (to release hold, etc).
3536 */
3537 if (current_call != -1) {
3538
3539 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3540
3541 } else {
3542 PJ_LOG(3,(THIS_FILE, "No current call"));
3543 }
3544 break;
3545
Benny Prijonoc08682e2007-10-04 06:17:58 +00003546 case 'U':
3547 /*
3548 * Send UPDATE
3549 */
3550 if (current_call != -1) {
3551
3552 pjsua_call_update(current_call, 0, NULL);
3553
3554 } else {
3555 PJ_LOG(3,(THIS_FILE, "No current call"));
3556 }
3557 break;
3558
3559 case 'C':
3560 if (menuin[1] == 'p') {
3561 manage_codec_prio();
3562 }
3563 break;
3564
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003565 case 'x':
3566 /*
3567 * Transfer call.
3568 */
3569 if (current_call == -1) {
3570
3571 PJ_LOG(3,(THIS_FILE, "No current call"));
3572
3573 } else {
3574 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003575 pjsua_msg_data msg_data;
3576 pjsip_generic_string_hdr refer_sub;
3577 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3578 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003579 pjsua_call_info ci;
3580
3581 pjsua_call_get_info(current_call, &ci);
3582 printf("Transfering current call [%d] %.*s\n",
3583 current_call,
3584 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003585
3586 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3587
3588 /* Check if call is still there. */
3589
3590 if (call != current_call) {
3591 puts("Call has been disconnected");
3592 continue;
3593 }
3594
Benny Prijonod524e822006-09-22 12:48:18 +00003595 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003596 if (app_config.no_refersub) {
3597 /* Add Refer-Sub: false in outgoing REFER request */
3598 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3599 &STR_FALSE);
3600 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3601 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003602 if (result.nb_result != NO_NB) {
3603 if (result.nb_result == -1 || result.nb_result == 0)
3604 puts("You can't do that with transfer call!");
3605 else {
3606 pjsua_buddy_info binfo;
3607 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003608 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003609 }
3610
3611 } else if (result.uri_result) {
3612 pj_str_t tmp;
3613 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003614 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003615 }
3616 }
3617 break;
3618
Benny Prijonof7b1c392006-11-11 16:46:34 +00003619 case 'X':
3620 /*
3621 * Transfer call with replaces.
3622 */
3623 if (current_call == -1) {
3624
3625 PJ_LOG(3,(THIS_FILE, "No current call"));
3626
3627 } else {
3628 int call = current_call;
3629 int dst_call;
3630 pjsua_msg_data msg_data;
3631 pjsip_generic_string_hdr refer_sub;
3632 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3633 pj_str_t STR_FALSE = { "false", 5 };
3634 pjsua_call_id ids[PJSUA_MAX_CALLS];
3635 pjsua_call_info ci;
3636 unsigned i, count;
3637
3638 count = PJ_ARRAY_SIZE(ids);
3639 pjsua_enum_calls(ids, &count);
3640
3641 if (count <= 1) {
3642 puts("There are no other calls");
3643 continue;
3644 }
3645
3646 pjsua_call_get_info(current_call, &ci);
3647 printf("Transfer call [%d] %.*s to one of the following:\n",
3648 current_call,
3649 (int)ci.remote_info.slen, ci.remote_info.ptr);
3650
3651 for (i=0; i<count; ++i) {
3652 pjsua_call_info call_info;
3653
3654 if (ids[i] == call)
3655 continue;
3656
3657 pjsua_call_get_info(ids[i], &call_info);
3658 printf("%d %.*s [%.*s]\n",
3659 ids[i],
3660 (int)call_info.remote_info.slen,
3661 call_info.remote_info.ptr,
3662 (int)call_info.state_text.slen,
3663 call_info.state_text.ptr);
3664 }
3665
3666 if (!simple_input("Enter call number to be replaced",
3667 buf, sizeof(buf)))
3668 continue;
3669
3670 dst_call = my_atoi(buf);
3671
3672 /* Check if call is still there. */
3673
3674 if (call != current_call) {
3675 puts("Call has been disconnected");
3676 continue;
3677 }
3678
3679 /* Check that destination call is valid. */
3680 if (dst_call == call) {
3681 puts("Destination call number must not be the same "
3682 "as the call being transfered");
3683 continue;
3684 }
3685 if (dst_call >= PJSUA_MAX_CALLS) {
3686 puts("Invalid destination call number");
3687 continue;
3688 }
3689 if (!pjsua_call_is_active(dst_call)) {
3690 puts("Invalid destination call number");
3691 continue;
3692 }
3693
3694 pjsua_msg_data_init(&msg_data);
3695 if (app_config.no_refersub) {
3696 /* Add Refer-Sub: false in outgoing REFER request */
3697 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3698 &STR_FALSE);
3699 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3700 }
3701
Benny Prijono329d6382009-05-29 13:04:03 +00003702 pjsua_call_xfer_replaces(call, dst_call,
3703 PJSUA_XFER_NO_REQUIRE_REPLACES,
3704 &msg_data);
Benny Prijonof7b1c392006-11-11 16:46:34 +00003705 }
3706 break;
3707
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003708 case '#':
3709 /*
3710 * Send DTMF strings.
3711 */
3712 if (current_call == -1) {
3713
3714 PJ_LOG(3,(THIS_FILE, "No current call"));
3715
3716 } else if (!pjsua_call_has_media(current_call)) {
3717
3718 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3719
3720 } else {
3721 pj_str_t digits;
3722 int call = current_call;
3723 pj_status_t status;
3724
3725 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3726 sizeof(buf)))
3727 {
3728 break;
3729 }
3730
3731 if (call != current_call) {
3732 puts("Call has been disconnected");
3733 continue;
3734 }
3735
3736 digits = pj_str(buf);
3737 status = pjsua_call_dial_dtmf(current_call, &digits);
3738 if (status != PJ_SUCCESS) {
3739 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3740 } else {
3741 puts("DTMF digits enqueued for transmission");
3742 }
3743 }
3744 break;
3745
Benny Prijonofeb69f42007-10-05 09:12:26 +00003746 case '*':
3747 /* Send DTMF with INFO */
3748 if (current_call == -1) {
3749
3750 PJ_LOG(3,(THIS_FILE, "No current call"));
3751
3752 } else {
3753 const pj_str_t SIP_INFO = pj_str("INFO");
3754 pj_str_t digits;
3755 int call = current_call;
3756 int i;
3757 pj_status_t status;
3758
3759 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3760 sizeof(buf)))
3761 {
3762 break;
3763 }
3764
3765 if (call != current_call) {
3766 puts("Call has been disconnected");
3767 continue;
3768 }
3769
3770 digits = pj_str(buf);
3771 for (i=0; i<digits.slen; ++i) {
3772 pjsua_msg_data msg_data;
3773 char body[80];
3774
3775 pjsua_msg_data_init(&msg_data);
3776 msg_data.content_type = pj_str("application/dtmf-relay");
3777
3778 pj_ansi_snprintf(body, sizeof(body),
3779 "Signal=%c\r\n"
3780 "Duration=160",
3781 buf[i]);
3782 msg_data.msg_body = pj_str(body);
3783
3784 status = pjsua_call_send_request(current_call, &SIP_INFO,
3785 &msg_data);
3786 if (status != PJ_SUCCESS) {
3787 break;
3788 }
3789 }
3790 }
3791 break;
3792
Benny Prijono56315612006-07-18 14:39:40 +00003793 case 'S':
3794 /*
3795 * Send arbitrary request
3796 */
3797 if (pjsua_acc_get_count() == 0) {
3798 puts("Sorry, need at least one account configured");
3799 break;
3800 }
3801
3802 puts("Send arbitrary request to remote host");
3803
3804 /* Input METHOD */
3805 if (!simple_input("Request method:",text,sizeof(text)))
3806 break;
3807
3808 /* Input destination URI */
3809 uri = NULL;
3810 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3811 if (result.nb_result != NO_NB) {
3812
Benny Prijono7efa2d62009-04-27 12:50:16 +00003813 if (result.nb_result == -1) {
Benny Prijono56315612006-07-18 14:39:40 +00003814 puts("Sorry you can't do that!");
3815 continue;
Benny Prijono7efa2d62009-04-27 12:50:16 +00003816 } else if (result.nb_result == 0) {
3817 uri = NULL;
3818 if (current_call == PJSUA_INVALID_ID) {
3819 puts("No current call");
3820 continue;
3821 }
Benny Prijono56315612006-07-18 14:39:40 +00003822 } else {
3823 pjsua_buddy_info binfo;
3824 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003825 tmp.ptr = buf;
3826 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
3827 uri = buf;
Benny Prijono56315612006-07-18 14:39:40 +00003828 }
3829
3830 } else if (result.uri_result) {
3831 uri = result.uri_result;
3832 }
3833
Benny Prijono7efa2d62009-04-27 12:50:16 +00003834 if (uri) {
3835 tmp = pj_str(uri);
3836 send_request(text, &tmp);
3837 } else {
3838 /* If you send call control request using this method
3839 * (such requests includes BYE, CANCEL, etc.), it will
3840 * not go well with the call state, so don't do it
3841 * unless it's for testing.
3842 */
3843 pj_str_t method = pj_str(text);
3844 pjsua_call_send_request(current_call, &method, NULL);
3845 }
Benny Prijono56315612006-07-18 14:39:40 +00003846 break;
3847
Benny Prijonoebc32c32008-06-12 13:30:39 +00003848 case 'e':
3849 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3850 pj_str_t tmp;
3851
3852 tmp.ptr = menuin+5;
3853 tmp.slen = pj_ansi_strlen(menuin)-6;
3854
3855 if (tmp.slen < 1) {
3856 puts("Usage: echo [0|1]");
3857 break;
3858 }
3859
Benny Prijonoddd02de2008-06-26 22:20:11 +00003860 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003861 }
3862 break;
3863
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003864 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003865 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3866 pj_str_t tmp;
3867 int delay;
3868
3869 tmp.ptr = menuin+6;
3870 tmp.slen = pj_ansi_strlen(menuin)-7;
3871
3872 if (tmp.slen < 1) {
3873 puts("Usage: sleep MSEC");
3874 break;
3875 }
3876
3877 delay = pj_strtoul(&tmp);
3878 if (delay < 0) delay = 0;
3879 pj_thread_sleep(delay);
3880 break;
3881 }
3882 /* Continue below */
3883
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003884 case 'u':
3885 /*
3886 * Subscribe/unsubscribe presence.
3887 */
3888 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3889 if (result.nb_result != NO_NB) {
3890 if (result.nb_result == -1) {
3891 int i, count;
3892 count = pjsua_get_buddy_count();
3893 for (i=0; i<count; ++i)
3894 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3895 } else if (result.nb_result == 0) {
3896 puts("Sorry, can only subscribe to buddy's presence, "
3897 "not from existing call");
3898 } else {
3899 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3900 }
3901
3902 } else if (result.uri_result) {
3903 puts("Sorry, can only subscribe to buddy's presence, "
3904 "not arbitrary URL (for now)");
3905 }
3906
3907 break;
3908
3909 case 'r':
3910 switch (menuin[1]) {
3911 case 'r':
3912 /*
3913 * Re-Register.
3914 */
3915 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3916 break;
3917 case 'u':
3918 /*
3919 * Unregister
3920 */
3921 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3922 break;
3923 }
3924 break;
3925
3926 case 't':
3927 pjsua_acc_get_info(current_acc, &acc_info);
3928 acc_info.online_status = !acc_info.online_status;
3929 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3930 printf("Setting %s online status to %s\n",
3931 acc_info.acc_uri.ptr,
3932 (acc_info.online_status?"online":"offline"));
3933 break;
3934
Benny Prijono4461c7d2007-08-25 13:36:15 +00003935 case 'T':
3936 change_online_status();
3937 break;
3938
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003939 case 'c':
3940 switch (menuin[1]) {
3941 case 'l':
3942 conf_list();
3943 break;
3944 case 'c':
3945 case 'd':
3946 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003947 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003948 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003949 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003950 const char *src_title, *dst_title;
3951
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003952 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003953
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003954 if (cnt != 3) {
3955 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003956
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003957 src_title = (menuin[1]=='c'?
3958 "Connect src port #":
3959 "Disconnect src port #");
3960 dst_title = (menuin[1]=='c'?
3961 "To dst port #":
3962 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003963
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003964 if (!simple_input(src_title, src_port, sizeof(src_port)))
3965 break;
3966
3967 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3968 break;
3969 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003970
3971 if (menuin[1]=='c') {
3972 status = pjsua_conf_connect(my_atoi(src_port),
3973 my_atoi(dst_port));
3974 } else {
3975 status = pjsua_conf_disconnect(my_atoi(src_port),
3976 my_atoi(dst_port));
3977 }
3978 if (status == PJ_SUCCESS) {
3979 puts("Success");
3980 } else {
3981 puts("ERROR!!");
3982 }
3983 }
3984 break;
3985 }
3986 break;
3987
Benny Prijono6dd967c2006-12-26 02:27:14 +00003988 case 'V':
3989 /* Adjust audio volume */
3990 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3991 if (simple_input(buf,text,sizeof(text))) {
3992 char *err;
3993 app_config.mic_level = (float)strtod(text, &err);
3994 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3995 }
3996 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3997 app_config.speaker_level);
3998 if (simple_input(buf,text,sizeof(text))) {
3999 char *err;
4000 app_config.speaker_level = (float)strtod(text, &err);
4001 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
4002 }
4003
4004 break;
4005
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004006 case 'd':
4007 if (menuin[1] == 'c') {
4008 char settings[2000];
4009 int len;
4010
4011 len = write_settings(&app_config, settings, sizeof(settings));
4012 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00004013 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004014 else
4015 PJ_LOG(3,(THIS_FILE,
4016 "Dumping configuration (%d bytes):\n%s\n",
4017 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00004018
4019 } else if (menuin[1] == 'q') {
4020
4021 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00004022 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00004023 } else {
4024 PJ_LOG(3,(THIS_FILE, "No current call"));
4025 }
4026
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004027 } else {
4028 app_dump(menuin[1]=='d');
4029 }
4030 break;
4031
4032
4033 case 'f':
4034 if (simple_input("Enter output filename", buf, sizeof(buf))) {
4035 char settings[2000];
4036 int len;
4037
4038 len = write_settings(&app_config, settings, sizeof(settings));
4039 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00004040 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004041 else {
4042 pj_oshandle_t fd;
4043 pj_status_t status;
4044
4045 status = pj_file_open(app_config.pool, buf,
4046 PJ_O_WRONLY, &fd);
4047 if (status != PJ_SUCCESS) {
4048 pjsua_perror(THIS_FILE, "Unable to open file", status);
4049 } else {
4050 pj_ssize_t size = len;
4051 pj_file_write(fd, settings, &size);
4052 pj_file_close(fd);
4053
4054 printf("Settings successfully written to '%s'\n", buf);
4055 }
4056 }
4057
4058 }
4059 break;
4060
4061
4062 case 'q':
4063 goto on_exit;
4064
4065
Benny Prijono5e51a4e2008-11-27 00:06:46 +00004066 case 'R':
4067 if (!pjsua_call_is_active(current_call)) {
4068 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
4069 } else if (menuin[1] == 'a') {
4070 pjsua_call_process_redirect(current_call,
4071 PJSIP_REDIRECT_ACCEPT);
4072 } else if (menuin[1] == 'r') {
4073 pjsua_call_process_redirect(current_call,
4074 PJSIP_REDIRECT_REJECT);
4075 } else {
4076 pjsua_call_process_redirect(current_call,
4077 PJSIP_REDIRECT_STOP);
4078 }
4079 break;
4080
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004081 default:
4082 if (menuin[0] != '\n' && menuin[0] != '\r') {
4083 printf("Invalid input %s", menuin);
4084 }
4085 keystroke_help();
4086 break;
4087 }
4088 }
4089
4090on_exit:
4091 ;
4092}
4093
Benny Prijonod9e8dfe2009-03-12 11:25:11 +00004094/*****************************************************************************
4095 * A simple module to handle otherwise unhandled request. We will register
4096 * this with the lowest priority.
4097 */
4098
4099/* Notification on incoming request */
4100static pj_bool_t default_mod_on_rx_request(pjsip_rx_data *rdata)
4101{
4102 pjsip_tx_data *tdata;
4103 pjsip_status_code status_code;
4104 pj_status_t status;
4105
4106 /* Don't respond to ACK! */
4107 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
4108 &pjsip_ack_method) == 0)
4109 return PJ_TRUE;
4110
4111 /* Create basic response. */
4112 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
4113 &pjsip_notify_method) == 0)
4114 {
4115 /* Unsolicited NOTIFY's, send with Bad Request */
4116 status_code = PJSIP_SC_BAD_REQUEST;
4117 } else {
4118 /* Probably unknown method */
4119 status_code = PJSIP_SC_METHOD_NOT_ALLOWED;
4120 }
4121 status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
4122 rdata, status_code,
4123 NULL, &tdata);
4124 if (status != PJ_SUCCESS) {
4125 pjsua_perror(THIS_FILE, "Unable to create response", status);
4126 return PJ_TRUE;
4127 }
4128
4129 /* Add Allow if we're responding with 405 */
4130 if (status_code == PJSIP_SC_METHOD_NOT_ALLOWED) {
4131 const pjsip_hdr *cap_hdr;
4132 cap_hdr = pjsip_endpt_get_capability(pjsua_get_pjsip_endpt(),
4133 PJSIP_H_ALLOW, NULL);
4134 if (cap_hdr) {
4135 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool,
4136 cap_hdr));
4137 }
4138 }
4139
4140 /* Add User-Agent header */
4141 {
4142 pj_str_t user_agent;
4143 char tmp[80];
4144 const pj_str_t USER_AGENT = { "User-Agent", 10};
4145 pjsip_hdr *h;
4146
4147 pj_ansi_snprintf(tmp, sizeof(tmp), "PJSUA v%s/%s",
4148 pj_get_version(), PJ_OS_NAME);
4149 pj_strdup2_with_null(tdata->pool, &user_agent, tmp);
4150
4151 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
4152 &USER_AGENT,
4153 &user_agent);
4154 pjsip_msg_add_hdr(tdata->msg, h);
4155 }
4156
4157 pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(), rdata, tdata,
4158 NULL, NULL);
4159
4160 return PJ_TRUE;
4161}
4162
4163
4164/* The module instance. */
4165static pjsip_module mod_default_handler =
4166{
4167 NULL, NULL, /* prev, next. */
4168 { "mod-default-handler", 19 }, /* Name. */
4169 -1, /* Id */
4170 PJSIP_MOD_PRIORITY_APPLICATION+99, /* Priority */
4171 NULL, /* load() */
4172 NULL, /* start() */
4173 NULL, /* stop() */
4174 NULL, /* unload() */
4175 &default_mod_on_rx_request, /* on_rx_request() */
4176 NULL, /* on_rx_response() */
4177 NULL, /* on_tx_request. */
4178 NULL, /* on_tx_response() */
4179 NULL, /* on_tsx_state() */
4180
4181};
4182
4183
4184
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004185
4186/*****************************************************************************
4187 * Public API
4188 */
4189
4190pj_status_t app_init(int argc, char *argv[])
4191{
Benny Prijonoe93e2872006-06-28 16:46:49 +00004192 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00004193 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004194 unsigned i;
4195 pj_status_t status;
4196
4197 /* Create pjsua */
4198 status = pjsua_create();
4199 if (status != PJ_SUCCESS)
4200 return status;
4201
4202 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00004203 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004204
4205 /* Initialize default config */
4206 default_config(&app_config);
4207
4208 /* Parse the arguments */
4209 status = parse_args(argc, argv, &app_config, &uri_arg);
4210 if (status != PJ_SUCCESS)
4211 return status;
4212
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004213 /* Initialize application callbacks */
4214 app_config.cfg.cb.on_call_state = &on_call_state;
4215 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
4216 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00004217 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00004218 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00004219 app_config.cfg.cb.on_call_redirected = &call_on_redirected;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004220 app_config.cfg.cb.on_reg_state = &on_reg_state;
Benny Prijono563cd7a2008-08-19 20:02:15 +00004221 app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004222 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
4223 app_config.cfg.cb.on_pager = &on_pager;
4224 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00004225 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00004226 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00004227 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004228
Benny Prijono10454dc2009-02-21 14:21:59 +00004229 /* Set sound device latency */
4230 if (app_config.capture_lat > 0)
4231 app_config.media_cfg.snd_rec_latency = app_config.capture_lat;
4232 if (app_config.playback_lat)
4233 app_config.media_cfg.snd_play_latency = app_config.playback_lat;
4234
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004235 /* Initialize pjsua */
4236 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
4237 &app_config.media_cfg);
4238 if (status != PJ_SUCCESS)
4239 return status;
4240
Benny Prijonod9e8dfe2009-03-12 11:25:11 +00004241 /* Initialize our module to handle otherwise unhandled request */
4242 status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
4243 &mod_default_handler);
4244 if (status != PJ_SUCCESS)
4245 return status;
4246
Benny Prijonoe909eac2006-07-27 22:04:56 +00004247#ifdef STEREO_DEMO
4248 stereo_demo();
4249#endif
4250
Benny Prijono804ff0a2006-09-14 11:17:48 +00004251 /* Initialize calls data */
4252 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
4253 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
4254 app_config.call_data[i].timer.cb = &call_timeout_callback;
4255 }
4256
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004257 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00004258 for (i=0; i<app_config.wav_count; ++i) {
4259 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004260 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00004261
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004262 if (app_config.auto_play_hangup)
4263 play_options |= PJMEDIA_FILE_NO_LOOP;
4264
4265 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00004266 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004267 if (status != PJ_SUCCESS)
4268 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00004269
Benny Prijono72c04d22007-02-17 22:20:58 +00004270 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00004271 app_config.wav_id = wav_id;
4272 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004273 if (app_config.auto_play_hangup) {
4274 pjmedia_port *port;
4275
4276 pjsua_player_get_port(app_config.wav_id, &port);
4277 status = pjmedia_wav_player_set_eof_cb(port, NULL,
4278 &on_playfile_done);
4279 if (status != PJ_SUCCESS)
4280 goto on_error;
4281
4282 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
4283 &hangup_timeout_callback);
4284 }
Benny Prijono32e4f492007-01-24 00:44:26 +00004285 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004286 }
4287
Benny Prijono4af234b2007-01-24 02:02:09 +00004288 /* Optionally registers tone players */
4289 for (i=0; i<app_config.tone_count; ++i) {
4290 pjmedia_port *tport;
4291 char name[80];
4292 pj_str_t label;
4293 pj_status_t status;
4294
4295 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
4296 app_config.tones[i].freq1,
4297 app_config.tones[i].freq2);
4298 label = pj_str(name);
4299 status = pjmedia_tonegen_create2(app_config.pool, &label,
4300 8000, 1, 160, 16,
4301 PJMEDIA_TONEGEN_LOOP, &tport);
4302 if (status != PJ_SUCCESS) {
4303 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
4304 goto on_error;
4305 }
4306
4307 status = pjsua_conf_add_port(app_config.pool, tport,
4308 &app_config.tone_slots[i]);
4309 pj_assert(status == PJ_SUCCESS);
4310
4311 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
4312 pj_assert(status == PJ_SUCCESS);
4313 }
4314
Benny Prijono1ebd6142006-10-19 15:48:02 +00004315 /* Optionally create recorder file, if any. */
4316 if (app_config.rec_file.slen) {
4317 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
4318 &app_config.rec_id);
4319 if (status != PJ_SUCCESS)
4320 goto on_error;
4321
4322 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
4323 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004324
Benny Prijonoe347cb02007-02-14 14:36:13 +00004325 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
4326
Benny Prijono91d20f42008-06-14 19:42:37 +00004327 /* Create ringback tones */
4328 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00004329 unsigned i, samples_per_frame;
4330 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00004331 pj_str_t name;
4332
Benny Prijono4f966892008-06-14 22:43:56 +00004333 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
4334 app_config.media_cfg.clock_rate *
4335 app_config.media_cfg.channel_count / 1000;
4336
Benny Prijono91d20f42008-06-14 19:42:37 +00004337 /* Ringback tone (call is ringing) */
4338 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00004339 status = pjmedia_tonegen_create2(app_config.pool, &name,
4340 app_config.media_cfg.clock_rate,
4341 app_config.media_cfg.channel_count,
4342 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004343 16, PJMEDIA_TONEGEN_LOOP,
4344 &app_config.ringback_port);
4345 if (status != PJ_SUCCESS)
4346 goto on_error;
4347
Benny Prijonoe28cec82008-06-14 20:40:53 +00004348 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004349 for (i=0; i<RINGBACK_CNT; ++i) {
4350 tone[i].freq1 = RINGBACK_FREQ1;
4351 tone[i].freq2 = RINGBACK_FREQ2;
4352 tone[i].on_msec = RINGBACK_ON;
4353 tone[i].off_msec = RINGBACK_OFF;
4354 }
4355 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004356
Benny Prijono4f966892008-06-14 22:43:56 +00004357 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004358 PJMEDIA_TONEGEN_LOOP);
4359
4360
Benny Prijono91d20f42008-06-14 19:42:37 +00004361 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4362 &app_config.ringback_slot);
4363 if (status != PJ_SUCCESS)
4364 goto on_error;
4365
4366 /* Ring (to alert incoming call) */
4367 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004368 status = pjmedia_tonegen_create2(app_config.pool, &name,
4369 app_config.media_cfg.clock_rate,
4370 app_config.media_cfg.channel_count,
4371 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004372 16, PJMEDIA_TONEGEN_LOOP,
4373 &app_config.ring_port);
4374 if (status != PJ_SUCCESS)
4375 goto on_error;
4376
Benny Prijono4f966892008-06-14 22:43:56 +00004377 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004378 tone[i].freq1 = RING_FREQ1;
4379 tone[i].freq2 = RING_FREQ2;
4380 tone[i].on_msec = RING_ON;
4381 tone[i].off_msec = RING_OFF;
4382 }
Benny Prijono4f966892008-06-14 22:43:56 +00004383 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004384
Benny Prijono4f966892008-06-14 22:43:56 +00004385 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004386 tone, PJMEDIA_TONEGEN_LOOP);
4387
Benny Prijono91d20f42008-06-14 19:42:37 +00004388 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4389 &app_config.ring_slot);
4390 if (status != PJ_SUCCESS)
4391 goto on_error;
4392
4393 }
4394
Benny Prijono87ef89a2007-01-14 00:39:45 +00004395 /* Add UDP transport unless it's disabled. */
4396 if (!app_config.no_udp) {
4397 pjsua_acc_id aid;
Benny Prijono8972bf02009-04-05 18:30:45 +00004398 pjsip_transport_type_e type = PJSIP_TRANSPORT_UDP;
Benny Prijono87ef89a2007-01-14 00:39:45 +00004399
Benny Prijono8972bf02009-04-05 18:30:45 +00004400 if (app_config.ipv6)
4401 type = PJSIP_TRANSPORT_UDP6;
4402
4403 status = pjsua_transport_create(type,
Benny Prijono87ef89a2007-01-14 00:39:45 +00004404 &app_config.udp_cfg,
4405 &transport_id);
4406 if (status != PJ_SUCCESS)
4407 goto on_error;
4408
4409 /* Add local account */
4410 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4411 //pjsua_acc_set_transport(aid, transport_id);
4412 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004413
4414 if (app_config.udp_cfg.port == 0) {
4415 pjsua_transport_info ti;
4416 pj_sockaddr_in *a;
4417
4418 pjsua_transport_get_info(transport_id, &ti);
4419 a = (pj_sockaddr_in*)&ti.local_addr;
4420
4421 tcp_cfg.port = pj_ntohs(a->sin_port);
4422 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004423 }
4424
Benny Prijonoe93e2872006-06-28 16:46:49 +00004425 /* Add TCP transport unless it's disabled */
4426 if (!app_config.no_tcp) {
4427 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004428 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004429 &transport_id);
4430 if (status != PJ_SUCCESS)
4431 goto on_error;
4432
4433 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004434 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004435 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4436
4437 }
4438
Benny Prijonoe93e2872006-06-28 16:46:49 +00004439
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004440#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4441 /* Add TLS transport when application wants one */
4442 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004443
4444 pjsua_acc_id acc_id;
4445
4446 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004447 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004448 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004449 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004450 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004451 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004452 if (status != PJ_SUCCESS)
4453 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004454
4455 /* Add local account */
4456 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4457 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004458 }
4459#endif
4460
Benny Prijonoe93e2872006-06-28 16:46:49 +00004461 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004462 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004463 status = -1;
4464 goto on_error;
4465 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004466
4467
4468 /* Add accounts */
4469 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004470 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004471 if (status != PJ_SUCCESS)
4472 goto on_error;
4473 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4474 }
4475
4476 /* Add buddies */
4477 for (i=0; i<app_config.buddy_cnt; ++i) {
4478 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4479 if (status != PJ_SUCCESS)
4480 goto on_error;
4481 }
4482
4483 /* Optionally set codec orders */
4484 for (i=0; i<app_config.codec_cnt; ++i) {
4485 pjsua_codec_set_priority(&app_config.codec_arg[i],
4486 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4487 }
4488
Benny Prijonofce28542007-12-09 15:41:10 +00004489 /* Optionally disable some codec */
4490 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4491 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4492 }
4493
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004494 /* Add RTP transports */
Benny Prijono40860c32008-09-04 13:55:33 +00004495#ifdef TRANSPORT_ADAPTER_SAMPLE
4496 status = transport_adapter_sample();
4497
4498#else
Benny Prijono8972bf02009-04-05 18:30:45 +00004499 if (app_config.ipv6)
4500 status = create_ipv6_media_transports();
4501 else
4502 status = pjsua_media_transports_create(&app_config.rtp_cfg);
Benny Prijono40860c32008-09-04 13:55:33 +00004503#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004504 if (status != PJ_SUCCESS)
4505 goto on_error;
4506
Benny Prijono22a300a2006-06-14 20:04:55 +00004507 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004508#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004509 if (app_config.null_audio) {
4510 status = pjsua_set_null_snd_dev();
4511 if (status != PJ_SUCCESS)
4512 return status;
4513 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004514#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004515
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004516 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4517 app_config.playback_dev != PJSUA_INVALID_ID)
4518 {
4519 status = pjsua_set_snd_dev(app_config.capture_dev,
4520 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004521 if (status != PJ_SUCCESS)
4522 goto on_error;
4523 }
4524
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004525 return PJ_SUCCESS;
4526
4527on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004528 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004529 return status;
4530}
4531
4532
Benny Prijonoebc32c32008-06-12 13:30:39 +00004533static int stdout_refresh_proc(void *arg)
4534{
4535 PJ_UNUSED_ARG(arg);
4536
4537 /* Set thread to lowest priority so that it doesn't clobber
4538 * stdout output
4539 */
4540 pj_thread_set_prio(pj_thread_this(),
4541 pj_thread_get_prio_min(pj_thread_this()));
4542
4543 while (!stdout_refresh_quit) {
4544 pj_thread_sleep(stdout_refresh * 1000);
4545 puts(stdout_refresh_text);
4546 fflush(stdout);
4547 }
4548
4549 return 0;
4550}
4551
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004552pj_status_t app_main(void)
4553{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004554 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004555 pj_status_t status;
4556
4557 /* Start pjsua */
4558 status = pjsua_start();
4559 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004560 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004561 return status;
4562 }
4563
Benny Prijonoebc32c32008-06-12 13:30:39 +00004564 /* Start console refresh thread */
4565 if (stdout_refresh > 0) {
4566 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4567 NULL, 0, 0, &stdout_refresh_thread);
4568 }
4569
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004570 console_app_main(&uri_arg);
4571
Benny Prijonoebc32c32008-06-12 13:30:39 +00004572 if (stdout_refresh_thread) {
4573 stdout_refresh_quit = PJ_TRUE;
4574 pj_thread_join(stdout_refresh_thread);
4575 pj_thread_destroy(stdout_refresh_thread);
4576 }
4577
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004578 return PJ_SUCCESS;
4579}
4580
4581pj_status_t app_destroy(void)
4582{
Benny Prijonof762ee72006-12-01 11:14:37 +00004583 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004584 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004585
Benny Prijonoe909eac2006-07-27 22:04:56 +00004586#ifdef STEREO_DEMO
4587 if (app_config.snd) {
4588 pjmedia_snd_port_destroy(app_config.snd);
4589 app_config.snd = NULL;
4590 }
4591#endif
4592
Benny Prijono91d20f42008-06-14 19:42:37 +00004593 /* Close ringback port */
4594 if (app_config.ringback_port &&
4595 app_config.ringback_slot != PJSUA_INVALID_ID)
4596 {
4597 pjsua_conf_remove_port(app_config.ringback_slot);
4598 app_config.ringback_slot = PJSUA_INVALID_ID;
4599 pjmedia_port_destroy(app_config.ringback_port);
4600 app_config.ringback_port = NULL;
4601 }
4602
4603 /* Close ring port */
4604 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4605 pjsua_conf_remove_port(app_config.ring_slot);
4606 app_config.ring_slot = PJSUA_INVALID_ID;
4607 pjmedia_port_destroy(app_config.ring_port);
4608 app_config.ring_port = NULL;
4609 }
4610
Benny Prijono4af234b2007-01-24 02:02:09 +00004611 /* Close tone generators */
4612 for (i=0; i<app_config.tone_count; ++i) {
4613 pjsua_conf_remove_port(app_config.tone_slots[i]);
4614 }
4615
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004616 if (app_config.pool) {
4617 pj_pool_release(app_config.pool);
4618 app_config.pool = NULL;
4619 }
4620
Benny Prijonof762ee72006-12-01 11:14:37 +00004621 status = pjsua_destroy();
4622
4623 pj_bzero(&app_config, sizeof(app_config));
4624
4625 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004626}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004627
4628
4629#ifdef STEREO_DEMO
4630static void stereo_demo()
4631{
4632 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004633 pj_status_t status;
4634
4635 /* Disable existing sound device */
4636 conf = pjsua_set_no_snd_dev();
4637
Benny Prijonoe909eac2006-07-27 22:04:56 +00004638 /* Create stereo-mono splitter/combiner */
4639 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004640 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004641 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004642 2 * conf->info.samples_per_frame,
4643 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004644 0 /* options */,
4645 &splitter);
4646 pj_assert(status == PJ_SUCCESS);
4647
4648 /* Connect channel0 (left channel?) to conference port slot0 */
4649 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4650 0 /*options*/,
4651 conf);
4652 pj_assert(status == PJ_SUCCESS);
4653
4654 /* Create reverse channel for channel1 (right channel?)... */
4655 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4656 splitter,
4657 1 /* ch1 */,
4658 0 /* options */,
4659 &ch1);
4660 pj_assert(status == PJ_SUCCESS);
4661
4662 /* .. and register it to conference bridge (it would be slot1
4663 * if there's no other devices connected to the bridge)
4664 */
4665 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4666 pj_assert(status == PJ_SUCCESS);
4667
4668 /* Create sound device */
4669 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004670 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004671 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004672 2 * conf->info.samples_per_frame,
4673 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004674 0, &app_config.snd);
4675 pj_assert(status == PJ_SUCCESS);
4676
4677
4678 /* Connect the splitter to the sound device */
4679 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4680 pj_assert(status == PJ_SUCCESS);
4681
4682}
4683#endif
4684
Benny Prijono40860c32008-09-04 13:55:33 +00004685#ifdef TRANSPORT_ADAPTER_SAMPLE
4686static pj_status_t create_transport_adapter(pjmedia_endpt *med_endpt, int port,
4687 pjmedia_transport **p_tp)
4688{
4689 pjmedia_transport *udp;
4690 pj_status_t status;
4691
4692 /* Create the UDP media transport */
4693 status = pjmedia_transport_udp_create(med_endpt, NULL, port, 0, &udp);
4694 if (status != PJ_SUCCESS)
4695 return status;
4696
4697 /* Create the adapter */
4698 status = pjmedia_tp_adapter_create(med_endpt, NULL, udp, p_tp);
4699 if (status != PJ_SUCCESS) {
4700 pjmedia_transport_close(udp);
4701 return status;
4702 }
4703
4704 return PJ_SUCCESS;
4705}
4706
4707static pj_status_t transport_adapter_sample(void)
4708{
4709 pjsua_media_transport tp[PJSUA_MAX_CALLS];
4710 pj_status_t status;
4711 int port = 7000;
4712 unsigned i;
4713
4714 for (i=0; i<app_config.cfg.max_calls; ++i) {
4715 status = create_transport_adapter(pjsua_get_pjmedia_endpt(),
4716 port + i*10,
4717 &tp[i].transport);
4718 if (status != PJ_SUCCESS)
4719 return status;
4720 }
4721
4722 return pjsua_media_transports_attach(tp, i, PJ_TRUE);
4723}
4724#endif
4725
Benny Prijono8972bf02009-04-05 18:30:45 +00004726static pj_status_t create_ipv6_media_transports(void)
4727{
4728 pjsua_media_transport tp[PJSUA_MAX_CALLS];
4729 pj_status_t status;
4730 int port = app_config.rtp_cfg.port;
4731 unsigned i;
4732
4733 for (i=0; i<app_config.cfg.max_calls; ++i) {
4734 enum { MAX_RETRY = 10 };
Benny Prijono329d6382009-05-29 13:04:03 +00004735 pj_sock_t sock[2];
4736 pjmedia_sock_info si;
Benny Prijono8972bf02009-04-05 18:30:45 +00004737 unsigned j;
4738
4739 /* Get rid of uninitialized var compiler warning with MSVC */
4740 status = PJ_SUCCESS;
4741
4742 for (j=0; j<MAX_RETRY; ++j) {
Benny Prijono329d6382009-05-29 13:04:03 +00004743 unsigned k;
Benny Prijono8972bf02009-04-05 18:30:45 +00004744
Benny Prijono329d6382009-05-29 13:04:03 +00004745 for (k=0; k<2; ++k) {
4746 pj_sockaddr bound_addr;
4747
4748 status = pj_sock_socket(pj_AF_INET6(), pj_SOCK_DGRAM(), 0, &sock[k]);
4749 if (status != PJ_SUCCESS)
4750 break;
4751
4752 status = pj_sockaddr_init(pj_AF_INET6(), &bound_addr,
4753 &app_config.rtp_cfg.bound_addr,
4754 (unsigned short)(port+k));
4755 if (status != PJ_SUCCESS)
4756 break;
4757
4758 status = pj_sock_bind(sock[k], &bound_addr,
4759 pj_sockaddr_get_len(&bound_addr));
4760 if (status != PJ_SUCCESS)
4761 break;
4762 }
4763 if (status != PJ_SUCCESS) {
4764 if (k==1)
4765 pj_sock_close(sock[0]);
4766
4767 if (port != 0)
4768 port += 10;
4769 else
4770 break;
4771
4772 continue;
4773 }
4774
4775 pj_bzero(&si, sizeof(si));
4776 si.rtp_sock = sock[0];
4777 si.rtcp_sock = sock[1];
4778
4779 pj_sockaddr_init(pj_AF_INET6(), &si.rtp_addr_name,
4780 &app_config.rtp_cfg.public_addr,
4781 (unsigned short)(port));
4782 pj_sockaddr_init(pj_AF_INET6(), &si.rtcp_addr_name,
4783 &app_config.rtp_cfg.public_addr,
4784 (unsigned short)(port+1));
4785
4786 status = pjmedia_transport_udp_attach(pjsua_get_pjmedia_endpt(),
4787 NULL,
4788 &si,
4789 0,
4790 &tp[i].transport);
Benny Prijono8972bf02009-04-05 18:30:45 +00004791 if (port != 0)
4792 port += 10;
4793 else
4794 break;
4795
4796 if (status == PJ_SUCCESS)
4797 break;
4798 }
4799
4800 if (status != PJ_SUCCESS) {
4801 pjsua_perror(THIS_FILE, "Error creating IPv6 UDP media transport",
4802 status);
4803 for (j=0; j<i; ++j) {
4804 pjmedia_transport_close(tp[j].transport);
4805 }
4806 return status;
4807 }
4808 }
4809
4810 return pjsua_media_transports_attach(tp, i, PJ_TRUE);
4811}