blob: 004be5a1b9795b9a07fecc727886059814311a2c [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20
21
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000022#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000023#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000024
Benny Prijonoe909eac2006-07-27 22:04:56 +000025//#define STEREO_DEMO
Benny Prijono40860c32008-09-04 13:55:33 +000026//#define TRANSPORT_ADAPTER_SAMPLE
Benny Prijonoeebe9af2006-06-13 22:57:13 +000027
Benny Prijono4f966892008-06-14 22:43:56 +000028/* Ringtones US UK */
29#define RINGBACK_FREQ1 440 /* 400 */
30#define RINGBACK_FREQ2 480 /* 450 */
31#define RINGBACK_ON 2000 /* 400 */
32#define RINGBACK_OFF 4000 /* 200 */
33#define RINGBACK_CNT 1 /* 2 */
34#define RINGBACK_INTERVAL 4000 /* 2000 */
Benny Prijono91d20f42008-06-14 19:42:37 +000035
Benny Prijono4f966892008-06-14 22:43:56 +000036#define RING_FREQ1 800
37#define RING_FREQ2 640
38#define RING_ON 200
39#define RING_OFF 100
40#define RING_CNT 3
41#define RING_INTERVAL 3000
Benny Prijono91d20f42008-06-14 19:42:37 +000042
43
Benny Prijono804ff0a2006-09-14 11:17:48 +000044/* Call specific data */
45struct call_data
46{
47 pj_timer_entry timer;
Benny Prijono91d20f42008-06-14 19:42:37 +000048 pj_bool_t ringback_on;
49 pj_bool_t ring_on;
Benny Prijono804ff0a2006-09-14 11:17:48 +000050};
51
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052
53/* Pjsua application data */
54static struct app_config
55{
56 pjsua_config cfg;
57 pjsua_logging_config log_cfg;
58 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000059 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000060 pj_bool_t no_tcp;
61 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000062 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000063 pjsua_transport_config udp_cfg;
64 pjsua_transport_config rtp_cfg;
65
66 unsigned acc_cnt;
67 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
68
69 unsigned buddy_cnt;
70 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
71
Benny Prijono804ff0a2006-09-14 11:17:48 +000072 struct call_data call_data[PJSUA_MAX_CALLS];
73
Benny Prijonoeebe9af2006-06-13 22:57:13 +000074 pj_pool_t *pool;
75 /* Compatibility with older pjsua */
76
77 unsigned codec_cnt;
78 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000079 unsigned codec_dis_cnt;
80 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000082 unsigned wav_count;
83 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000084 unsigned tone_count;
85 pjmedia_tone_desc tones[32];
86 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000087 pjsua_player_id wav_id;
88 pjsua_conf_port_id wav_port;
89 pj_bool_t auto_play;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +000090 pj_bool_t auto_play_hangup;
91 pj_timer_entry auto_hangup_timer;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000092 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000093 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000094 pj_str_t rec_file;
95 pj_bool_t auto_rec;
96 pjsua_recorder_id rec_id;
97 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000098 unsigned auto_answer;
99 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +0000100
101#ifdef STEREO_DEMO
102 pjmedia_snd_port *snd;
103#endif
104
Benny Prijono6dd967c2006-12-26 02:27:14 +0000105 float mic_level,
106 speaker_level;
107
Benny Prijono4e5d5512007-03-06 18:11:30 +0000108 int capture_dev, playback_dev;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000109 unsigned capture_lat, playback_lat;
Benny Prijono91d20f42008-06-14 19:42:37 +0000110
111 pj_bool_t no_tones;
112 int ringback_slot;
113 int ringback_cnt;
114 pjmedia_port *ringback_port;
115 int ring_slot;
116 int ring_cnt;
117 pjmedia_port *ring_port;
118
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000119} app_config;
120
121
Benny Prijono21b9ad92006-08-15 13:11:22 +0000122//static pjsua_acc_id current_acc;
123#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +0000124static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoebc32c32008-06-12 13:30:39 +0000125static pj_bool_t cmd_echo;
126static int stdout_refresh = -1;
127static const char *stdout_refresh_text = "STDOUT_REFRESH";
128static pj_bool_t stdout_refresh_quit = PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000129static pj_str_t uri_arg;
130
Nanang Izzuddin660eee82008-07-17 14:54:03 +0000131static char some_buf[1024 * 3];
Benny Prijono6eddd872008-03-21 13:46:08 +0000132
Benny Prijono594e4c52006-09-14 18:51:01 +0000133#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +0000134static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +0000135#endif
Benny Prijono40860c32008-09-04 13:55:33 +0000136#ifdef TRANSPORT_ADAPTER_SAMPLE
137static pj_status_t transport_adapter_sample(void);
138#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000139pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000140
Benny Prijono91d20f42008-06-14 19:42:37 +0000141static void ringback_start(pjsua_call_id call_id);
142static void ring_start(pjsua_call_id call_id);
143static void ring_stop(pjsua_call_id call_id);
144
145
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000146/*****************************************************************************
147 * Configuration manipulation
148 */
149
150/* Show usage */
151static void usage(void)
152{
153 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000154 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000155 puts ("");
156 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000157 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000158 puts (" --help Display this help screen");
159 puts (" --version Display version info");
160 puts ("");
161 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000162 puts (" --log-file=fname Log to filename (default stderr)");
163 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
164 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000165 puts (" --color Use colorful logging (default yes on Win32)");
166 puts (" --no-color Disable colorful logging");
Benny Prijonob7944862008-07-19 20:53:49 +0000167 puts (" --light-bg Use dark colors for light background (default is dark bg)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000168
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000169 puts ("");
170 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000171 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000172#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000173 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
174 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000175#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000176 puts (" --registrar=url Set the URL of registrar server");
177 puts (" --id=url Set the URL of local ID (used in From header)");
178 puts (" --contact=url Optionally override the Contact information");
179 puts (" --proxy=url Optional URL of proxy server to visit");
180 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000181 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 puts (" --realm=string Set realm");
183 puts (" --username=string Set authentication username");
184 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000185 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000186 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000187 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
188 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000189 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000190 puts ("");
191 puts ("SIP Account Control:");
192 puts (" --next-account Add more account");
193 puts ("");
194 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000195 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
196 puts (" TCP and UDP transports on the specified port, unless");
197 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000198 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
199 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000200 puts (" --no-tcp Disable TCP transport.");
201 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000202 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
203 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000204 puts (" --outbound=url Set the URL of global outbound proxy server");
205 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000206 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000207 puts ("");
208 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000209 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000210 puts (" --tls-ca-file Specify TLS CA file (default=none)");
211 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
212 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
213 puts (" --tls-password Specify TLS password to private key file (default=none)");
214 puts (" --tls-verify-server Verify server's certificate (default=no)");
215 puts (" --tls-verify-client Verify client's certificate (default=no)");
216 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
Benny Prijonoe10db842008-07-01 15:31:59 +0000217 puts (" --tls-srv-name Specify TLS server name for multi-hosting server (optional)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000218
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000219 puts ("");
220 puts ("Media Options:");
221 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000222 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000223 puts (" --clock-rate=N Override conference bridge clock rate");
224 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000225 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000226 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000227 puts (" --play-file=file Register WAV file in conference bridge.");
228 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000229 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
230 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
231 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000232 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233 puts (" --auto-play Automatically play the file (to incoming calls only)");
234 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000235 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000236 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
237 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000238 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000239 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000240 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000241 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000242 puts (" --ec-opt=OPT Select echo canceller algorithm (0=default, ");
243 puts (" 1=speex, 2=suppressor)");
Benny Prijono00cae612006-07-31 15:19:36 +0000244 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000245 puts (" --capture-dev=id Audio capture device ID (default=-1)");
246 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000247 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000248 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000249 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
250 puts (" Specify N=-1 (default) to disable this feature.");
251 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000252 puts (" --no-tones Disable audible tones");
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000253 puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000254
Benny Prijonof76e1392008-06-06 14:51:48 +0000255 puts ("");
256 puts ("Media Transport Options:");
257 puts (" --use-ice Enable ICE (default:no)");
Benny Prijono551af422008-08-07 09:55:52 +0000258 puts (" --ice-no-host Disable ICE host candidates (default: no)");
259 puts (" --ice-no-rtcp Disable RTCP component in ICE (default: no)");
Benny Prijonof76e1392008-06-06 14:51:48 +0000260 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
261 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
262 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
263 puts (" --use-turn Enable TURN relay with ICE (default:no)");
264 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
265 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
266 puts (" --turn-user TURN username");
267 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000268
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000269 puts ("");
270 puts ("Buddy List (can be more than one):");
271 puts (" --add-buddy url Add the specified URL to the buddy list.");
272 puts ("");
273 puts ("User Agent options:");
274 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
275 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000276 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000277 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000278 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000279 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono91d06b62008-09-20 12:16:56 +0000280 puts (" --no-force-lr Allow strict-route to be used (i.e. do not force lr)");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000281
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000282 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000283 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
284 puts ("");
285
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000286 fflush(stdout);
287}
288
289
290/* Set default config. */
291static void default_config(struct app_config *cfg)
292{
Benny Prijono56315612006-07-18 14:39:40 +0000293 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000294 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000295
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000296 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000297 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000298 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
299
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000300 pjsua_logging_config_default(&cfg->log_cfg);
301 pjsua_media_config_default(&cfg->media_cfg);
302 pjsua_transport_config_default(&cfg->udp_cfg);
303 cfg->udp_cfg.port = 5060;
304 pjsua_transport_config_default(&cfg->rtp_cfg);
305 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000306 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000307 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000308 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000309 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000310 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000311 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000312 cfg->capture_dev = PJSUA_INVALID_ID;
313 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000314 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
315 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000316 cfg->ringback_slot = PJSUA_INVALID_ID;
317 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000318
319 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
320 pjsua_acc_config_default(&cfg->acc_cfg[i]);
321
322 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
323 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000324}
325
326
327/*
328 * Read command arguments from config file.
329 */
330static int read_config_file(pj_pool_t *pool, const char *filename,
331 int *app_argc, char ***app_argv)
332{
333 int i;
334 FILE *fhnd;
335 char line[200];
336 int argc = 0;
337 char **argv;
338 enum { MAX_ARGS = 64 };
339
340 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
341 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
342 argv[argc++] = *app_argv[0];
343
344 /* Open config file. */
345 fhnd = fopen(filename, "rt");
346 if (!fhnd) {
347 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
348 fflush(stdout);
349 return -1;
350 }
351
352 /* Scan tokens in the file. */
353 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000354 char *token;
355 char *p;
356 const char *whitespace = " \t\r\n";
357 char cDelimiter;
358 int len, token_len;
359
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000360 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000361
362 // Trim ending newlines
363 len = strlen(line);
364 if (line[len-1]=='\n')
365 line[--len] = '\0';
366 if (line[len-1]=='\r')
367 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368
Benny Prijonobf5b4692007-06-28 03:20:17 +0000369 if (len==0) continue;
370
371 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
372 // first, scan whitespaces
373 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
374
375 if (*p == '\0') // are we done yet?
376 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377
Benny Prijonobf5b4692007-06-28 03:20:17 +0000378 if (*p == '"' || *p == '\'') { // is token a quoted string
379 cDelimiter = *p++; // save quote delimiter
380 token = p;
381
382 while (*p != '\0' && *p != cDelimiter) p++;
383
384 if (*p == '\0') // found end of the line, but,
385 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000386
Benny Prijonobf5b4692007-06-28 03:20:17 +0000387 } else { // token's not a quoted string
388 token = p;
389
390 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
391
392 cDelimiter = *p;
393 }
394
395 *p = '\0';
396 token_len = p-token;
397
398 if (token_len > 0) {
399 if (*token == '#')
400 break; // ignore remainder of line
401
402 argv[argc] = pj_pool_alloc(pool, token_len + 1);
403 pj_memcpy(argv[argc], token, token_len + 1);
404 ++argc;
405 }
406
407 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000408 }
409 }
410
411 /* Copy arguments from command line */
412 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
413 argv[argc++] = (*app_argv)[i];
414
415 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
416 PJ_LOG(1,(THIS_FILE,
417 "Too many arguments specified in cmd line/config file"));
418 fflush(stdout);
419 fclose(fhnd);
420 return -1;
421 }
422
423 fclose(fhnd);
424
425 /* Assign the new command line back to the original command line. */
426 *app_argc = argc;
427 *app_argv = argv;
428 return 0;
429
430}
431
432static int my_atoi(const char *cs)
433{
434 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000435
436 pj_cstr(&s, cs);
437 if (cs[0] == '-') {
438 s.ptr++, s.slen--;
439 return 0 - (int)pj_strtoul(&s);
440 } else if (cs[0] == '+') {
441 s.ptr++, s.slen--;
442 return pj_strtoul(&s);
443 } else {
444 return pj_strtoul(&s);
445 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000446}
447
448
449/* Parse arguments. */
450static pj_status_t parse_args(int argc, char *argv[],
451 struct app_config *cfg,
452 pj_str_t *uri_to_call)
453{
454 int c;
455 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000456 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonob7944862008-07-19 20:53:49 +0000457 OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000458 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000459 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
460 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000461 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000462 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000463 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000464 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000465 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
466 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijono551af422008-08-07 09:55:52 +0000467 OPT_USE_TURN, OPT_ICE_NO_HOST, OPT_ICE_NO_RTCP, OPT_TURN_SRV,
468 OPT_TURN_TCP, OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000469 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
470 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000471 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000472 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL, OPT_EC_OPT,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000473 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000474 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000475 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000476 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
477 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000478 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000479 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000480 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000481 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000482#ifdef _IONBF
483 OPT_STDOUT_NO_BUF,
484#endif
Benny Prijono91d06b62008-09-20 12:16:56 +0000485 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC,
486 OPT_NO_FORCE_LR
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487 };
488 struct pj_getopt_option long_options[] = {
489 { "config-file",1, 0, OPT_CONFIG_FILE},
490 { "log-file", 1, 0, OPT_LOG_FILE},
491 { "log-level", 1, 0, OPT_LOG_LEVEL},
492 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
Benny Prijonod6e362a2008-07-19 17:53:47 +0000493 { "color", 0, 0, OPT_COLOR},
494 { "no-color", 0, 0, OPT_NO_COLOR},
Benny Prijonob7944862008-07-19 20:53:49 +0000495 { "light-bg", 0, 0, OPT_LIGHT_BG},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496 { "help", 0, 0, OPT_HELP},
497 { "version", 0, 0, OPT_VERSION},
498 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000499 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000500 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000501 { "null-audio", 0, 0, OPT_NULL_AUDIO},
502 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000503 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000504 { "no-tcp", 0, 0, OPT_NO_TCP},
505 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000506 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000507 { "proxy", 1, 0, OPT_PROXY},
508 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
509 { "registrar", 1, 0, OPT_REGISTRAR},
510 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000511 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000512 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000513 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 { "id", 1, 0, OPT_ID},
515 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000516 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
517 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijono91d06b62008-09-20 12:16:56 +0000518 { "no-force-lr",0, 0, OPT_NO_FORCE_LR},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 { "realm", 1, 0, OPT_REALM},
520 { "username", 1, 0, OPT_USERNAME},
521 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000522 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000523 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000524 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000525 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
526 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
527 { "no-presence", 0, 0, OPT_NO_PRESENCE},
528 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000530 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000531 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000532 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
533 { "auto-conf", 0, 0, OPT_AUTO_CONF},
534 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000535 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000536 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000538
Benny Prijonoc97608e2007-03-23 16:34:20 +0000539 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000540 { "use-turn", 0, 0, OPT_USE_TURN},
541 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
Benny Prijono551af422008-08-07 09:55:52 +0000542 { "ice-no-rtcp",0, 0, OPT_ICE_NO_RTCP},
Benny Prijonof76e1392008-06-06 14:51:48 +0000543 { "turn-srv", 1, 0, OPT_TURN_SRV},
544 { "turn-tcp", 0, 0, OPT_TURN_TCP},
545 { "turn-user", 1, 0, OPT_TURN_USER},
546 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
547
Benny Prijonod8179652008-01-23 20:39:07 +0000548#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
549 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000550 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000551#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000552 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000553 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000554 { "complexity", 1, 0, OPT_COMPLEXITY},
555 { "quality", 1, 0, OPT_QUALITY},
556 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000557 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000558 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000559 { "ec-opt", 1, 0, OPT_EC_OPT},
Benny Prijono00cae612006-07-31 15:19:36 +0000560 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
561 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
562 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000563 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000564 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000565 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000566 { "duration", 1, 0, OPT_DURATION},
567 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000568 { "use-tls", 0, 0, OPT_USE_TLS},
569 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000570 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
571 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000572 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000573 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
574 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
575 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000576 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000577 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
578 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000579 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
580 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000581 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
582 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000583#ifdef _IONBF
584 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
585#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000586 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000587 { "no-tones", 0, 0, OPT_NO_TONES},
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000588 { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000589 { NULL, 0, 0, 0}
590 };
591 pj_status_t status;
592 pjsua_acc_config *cur_acc;
593 char *config_file = NULL;
594 unsigned i;
595
596 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000597 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000598 while ((c=pj_getopt_long(argc, argv, "", long_options,
599 &option_index)) != -1)
600 {
601 switch (c) {
602 case OPT_CONFIG_FILE:
603 config_file = pj_optarg;
604 break;
605 }
606 if (config_file)
607 break;
608 }
609
610 if (config_file) {
611 status = read_config_file(app_config.pool, config_file, &argc, &argv);
612 if (status != 0)
613 return status;
614 }
615
616 cfg->acc_cnt = 0;
617 cur_acc = &cfg->acc_cfg[0];
618
619
620 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
621 * read from config file.
622 */
623 pj_optind = 0;
624 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000625 pj_str_t tmp;
626 long lval;
627
628 switch (c) {
629
Benny Prijono6f137482006-06-15 11:31:36 +0000630 case OPT_CONFIG_FILE:
631 /* Ignore as this has been processed before */
632 break;
633
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000634 case OPT_LOG_FILE:
635 cfg->log_cfg.log_filename = pj_str(pj_optarg);
636 break;
637
638 case OPT_LOG_LEVEL:
639 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
640 if (c < 0 || c > 6) {
641 PJ_LOG(1,(THIS_FILE,
642 "Error: expecting integer value 0-6 "
643 "for --log-level"));
644 return PJ_EINVAL;
645 }
646 cfg->log_cfg.level = c;
647 pj_log_set_level( c );
648 break;
649
650 case OPT_APP_LOG_LEVEL:
651 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
652 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
653 PJ_LOG(1,(THIS_FILE,
654 "Error: expecting integer value 0-6 "
655 "for --app-log-level"));
656 return PJ_EINVAL;
657 }
658 break;
659
Benny Prijonod6e362a2008-07-19 17:53:47 +0000660 case OPT_COLOR:
661 cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
662 break;
663
664 case OPT_NO_COLOR:
665 cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
666 break;
667
Benny Prijonob7944862008-07-19 20:53:49 +0000668 case OPT_LIGHT_BG:
669 pj_log_set_color(1, PJ_TERM_COLOR_R);
670 pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
671 pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
672 pj_log_set_color(4, 0);
673 pj_log_set_color(5, 0);
674 pj_log_set_color(77, 0);
675 break;
676
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000677 case OPT_HELP:
678 usage();
679 return PJ_EINVAL;
680
681 case OPT_VERSION: /* version */
682 pj_dump_config();
683 return PJ_EINVAL;
684
685 case OPT_NULL_AUDIO:
686 cfg->null_audio = PJ_TRUE;
687 break;
688
689 case OPT_CLOCK_RATE:
690 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000691 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000692 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000693 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000694 return PJ_EINVAL;
695 }
696 cfg->media_cfg.clock_rate = lval;
697 break;
698
Benny Prijonof3758ee2008-02-26 15:32:16 +0000699 case OPT_SND_CLOCK_RATE:
700 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000701 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000702 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000703 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000704 return PJ_EINVAL;
705 }
706 cfg->media_cfg.snd_clock_rate = lval;
707 break;
708
Benny Prijono7d60d052008-03-29 12:24:20 +0000709 case OPT_STEREO:
710 cfg->media_cfg.channel_count = 2;
711 break;
712
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000713 case OPT_LOCAL_PORT: /* local-port */
714 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000715 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000716 PJ_LOG(1,(THIS_FILE,
717 "Error: expecting integer value for "
718 "--local-port"));
719 return PJ_EINVAL;
720 }
721 cfg->udp_cfg.port = (pj_uint16_t)lval;
722 break;
723
Benny Prijono0a5cad82006-09-26 13:21:02 +0000724 case OPT_IP_ADDR: /* ip-addr */
725 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
726 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
727 break;
728
Benny Prijonoe93e2872006-06-28 16:46:49 +0000729 case OPT_NO_UDP: /* no-udp */
730 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000731 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
732 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000733 }
734
735 cfg->no_udp = PJ_TRUE;
736 break;
737
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000738 case OPT_NOREFERSUB: /* norefersub */
739 cfg->no_refersub = PJ_TRUE;
740 break;
741
Benny Prijonoe93e2872006-06-28 16:46:49 +0000742 case OPT_NO_TCP: /* no-tcp */
743 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000744 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
745 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000746 }
747
748 cfg->no_tcp = PJ_TRUE;
749 break;
750
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000751 case OPT_PROXY: /* proxy */
752 if (pjsua_verify_sip_url(pj_optarg) != 0) {
753 PJ_LOG(1,(THIS_FILE,
754 "Error: invalid SIP URL '%s' "
755 "in proxy argument", pj_optarg));
756 return PJ_EINVAL;
757 }
758 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
759 break;
760
761 case OPT_OUTBOUND_PROXY: /* outbound proxy */
762 if (pjsua_verify_sip_url(pj_optarg) != 0) {
763 PJ_LOG(1,(THIS_FILE,
764 "Error: invalid SIP URL '%s' "
765 "in outbound proxy argument", pj_optarg));
766 return PJ_EINVAL;
767 }
768 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
769 break;
770
771 case OPT_REGISTRAR: /* registrar */
772 if (pjsua_verify_sip_url(pj_optarg) != 0) {
773 PJ_LOG(1,(THIS_FILE,
774 "Error: invalid SIP URL '%s' in "
775 "registrar argument", pj_optarg));
776 return PJ_EINVAL;
777 }
778 cur_acc->reg_uri = pj_str(pj_optarg);
779 break;
780
781 case OPT_REG_TIMEOUT: /* reg-timeout */
782 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
783 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
784 PJ_LOG(1,(THIS_FILE,
785 "Error: invalid value for --reg-timeout "
786 "(expecting 1-3600)"));
787 return PJ_EINVAL;
788 }
789 break;
790
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000791 case OPT_PUBLISH: /* publish */
792 cur_acc->publish_enabled = PJ_TRUE;
793 break;
794
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000795 case OPT_100REL: /** 100rel */
796 cur_acc->require_100rel = PJ_TRUE;
797 cfg->cfg.require_100rel = PJ_TRUE;
798 break;
799
Benny Prijono48ab2b72007-11-08 09:24:30 +0000800 case OPT_USE_IMS: /* Activate IMS settings */
801 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000802 break;
803
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000804 case OPT_ID: /* id */
805 if (pjsua_verify_sip_url(pj_optarg) != 0) {
806 PJ_LOG(1,(THIS_FILE,
807 "Error: invalid SIP URL '%s' "
808 "in local id argument", pj_optarg));
809 return PJ_EINVAL;
810 }
811 cur_acc->id = pj_str(pj_optarg);
812 break;
813
814 case OPT_CONTACT: /* contact */
815 if (pjsua_verify_sip_url(pj_optarg) != 0) {
816 PJ_LOG(1,(THIS_FILE,
817 "Error: invalid SIP URL '%s' "
818 "in contact argument", pj_optarg));
819 return PJ_EINVAL;
820 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000821 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000822 break;
823
Benny Prijonofce28542007-12-09 15:41:10 +0000824 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000825 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000826 break;
827
828 case OPT_USE_COMPACT_FORM:
829 /* enable compact form - from Ticket #342 */
830 {
831 extern pj_bool_t pjsip_use_compact_form;
832 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
833 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
834
835 pjsip_use_compact_form = PJ_TRUE;
836 /* do not transmit Allow header */
837 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
838 /* Do not include rtpmap for static payload types (<96) */
839 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
840 }
841 break;
842
Benny Prijono91d06b62008-09-20 12:16:56 +0000843 case OPT_NO_FORCE_LR:
844 cfg->cfg.force_lr = PJ_FALSE;
845 break;
846
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000847 case OPT_NEXT_ACCOUNT: /* Add more account. */
848 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000849 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000850 break;
851
852 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000853 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000854 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000855 break;
856
857 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000858 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000859 break;
860
861 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000862 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
863 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000864#if PJSIP_HAS_DIGEST_AKA_AUTH
865 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
866 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
867 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
868#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000869 break;
870
871 case OPT_NEXT_CRED: /* next credential */
872 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000873 break;
874
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000875 case OPT_NAMESERVER: /* nameserver */
876 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
877 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
878 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
879 return PJ_ETOOMANY;
880 }
881 break;
882
Benny Prijonoebbf6892007-03-24 17:37:25 +0000883 case OPT_STUN_DOMAIN: /* STUN domain */
884 cfg->cfg.stun_domain = pj_str(pj_optarg);
885 break;
886
Benny Prijonoc97608e2007-03-23 16:34:20 +0000887 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000888 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 break;
890
891 case OPT_ADD_BUDDY: /* Add to buddy list. */
892 if (pjsua_verify_sip_url(pj_optarg) != 0) {
893 PJ_LOG(1,(THIS_FILE,
894 "Error: invalid URL '%s' in "
895 "--add-buddy option", pj_optarg));
896 return -1;
897 }
898 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
899 PJ_LOG(1,(THIS_FILE,
900 "Error: too many buddies in buddy list."));
901 return -1;
902 }
903 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
904 cfg->buddy_cnt++;
905 break;
906
907 case OPT_AUTO_PLAY:
908 cfg->auto_play = 1;
909 break;
910
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000911 case OPT_AUTO_PLAY_HANGUP:
912 cfg->auto_play_hangup = 1;
913 break;
914
Benny Prijono1ebd6142006-10-19 15:48:02 +0000915 case OPT_AUTO_REC:
916 cfg->auto_rec = 1;
917 break;
918
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000919 case OPT_AUTO_LOOP:
920 cfg->auto_loop = 1;
921 break;
922
Benny Prijono7ca96da2006-08-07 12:11:40 +0000923 case OPT_AUTO_CONF:
924 cfg->auto_conf = 1;
925 break;
926
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000927 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000928 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000929 break;
930
Benny Prijono4af234b2007-01-24 02:02:09 +0000931 case OPT_PLAY_TONE:
932 {
933 int f1, f2, on, off;
934 int n;
935
936 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
937 if (n != 4) {
938 puts("Expecting f1,f2,on,off in --play-tone");
939 return -1;
940 }
941
942 cfg->tones[cfg->tone_count].freq1 = (short)f1;
943 cfg->tones[cfg->tone_count].freq2 = (short)f2;
944 cfg->tones[cfg->tone_count].on_msec = (short)on;
945 cfg->tones[cfg->tone_count].off_msec = (short)off;
946 ++cfg->tone_count;
947 }
948 break;
949
Benny Prijono1ebd6142006-10-19 15:48:02 +0000950 case OPT_REC_FILE:
951 cfg->rec_file = pj_str(pj_optarg);
952 break;
953
Benny Prijonoc97608e2007-03-23 16:34:20 +0000954 case OPT_USE_ICE:
955 cfg->media_cfg.enable_ice = PJ_TRUE;
956 break;
957
Benny Prijonof76e1392008-06-06 14:51:48 +0000958 case OPT_USE_TURN:
959 cfg->media_cfg.enable_turn = PJ_TRUE;
960 break;
961
962 case OPT_ICE_NO_HOST:
963 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
964 break;
965
Benny Prijono551af422008-08-07 09:55:52 +0000966 case OPT_ICE_NO_RTCP:
967 cfg->media_cfg.ice_no_rtcp = PJ_TRUE;
968 break;
969
Benny Prijonof76e1392008-06-06 14:51:48 +0000970 case OPT_TURN_SRV:
971 cfg->media_cfg.turn_server = pj_str(pj_optarg);
972 break;
973
974 case OPT_TURN_TCP:
975 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
976 break;
977
978 case OPT_TURN_USER:
979 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
980 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
981 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
982 break;
983
984 case OPT_TURN_PASSWD:
985 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
986 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
987 break;
988
Benny Prijonod8179652008-01-23 20:39:07 +0000989#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
990 case OPT_USE_SRTP:
991 app_config.cfg.use_srtp = my_atoi(pj_optarg);
992 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
993 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
994 return -1;
995 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000996 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +0000997 break;
Benny Prijonof6508982008-01-25 09:02:33 +0000998 case OPT_SRTP_SECURE:
999 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
1000 if (!pj_isdigit(*pj_optarg) ||
1001 app_config.cfg.srtp_secure_signaling > 2)
1002 {
1003 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
1004 return -1;
1005 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001006 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +00001007 break;
Benny Prijonod8179652008-01-23 20:39:07 +00001008#endif
1009
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001010 case OPT_RTP_PORT:
1011 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +00001012 if (cfg->rtp_cfg.port == 0) {
1013 enum { START_PORT=4000 };
1014 unsigned range;
1015
1016 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
1017 cfg->rtp_cfg.port = START_PORT +
1018 ((pj_rand() % range) & 0xFFFE);
1019 }
1020
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001021 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
1022 PJ_LOG(1,(THIS_FILE,
1023 "Error: rtp-port argument value "
1024 "(expecting 1-65535"));
1025 return -1;
1026 }
1027 break;
1028
Benny Prijonofce28542007-12-09 15:41:10 +00001029 case OPT_DIS_CODEC:
1030 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
1031 break;
1032
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001033 case OPT_ADD_CODEC:
1034 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
1035 break;
1036
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001037 /* These options were no longer valid after new pjsua */
1038 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001039 case OPT_COMPLEXITY:
1040 cfg->complexity = my_atoi(pj_optarg);
1041 if (cfg->complexity < 0 || cfg->complexity > 10) {
1042 PJ_LOG(1,(THIS_FILE,
1043 "Error: invalid --complexity (expecting 0-10"));
1044 return -1;
1045 }
1046 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001047 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001048
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001049 case OPT_DURATION:
1050 cfg->duration = my_atoi(pj_optarg);
1051 break;
1052
Benny Prijonof521eb02006-08-06 23:07:25 +00001053 case OPT_THREAD_CNT:
1054 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1055 if (cfg->cfg.thread_cnt > 128) {
1056 PJ_LOG(1,(THIS_FILE,
1057 "Error: invalid --thread-cnt option"));
1058 return -1;
1059 }
1060 break;
1061
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001062 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001063 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1064 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065 PJ_LOG(1,(THIS_FILE,
1066 "Error: invalid --ptime option"));
1067 return -1;
1068 }
1069 break;
1070
Benny Prijono0a12f002006-07-26 17:05:39 +00001071 case OPT_NO_VAD:
1072 cfg->media_cfg.no_vad = PJ_TRUE;
1073 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001074
Benny Prijonod79f25c2006-08-02 19:41:37 +00001075 case OPT_EC_TAIL:
1076 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1077 if (cfg->media_cfg.ec_tail_len > 1000) {
1078 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1079 "is too big"));
1080 return -1;
1081 }
1082 break;
1083
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001084 case OPT_EC_OPT:
1085 cfg->media_cfg.ec_options = my_atoi(pj_optarg);
1086 break;
1087
Benny Prijono0498d902006-06-19 14:49:14 +00001088 case OPT_QUALITY:
1089 cfg->media_cfg.quality = my_atoi(pj_optarg);
1090 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1091 PJ_LOG(1,(THIS_FILE,
1092 "Error: invalid --quality (expecting 0-10"));
1093 return -1;
1094 }
1095 break;
1096
Benny Prijono00cae612006-07-31 15:19:36 +00001097 case OPT_ILBC_MODE:
1098 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1099 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1100 PJ_LOG(1,(THIS_FILE,
1101 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1102 return -1;
1103 }
1104 break;
1105
1106 case OPT_RX_DROP_PCT:
1107 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1108 if (cfg->media_cfg.rx_drop_pct > 100) {
1109 PJ_LOG(1,(THIS_FILE,
1110 "Error: invalid --rx-drop-pct (expecting <= 100"));
1111 return -1;
1112 }
1113 break;
1114
1115 case OPT_TX_DROP_PCT:
1116 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1117 if (cfg->media_cfg.tx_drop_pct > 100) {
1118 PJ_LOG(1,(THIS_FILE,
1119 "Error: invalid --tx-drop-pct (expecting <= 100"));
1120 return -1;
1121 }
1122 break;
1123
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001124 case OPT_AUTO_ANSWER:
1125 cfg->auto_answer = my_atoi(pj_optarg);
1126 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1127 PJ_LOG(1,(THIS_FILE,
1128 "Error: invalid code in --auto-answer "
1129 "(expecting 100-699"));
1130 return -1;
1131 }
1132 break;
1133
1134 case OPT_MAX_CALLS:
1135 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001136 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001137 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1138 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001139 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001140 return -1;
1141 }
1142 break;
1143
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001144 case OPT_USE_TLS:
1145 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001146#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1147 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1148 return -1;
1149#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001150 break;
1151
1152 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001153 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1154#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1155 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1156 return -1;
1157#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001158 break;
1159
Benny Prijonof3bbc132006-12-25 06:43:59 +00001160 case OPT_TLS_CERT_FILE:
1161 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1162#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1163 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1164 return -1;
1165#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001166 break;
1167
Benny Prijonof3bbc132006-12-25 06:43:59 +00001168 case OPT_TLS_PRIV_FILE:
1169 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1170 break;
1171
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001172 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001173 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1174#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1175 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1176 return -1;
1177#endif
1178 break;
1179
1180 case OPT_TLS_VERIFY_SERVER:
1181 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1182 break;
1183
1184 case OPT_TLS_VERIFY_CLIENT:
1185 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1186 break;
1187
1188 case OPT_TLS_NEG_TIMEOUT:
1189 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001190 break;
1191
Benny Prijonoe10db842008-07-01 15:31:59 +00001192 case OPT_TLS_SRV_NAME:
1193 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1194 break;
1195
Benny Prijono4e5d5512007-03-06 18:11:30 +00001196 case OPT_CAPTURE_DEV:
1197 cfg->capture_dev = atoi(pj_optarg);
1198 break;
1199
1200 case OPT_PLAYBACK_DEV:
1201 cfg->playback_dev = atoi(pj_optarg);
1202 break;
1203
Benny Prijonoebc32c32008-06-12 13:30:39 +00001204 case OPT_STDOUT_REFRESH:
1205 stdout_refresh = atoi(pj_optarg);
1206 break;
1207
1208 case OPT_STDOUT_REFRESH_TEXT:
1209 stdout_refresh_text = pj_optarg;
1210 break;
1211
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001212#ifdef _IONBF
1213 case OPT_STDOUT_NO_BUF:
1214 setvbuf(stdout, NULL, _IONBF, 0);
1215 break;
1216#endif
1217
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001218 case OPT_CAPTURE_LAT:
1219 cfg->capture_lat = atoi(pj_optarg);
1220 break;
1221
1222 case OPT_PLAYBACK_LAT:
1223 cfg->playback_lat = atoi(pj_optarg);
1224 break;
1225
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001226 case OPT_SND_AUTO_CLOSE:
1227 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1228 break;
1229
Benny Prijono91d20f42008-06-14 19:42:37 +00001230 case OPT_NO_TONES:
1231 cfg->no_tones = PJ_TRUE;
1232 break;
1233
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001234 case OPT_JB_MAX_SIZE:
1235 cfg->media_cfg.jb_max = atoi(pj_optarg);
1236 break;
1237
Benny Prijono22a300a2006-06-14 20:04:55 +00001238 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001239 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001240 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001241 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001242 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001243 }
1244 }
1245
1246 if (pj_optind != argc) {
1247 pj_str_t uri_arg;
1248
1249 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1250 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1251 return -1;
1252 }
1253 uri_arg = pj_str(argv[pj_optind]);
1254 if (uri_to_call)
1255 *uri_to_call = uri_arg;
1256 pj_optind++;
1257
1258 /* Add URI to call to buddy list if it's not already there */
1259 for (i=0; i<cfg->buddy_cnt; ++i) {
1260 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1261 break;
1262 }
1263 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1264 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1265 }
1266
1267 } else {
1268 if (uri_to_call)
1269 uri_to_call->slen = 0;
1270 }
1271
1272 if (pj_optind != argc) {
1273 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1274 return PJ_EINVAL;
1275 }
1276
Benny Prijono56315612006-07-18 14:39:40 +00001277 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1278 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001279
1280 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001281 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1282
1283 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001284 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001285 acfg->cred_count++;
1286 }
1287
1288 /* When IMS mode is enabled for the account, verify that settings
1289 * are okay.
1290 */
1291 /* For now we check if IMS mode is activated by looking if
1292 * initial_auth is set.
1293 */
1294 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1295 /* Realm must point to the real domain */
1296 if (*acfg->cred_info[0].realm.ptr=='*') {
1297 PJ_LOG(1,(THIS_FILE,
1298 "Error: cannot use '*' as realm with IMS"));
1299 return PJ_EINVAL;
1300 }
1301
1302 /* Username for authentication must be in a@b format */
1303 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1304 PJ_LOG(1,(THIS_FILE,
1305 "Error: Username for authentication must "
1306 "be in user@domain format with IMS"));
1307 return PJ_EINVAL;
1308 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001309 }
1310 }
1311
1312
1313 return PJ_SUCCESS;
1314}
1315
1316
1317/*
1318 * Save account settings
1319 */
1320static void write_account_settings(int acc_index, pj_str_t *result)
1321{
1322 unsigned i;
1323 char line[128];
1324 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1325
1326
1327 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1328 pj_strcat2(result, line);
1329
1330
1331 /* Identity */
1332 if (acc_cfg->id.slen) {
1333 pj_ansi_sprintf(line, "--id %.*s\n",
1334 (int)acc_cfg->id.slen,
1335 acc_cfg->id.ptr);
1336 pj_strcat2(result, line);
1337 }
1338
1339 /* Registrar server */
1340 if (acc_cfg->reg_uri.slen) {
1341 pj_ansi_sprintf(line, "--registrar %.*s\n",
1342 (int)acc_cfg->reg_uri.slen,
1343 acc_cfg->reg_uri.ptr);
1344 pj_strcat2(result, line);
1345
1346 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1347 acc_cfg->reg_timeout);
1348 pj_strcat2(result, line);
1349 }
1350
1351 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001352 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001353 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001354 (int)acc_cfg->force_contact.slen,
1355 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001356 pj_strcat2(result, line);
1357 }
1358
Benny Prijonofce28542007-12-09 15:41:10 +00001359 /* */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001360 if (acc_cfg->allow_contact_rewrite==0)
Benny Prijonofce28542007-12-09 15:41:10 +00001361 {
Benny Prijono251e02a2008-07-25 10:05:55 +00001362 pj_ansi_sprintf(line, "--auto-update-nat %i\n",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001363 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001364 pj_strcat2(result, line);
1365 }
1366
Benny Prijonod8179652008-01-23 20:39:07 +00001367#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1368 /* SRTP */
1369 if (acc_cfg->use_srtp) {
1370 pj_ansi_sprintf(line, "--use-srtp %i\n",
1371 (int)acc_cfg->use_srtp);
1372 pj_strcat2(result, line);
1373 }
1374#endif
1375
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001376 /* Proxy */
1377 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1378 pj_ansi_sprintf(line, "--proxy %.*s\n",
1379 (int)acc_cfg->proxy[i].slen,
1380 acc_cfg->proxy[i].ptr);
1381 pj_strcat2(result, line);
1382 }
1383
1384 /* Credentials */
1385 for (i=0; i<acc_cfg->cred_count; ++i) {
1386 if (acc_cfg->cred_info[i].realm.slen) {
1387 pj_ansi_sprintf(line, "--realm %.*s\n",
1388 (int)acc_cfg->cred_info[i].realm.slen,
1389 acc_cfg->cred_info[i].realm.ptr);
1390 pj_strcat2(result, line);
1391 }
1392
1393 if (acc_cfg->cred_info[i].username.slen) {
1394 pj_ansi_sprintf(line, "--username %.*s\n",
1395 (int)acc_cfg->cred_info[i].username.slen,
1396 acc_cfg->cred_info[i].username.ptr);
1397 pj_strcat2(result, line);
1398 }
1399
1400 if (acc_cfg->cred_info[i].data.slen) {
1401 pj_ansi_sprintf(line, "--password %.*s\n",
1402 (int)acc_cfg->cred_info[i].data.slen,
1403 acc_cfg->cred_info[i].data.ptr);
1404 pj_strcat2(result, line);
1405 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001406
1407 if (i != acc_cfg->cred_count - 1)
1408 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001409 }
1410
1411}
1412
1413
1414/*
1415 * Write settings.
1416 */
1417static int write_settings(const struct app_config *config,
1418 char *buf, pj_size_t max)
1419{
1420 unsigned acc_index;
1421 unsigned i;
1422 pj_str_t cfg;
1423 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001424 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001425
1426 PJ_UNUSED_ARG(max);
1427
1428 cfg.ptr = buf;
1429 cfg.slen = 0;
1430
1431 /* Logging. */
1432 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1433 pj_ansi_sprintf(line, "--log-level %d\n",
1434 config->log_cfg.level);
1435 pj_strcat2(&cfg, line);
1436
1437 pj_ansi_sprintf(line, "--app-log-level %d\n",
1438 config->log_cfg.console_level);
1439 pj_strcat2(&cfg, line);
1440
1441 if (config->log_cfg.log_filename.slen) {
1442 pj_ansi_sprintf(line, "--log-file %.*s\n",
1443 (int)config->log_cfg.log_filename.slen,
1444 config->log_cfg.log_filename.ptr);
1445 pj_strcat2(&cfg, line);
1446 }
1447
1448
1449 /* Save account settings. */
1450 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1451
1452 write_account_settings(acc_index, &cfg);
1453
1454 if (acc_index < config->acc_cnt-1)
1455 pj_strcat2(&cfg, "--next-account\n");
1456 }
1457
1458
1459 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1460
1461 /* Outbound proxy */
1462 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1463 pj_ansi_sprintf(line, "--outbound %.*s\n",
1464 (int)config->cfg.outbound_proxy[i].slen,
1465 config->cfg.outbound_proxy[i].ptr);
1466 pj_strcat2(&cfg, line);
1467 }
1468
1469
1470 /* UDP Transport. */
1471 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1472 pj_strcat2(&cfg, line);
1473
Benny Prijono0a5cad82006-09-26 13:21:02 +00001474 /* IP address, if any. */
1475 if (config->udp_cfg.public_addr.slen) {
1476 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1477 (int)config->udp_cfg.public_addr.slen,
1478 config->udp_cfg.public_addr.ptr);
1479 pj_strcat2(&cfg, line);
1480 }
1481
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001482 /* No TCP ? */
1483 if (config->no_tcp) {
1484 pj_strcat2(&cfg, "--no-tcp\n");
1485 }
1486
1487 /* No UDP ? */
1488 if (config->no_udp) {
1489 pj_strcat2(&cfg, "--no-udp\n");
1490 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001491
1492 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001493 if (config->cfg.stun_domain.slen) {
1494 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1495 (int)config->cfg.stun_domain.slen,
1496 config->cfg.stun_domain.ptr);
1497 pj_strcat2(&cfg, line);
1498 }
1499 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001500 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001501 (int)config->cfg.stun_host.slen,
1502 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001503 pj_strcat2(&cfg, line);
1504 }
1505
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001506 /* TLS */
1507 if (config->use_tls)
1508 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001509 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001510 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001511 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1512 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001513 pj_strcat2(&cfg, line);
1514 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001515 if (config->udp_cfg.tls_setting.cert_file.slen) {
1516 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1517 (int)config->udp_cfg.tls_setting.cert_file.slen,
1518 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001519 pj_strcat2(&cfg, line);
1520 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001521 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1522 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1523 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1524 config->udp_cfg.tls_setting.privkey_file.ptr);
1525 pj_strcat2(&cfg, line);
1526 }
1527
1528 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001529 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001530 (int)config->udp_cfg.tls_setting.password.slen,
1531 config->udp_cfg.tls_setting.password.ptr);
1532 pj_strcat2(&cfg, line);
1533 }
1534
Benny Prijonoe10db842008-07-01 15:31:59 +00001535 if (config->udp_cfg.tls_setting.server_name.slen) {
1536 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1537 (int)config->udp_cfg.tls_setting.server_name.slen,
1538 config->udp_cfg.tls_setting.server_name.ptr);
1539 pj_strcat2(&cfg, line);
1540 }
1541
Benny Prijonof3bbc132006-12-25 06:43:59 +00001542 if (config->udp_cfg.tls_setting.verify_server)
1543 pj_strcat2(&cfg, "--tls-verify-server\n");
1544
1545 if (config->udp_cfg.tls_setting.verify_client)
1546 pj_strcat2(&cfg, "--tls-verify-client\n");
1547
1548 if (config->udp_cfg.tls_setting.timeout.sec) {
1549 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001550 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001551 pj_strcat2(&cfg, line);
1552 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001553
1554 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1555
Benny Prijonof6508982008-01-25 09:02:33 +00001556 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001557#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001558 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1559 pj_ansi_sprintf(line, "--use-srtp %d\n",
1560 app_config.cfg.use_srtp);
1561 pj_strcat2(&cfg, line);
1562 }
1563 if (app_config.cfg.srtp_secure_signaling !=
1564 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1565 {
1566 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1567 app_config.cfg.srtp_secure_signaling);
1568 pj_strcat2(&cfg, line);
1569 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001570#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001571
Benny Prijonof76e1392008-06-06 14:51:48 +00001572 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001573 if (config->media_cfg.enable_ice)
1574 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001575
1576 if (config->media_cfg.enable_turn)
1577 pj_strcat2(&cfg, "--use-turn\n");
1578
1579 if (config->media_cfg.ice_no_host_cands)
1580 pj_strcat2(&cfg, "--ice-no-host\n");
1581
Benny Prijono551af422008-08-07 09:55:52 +00001582 if (config->media_cfg.ice_no_rtcp)
1583 pj_strcat2(&cfg, "--ice-no-rtcp\n");
1584
Benny Prijonof76e1392008-06-06 14:51:48 +00001585 if (config->media_cfg.turn_server.slen) {
1586 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1587 (int)config->media_cfg.turn_server.slen,
1588 config->media_cfg.turn_server.ptr);
1589 pj_strcat2(&cfg, line);
1590 }
1591
1592 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1593 pj_strcat2(&cfg, "--turn-tcp\n");
1594
1595 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1596 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1597 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1598 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1599 pj_strcat2(&cfg, line);
1600 }
1601
1602 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1603 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1604 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1605 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1606 pj_strcat2(&cfg, line);
1607 }
1608
1609 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001610 if (config->null_audio)
1611 pj_strcat2(&cfg, "--null-audio\n");
1612 if (config->auto_play)
1613 pj_strcat2(&cfg, "--auto-play\n");
1614 if (config->auto_loop)
1615 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001616 if (config->auto_conf)
1617 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001618 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001619 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001620 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001621 pj_strcat2(&cfg, line);
1622 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001623 for (i=0; i<config->tone_count; ++i) {
1624 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1625 config->tones[i].freq1, config->tones[i].freq2,
1626 config->tones[i].on_msec, config->tones[i].off_msec);
1627 pj_strcat2(&cfg, line);
1628 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001629 if (config->rec_file.slen) {
1630 pj_ansi_sprintf(line, "--rec-file %s\n",
1631 config->rec_file.ptr);
1632 pj_strcat2(&cfg, line);
1633 }
1634 if (config->auto_rec)
1635 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001636 if (config->capture_dev != PJSUA_INVALID_ID) {
1637 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1638 pj_strcat2(&cfg, line);
1639 }
1640 if (config->playback_dev != PJSUA_INVALID_ID) {
1641 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1642 pj_strcat2(&cfg, line);
1643 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001644 if (config->media_cfg.snd_auto_close_time != -1) {
1645 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1646 config->media_cfg.snd_auto_close_time);
1647 pj_strcat2(&cfg, line);
1648 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001649 if (config->no_tones) {
1650 pj_strcat2(&cfg, "--no-tones\n");
1651 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001652 if (config->media_cfg.jb_max != -1) {
1653 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1654 config->media_cfg.jb_max);
1655 pj_strcat2(&cfg, line);
1656 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001657
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001658 /* Sound device latency */
1659 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1660 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1661 pj_strcat2(&cfg, line);
1662 }
1663 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1664 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1665 pj_strcat2(&cfg, line);
1666 }
1667
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001668 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001669 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001670 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001671 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001672 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001673 } else {
1674 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1675 config->media_cfg.clock_rate);
1676 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001677 }
Benny Prijono70972992006-08-05 11:13:58 +00001678
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001679 if (config->media_cfg.snd_clock_rate &&
1680 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1681 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001682 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1683 config->media_cfg.snd_clock_rate);
1684 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001685 }
1686
Benny Prijono7d60d052008-03-29 12:24:20 +00001687 /* Stereo mode. */
1688 if (config->media_cfg.channel_count == 2) {
1689 pj_ansi_sprintf(line, "--stereo\n");
1690 pj_strcat2(&cfg, line);
1691 }
1692
Benny Prijono70972992006-08-05 11:13:58 +00001693 /* quality */
1694 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001695 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001696 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001697 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001698 } else {
1699 pj_ansi_sprintf(line, "#using default --quality %d\n",
1700 config->media_cfg.quality);
1701 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001702 }
Benny Prijono0498d902006-06-19 14:49:14 +00001703
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001704
1705 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001706 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001707 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001708 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001709 pj_strcat2(&cfg, line);
1710 }
1711
Benny Prijono70972992006-08-05 11:13:58 +00001712 /* no-vad */
1713 if (config->media_cfg.no_vad) {
1714 pj_strcat2(&cfg, "--no-vad\n");
1715 }
1716
1717 /* ec-tail */
1718 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1719 pj_ansi_sprintf(line, "--ec-tail %d\n",
1720 config->media_cfg.ec_tail_len);
1721 pj_strcat2(&cfg, line);
1722 } else {
1723 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1724 config->media_cfg.ec_tail_len);
1725 pj_strcat2(&cfg, line);
1726 }
1727
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001728 /* ec-opt */
1729 if (config->media_cfg.ec_options != 0) {
1730 pj_ansi_sprintf(line, "--ec-opt %d\n",
1731 config->media_cfg.ec_options);
1732 pj_strcat2(&cfg, line);
1733 }
Benny Prijono70972992006-08-05 11:13:58 +00001734
1735 /* ilbc-mode */
1736 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1737 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1738 config->media_cfg.ilbc_mode);
1739 pj_strcat2(&cfg, line);
1740 } else {
1741 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1742 config->media_cfg.ilbc_mode);
1743 pj_strcat2(&cfg, line);
1744 }
1745
1746 /* RTP drop */
1747 if (config->media_cfg.tx_drop_pct) {
1748 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1749 config->media_cfg.tx_drop_pct);
1750 pj_strcat2(&cfg, line);
1751
1752 }
1753 if (config->media_cfg.rx_drop_pct) {
1754 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1755 config->media_cfg.rx_drop_pct);
1756 pj_strcat2(&cfg, line);
1757
1758 }
1759
1760
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001761 /* Start RTP port. */
1762 pj_ansi_sprintf(line, "--rtp-port %d\n",
1763 config->rtp_cfg.port);
1764 pj_strcat2(&cfg, line);
1765
1766 /* Add codec. */
1767 for (i=0; i<config->codec_cnt; ++i) {
1768 pj_ansi_sprintf(line, "--add-codec %s\n",
1769 config->codec_arg[i].ptr);
1770 pj_strcat2(&cfg, line);
1771 }
Benny Prijonofce28542007-12-09 15:41:10 +00001772 /* Disable codec */
1773 for (i=0; i<config->codec_dis_cnt; ++i) {
1774 pj_ansi_sprintf(line, "--dis-codec %s\n",
1775 config->codec_dis[i].ptr);
1776 pj_strcat2(&cfg, line);
1777 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001778
1779 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1780
1781 /* Auto-answer. */
1782 if (config->auto_answer != 0) {
1783 pj_ansi_sprintf(line, "--auto-answer %d\n",
1784 config->auto_answer);
1785 pj_strcat2(&cfg, line);
1786 }
1787
1788 /* Max calls. */
1789 pj_ansi_sprintf(line, "--max-calls %d\n",
1790 config->cfg.max_calls);
1791 pj_strcat2(&cfg, line);
1792
1793 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001794 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001795 pj_ansi_sprintf(line, "--duration %d\n",
1796 config->duration);
1797 pj_strcat2(&cfg, line);
1798 }
1799
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001800 /* norefersub ? */
1801 if (config->no_refersub) {
1802 pj_strcat2(&cfg, "--norefersub\n");
1803 }
1804
Benny Prijonofce28542007-12-09 15:41:10 +00001805 if (pjsip_use_compact_form)
1806 {
1807 pj_strcat2(&cfg, "--use-compact-form\n");
1808 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001809
Benny Prijono91d06b62008-09-20 12:16:56 +00001810 if (config->cfg.force_lr) {
1811 pj_strcat2(&cfg, "--no-force-lr\n");
1812 }
1813
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001814 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1815
1816 /* Add buddies. */
1817 for (i=0; i<config->buddy_cnt; ++i) {
1818 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1819 (int)config->buddy_cfg[i].uri.slen,
1820 config->buddy_cfg[i].uri.ptr);
1821 pj_strcat2(&cfg, line);
1822 }
1823
1824
1825 *(cfg.ptr + cfg.slen) = '\0';
1826 return cfg.slen;
1827}
1828
1829
1830/*
1831 * Dump application states.
1832 */
1833static void app_dump(pj_bool_t detail)
1834{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001835 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001836}
1837
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001838/*
1839 * Print log of call states. Since call states may be too long for logger,
1840 * printing it is a bit tricky, it should be printed part by part as long
1841 * as the logger can accept.
1842 */
1843static void log_call_dump(int call_id) {
1844 unsigned call_dump_len;
1845 unsigned part_len;
1846 unsigned part_idx;
1847 unsigned log_decor;
1848
1849 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1850 sizeof(some_buf), " ");
1851 call_dump_len = strlen(some_buf);
1852
1853 log_decor = pj_log_get_decor();
1854 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1855 PJ_LOG(3,(THIS_FILE, "\n"));
1856 pj_log_set_decor(0);
1857
1858 part_idx = 0;
1859 part_len = PJ_LOG_MAX_SIZE-80;
1860 while (part_idx < call_dump_len) {
1861 char p_orig, *p;
1862
1863 p = &some_buf[part_idx];
1864 if (part_idx + part_len > call_dump_len)
1865 part_len = call_dump_len - part_idx;
1866 p_orig = p[part_len];
1867 p[part_len] = '\0';
1868 PJ_LOG(3,(THIS_FILE, "%s", p));
1869 p[part_len] = p_orig;
1870 part_idx += part_len;
1871 }
1872 pj_log_set_decor(log_decor);
1873}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001874
1875/*****************************************************************************
1876 * Console application
1877 */
1878
Benny Prijono91d20f42008-06-14 19:42:37 +00001879static void ringback_start(pjsua_call_id call_id)
1880{
1881 if (app_config.no_tones)
1882 return;
1883
1884 if (app_config.call_data[call_id].ringback_on)
1885 return;
1886
1887 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1888
1889 if (++app_config.ringback_cnt==1 &&
1890 app_config.ringback_slot!=PJSUA_INVALID_ID)
1891 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001892 pjsua_conf_connect(app_config.ringback_slot, 0);
1893 }
1894}
1895
1896static void ring_stop(pjsua_call_id call_id)
1897{
1898 if (app_config.no_tones)
1899 return;
1900
1901 if (app_config.call_data[call_id].ringback_on) {
1902 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1903
1904 pj_assert(app_config.ringback_cnt>0);
1905 if (--app_config.ringback_cnt == 0 &&
1906 app_config.ringback_slot!=PJSUA_INVALID_ID)
1907 {
1908 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001909 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001910 }
1911 }
1912
1913 if (app_config.call_data[call_id].ring_on) {
1914 app_config.call_data[call_id].ring_on = PJ_FALSE;
1915
1916 pj_assert(app_config.ring_cnt>0);
1917 if (--app_config.ring_cnt == 0 &&
1918 app_config.ring_slot!=PJSUA_INVALID_ID)
1919 {
1920 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001921 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001922 }
1923 }
1924}
1925
1926static void ring_start(pjsua_call_id call_id)
1927{
1928 if (app_config.no_tones)
1929 return;
1930
1931 if (app_config.call_data[call_id].ring_on)
1932 return;
1933
1934 app_config.call_data[call_id].ring_on = PJ_TRUE;
1935
1936 if (++app_config.ring_cnt==1 &&
1937 app_config.ring_slot!=PJSUA_INVALID_ID)
1938 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001939 pjsua_conf_connect(app_config.ring_slot, 0);
1940 }
1941}
1942
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001943/*
1944 * Find next call when current call is disconnected or when user
1945 * press ']'
1946 */
1947static pj_bool_t find_next_call(void)
1948{
1949 int i, max;
1950
1951 max = pjsua_call_get_max_count();
1952 for (i=current_call+1; i<max; ++i) {
1953 if (pjsua_call_is_active(i)) {
1954 current_call = i;
1955 return PJ_TRUE;
1956 }
1957 }
1958
1959 for (i=0; i<current_call; ++i) {
1960 if (pjsua_call_is_active(i)) {
1961 current_call = i;
1962 return PJ_TRUE;
1963 }
1964 }
1965
1966 current_call = PJSUA_INVALID_ID;
1967 return PJ_FALSE;
1968}
1969
1970
1971/*
1972 * Find previous call when user press '['
1973 */
1974static pj_bool_t find_prev_call(void)
1975{
1976 int i, max;
1977
1978 max = pjsua_call_get_max_count();
1979 for (i=current_call-1; i>=0; --i) {
1980 if (pjsua_call_is_active(i)) {
1981 current_call = i;
1982 return PJ_TRUE;
1983 }
1984 }
1985
1986 for (i=max-1; i>current_call; --i) {
1987 if (pjsua_call_is_active(i)) {
1988 current_call = i;
1989 return PJ_TRUE;
1990 }
1991 }
1992
1993 current_call = PJSUA_INVALID_ID;
1994 return PJ_FALSE;
1995}
1996
1997
Benny Prijono804ff0a2006-09-14 11:17:48 +00001998/* Callback from timer when the maximum call duration has been
1999 * exceeded.
2000 */
2001static void call_timeout_callback(pj_timer_heap_t *timer_heap,
2002 struct pj_timer_entry *entry)
2003{
2004 pjsua_call_id call_id = entry->id;
2005 pjsua_msg_data msg_data;
2006 pjsip_generic_string_hdr warn;
2007 pj_str_t hname = pj_str("Warning");
2008 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
2009
2010 PJ_UNUSED_ARG(timer_heap);
2011
Benny Prijono148c9dd2006-09-19 13:37:53 +00002012 if (call_id == PJSUA_INVALID_ID) {
2013 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
2014 return;
2015 }
2016
Benny Prijono804ff0a2006-09-14 11:17:48 +00002017 /* Add warning header */
2018 pjsua_msg_data_init(&msg_data);
2019 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
2020 pj_list_push_back(&msg_data.hdr_list, &warn);
2021
2022 /* Call duration has been exceeded; disconnect the call */
2023 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
2024 "for call %d, disconnecting the call",
2025 app_config.duration, call_id));
2026 entry->id = PJSUA_INVALID_ID;
2027 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
2028}
2029
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002030
2031/*
2032 * Handler when invite state has changed.
2033 */
2034static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
2035{
2036 pjsua_call_info call_info;
2037
2038 PJ_UNUSED_ARG(e);
2039
2040 pjsua_call_get_info(call_id, &call_info);
2041
2042 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2043
Benny Prijono91d20f42008-06-14 19:42:37 +00002044 /* Stop all ringback for this call */
2045 ring_stop(call_id);
2046
Benny Prijono804ff0a2006-09-14 11:17:48 +00002047 /* Cancel duration timer, if any */
2048 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2049 struct call_data *cd = &app_config.call_data[call_id];
2050 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2051
2052 cd->timer.id = PJSUA_INVALID_ID;
2053 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2054 }
2055
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002056 /* Rewind play file when hangup automatically,
2057 * since file is not looped
2058 */
2059 if (app_config.auto_play_hangup)
2060 pjsua_player_set_pos(app_config.wav_id, 0);
2061
2062
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002063 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2064 call_id,
2065 call_info.last_status,
2066 call_info.last_status_text.ptr));
2067
2068 if (call_id == current_call) {
2069 find_next_call();
2070 }
2071
Benny Prijono4be63b52006-11-25 14:50:25 +00002072 /* Dump media state upon disconnected */
2073 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002074 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002075 "Call %d disconnected, dumping media stats..",
2076 call_id));
2077 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002078 }
2079
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002080 } else {
2081
Benny Prijono804ff0a2006-09-14 11:17:48 +00002082 if (app_config.duration!=NO_LIMIT &&
2083 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2084 {
2085 /* Schedule timer to hangup call after the specified duration */
2086 struct call_data *cd = &app_config.call_data[call_id];
2087 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2088 pj_time_val delay;
2089
2090 cd->timer.id = call_id;
2091 delay.sec = app_config.duration;
2092 delay.msec = 0;
2093 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2094 }
2095
Benny Prijono4be63b52006-11-25 14:50:25 +00002096 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2097 int code;
2098 pj_str_t reason;
2099 pjsip_msg *msg;
2100
2101 /* This can only occur because of TX or RX message */
2102 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2103
2104 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2105 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2106 } else {
2107 msg = e->body.tsx_state.src.tdata->msg;
2108 }
2109
2110 code = msg->line.status.code;
2111 reason = msg->line.status.reason;
2112
Benny Prijono91d20f42008-06-14 19:42:37 +00002113 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2114 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2115 msg->body == NULL &&
2116 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2117 {
2118 ringback_start(call_id);
2119 }
2120
Benny Prijono4be63b52006-11-25 14:50:25 +00002121 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2122 call_id, call_info.state_text.ptr,
2123 code, (int)reason.slen, reason.ptr));
2124 } else {
2125 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2126 call_id,
2127 call_info.state_text.ptr));
2128 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002129
2130 if (current_call==PJSUA_INVALID_ID)
2131 current_call = call_id;
2132
2133 }
2134}
2135
2136
2137/**
2138 * Handler when there is incoming call.
2139 */
2140static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2141 pjsip_rx_data *rdata)
2142{
2143 pjsua_call_info call_info;
2144
2145 PJ_UNUSED_ARG(acc_id);
2146 PJ_UNUSED_ARG(rdata);
2147
2148 pjsua_call_get_info(call_id, &call_info);
2149
Benny Prijono91d20f42008-06-14 19:42:37 +00002150 /* Start ringback */
2151 ring_start(call_id);
2152
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002153 if (app_config.auto_answer > 0) {
2154 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2155 }
2156
2157 if (app_config.auto_answer < 200) {
2158 PJ_LOG(3,(THIS_FILE,
2159 "Incoming call for account %d!\n"
2160 "From: %s\n"
2161 "To: %s\n"
2162 "Press a to answer or h to reject call",
2163 acc_id,
2164 call_info.remote_info.ptr,
2165 call_info.local_info.ptr));
2166 }
2167}
2168
2169
2170/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002171 * Handler when a transaction within a call has changed state.
2172 */
2173static void on_call_tsx_state(pjsua_call_id call_id,
2174 pjsip_transaction *tsx,
2175 pjsip_event *e)
2176{
2177 const pjsip_method info_method =
2178 {
2179 PJSIP_OTHER_METHOD,
2180 { "INFO", 4 }
2181 };
2182
2183 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2184 /*
2185 * Handle INFO method.
2186 */
2187 if (tsx->role == PJSIP_ROLE_UAC &&
2188 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002189 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2190 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002191 {
2192 /* Status of outgoing INFO request */
2193 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2194 PJ_LOG(4,(THIS_FILE,
2195 "Call %d: DTMF sent successfully with INFO",
2196 call_id));
2197 } else if (tsx->status_code >= 300) {
2198 PJ_LOG(4,(THIS_FILE,
2199 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2200 call_id,
2201 tsx->status_code,
2202 (int)tsx->status_text.slen,
2203 tsx->status_text.ptr));
2204 }
2205 } else if (tsx->role == PJSIP_ROLE_UAS &&
2206 tsx->state == PJSIP_TSX_STATE_TRYING)
2207 {
2208 /* Answer incoming INFO with 200/OK */
2209 pjsip_rx_data *rdata;
2210 pjsip_tx_data *tdata;
2211 pj_status_t status;
2212
2213 rdata = e->body.tsx_state.src.rdata;
2214
2215 if (rdata->msg_info.msg->body) {
2216 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2217 200, NULL, &tdata);
2218 if (status == PJ_SUCCESS)
2219 status = pjsip_tsx_send_msg(tsx, tdata);
2220
2221 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2222 call_id,
2223 (int)rdata->msg_info.msg->body->len,
2224 rdata->msg_info.msg->body->data));
2225 } else {
2226 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2227 400, NULL, &tdata);
2228 if (status == PJ_SUCCESS)
2229 status = pjsip_tsx_send_msg(tsx, tdata);
2230 }
2231 }
2232 }
2233}
2234
2235
2236/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002237 * Callback on media state changed event.
2238 * The action may connect the call to sound device, to file, or
2239 * to loop the call.
2240 */
2241static void on_call_media_state(pjsua_call_id call_id)
2242{
2243 pjsua_call_info call_info;
2244
2245 pjsua_call_get_info(call_id, &call_info);
2246
Benny Prijono91d20f42008-06-14 19:42:37 +00002247 /* Stop ringback */
2248 ring_stop(call_id);
2249
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002250 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
2251 * otherwise we should NOT connect the ports.
2252 */
2253 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE ||
2254 call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
2255 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002256 pj_bool_t connect_sound = PJ_TRUE;
2257
2258 /* Loopback sound, if desired */
2259 if (app_config.auto_loop) {
2260 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2261 connect_sound = PJ_FALSE;
Benny Prijonof6c77f42008-08-13 13:52:19 +00002262 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00002263
Benny Prijonof6c77f42008-08-13 13:52:19 +00002264 /* Automatically record conversation, if desired */
2265 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2266 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002267 }
2268
2269 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002270 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2271 app_config.wav_port != PJSUA_INVALID_ID)
2272 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002273 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2274 connect_sound = PJ_FALSE;
2275 }
2276
Benny Prijono7ca96da2006-08-07 12:11:40 +00002277 /* Put call in conference with other calls, if desired */
2278 if (app_config.auto_conf) {
2279 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002280 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002281 unsigned i;
2282
2283 /* Get all calls, and establish media connection between
2284 * this call and other calls.
2285 */
2286 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002287
Benny Prijono7ca96da2006-08-07 12:11:40 +00002288 for (i=0; i<call_cnt; ++i) {
2289 if (call_ids[i] == call_id)
2290 continue;
2291
2292 if (!pjsua_call_has_media(call_ids[i]))
2293 continue;
2294
2295 pjsua_conf_connect(call_info.conf_slot,
2296 pjsua_call_get_conf_port(call_ids[i]));
2297 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2298 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002299
2300 /* Automatically record conversation, if desired */
2301 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2302 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2303 app_config.rec_port);
2304 }
2305
Benny Prijono7ca96da2006-08-07 12:11:40 +00002306 }
2307
2308 /* Also connect call to local sound device */
2309 connect_sound = PJ_TRUE;
2310 }
2311
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002312 /* Otherwise connect to sound device */
2313 if (connect_sound) {
2314 pjsua_conf_connect(call_info.conf_slot, 0);
2315 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002316
2317 /* Automatically record conversation, if desired */
2318 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2319 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2320 pjsua_conf_connect(0, app_config.rec_port);
2321 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002322 }
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002323 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002324
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002325 /* Handle media status */
2326 switch (call_info.media_status) {
2327 case PJSUA_CALL_MEDIA_ACTIVE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002328 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002329 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002330
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002331 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002332 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2333 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002334 break;
2335
2336 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002337 PJ_LOG(3,(THIS_FILE,
2338 "Media for call %d is suspended (hold) by remote",
2339 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002340 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002341
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002342 case PJSUA_CALL_MEDIA_ERROR:
2343 PJ_LOG(3,(THIS_FILE,
Benny Prijono096c56c2007-09-15 08:30:16 +00002344 "Media has reported error, disconnecting call"));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002345 {
2346 pj_str_t reason = pj_str("ICE negotiation failed");
2347 pjsua_call_hangup(call_id, 500, &reason, NULL);
2348 }
2349 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002350
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002351 case PJSUA_CALL_MEDIA_NONE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002352 PJ_LOG(3,(THIS_FILE,
2353 "Media for call %d is inactive",
2354 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002355 break;
2356
2357 default:
2358 pj_assert(!"Unhandled media status");
2359 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002360 }
2361}
2362
Benny Prijono0875ae82006-12-26 00:11:48 +00002363/*
2364 * DTMF callback.
2365 */
2366static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2367{
2368 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2369}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002370
2371/*
2372 * Handler registration status has changed.
2373 */
2374static void on_reg_state(pjsua_acc_id acc_id)
2375{
2376 PJ_UNUSED_ARG(acc_id);
2377
2378 // Log already written.
2379}
2380
2381
2382/*
Benny Prijono563cd7a2008-08-19 20:02:15 +00002383 * Handler for incoming presence subscription request
2384 */
2385static void on_incoming_subscribe(pjsua_acc_id acc_id,
2386 pjsua_srv_pres *srv_pres,
2387 pjsua_buddy_id buddy_id,
2388 const pj_str_t *from,
2389 pjsip_rx_data *rdata,
2390 pjsip_status_code *code,
2391 pj_str_t *reason,
2392 pjsua_msg_data *msg_data)
2393{
2394 /* Just accept the request (the default behavior) */
2395 PJ_UNUSED_ARG(acc_id);
2396 PJ_UNUSED_ARG(srv_pres);
2397 PJ_UNUSED_ARG(buddy_id);
2398 PJ_UNUSED_ARG(from);
2399 PJ_UNUSED_ARG(rdata);
2400 PJ_UNUSED_ARG(code);
2401 PJ_UNUSED_ARG(reason);
2402 PJ_UNUSED_ARG(msg_data);
2403}
2404
2405
2406/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002407 * Handler on buddy state changed.
2408 */
2409static void on_buddy_state(pjsua_buddy_id buddy_id)
2410{
2411 pjsua_buddy_info info;
2412 pjsua_buddy_get_info(buddy_id, &info);
2413
2414 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2415 (int)info.uri.slen,
2416 info.uri.ptr,
2417 (int)info.status_text.slen,
2418 info.status_text.ptr));
2419}
2420
2421
2422/**
2423 * Incoming IM message (i.e. MESSAGE request)!
2424 */
2425static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2426 const pj_str_t *to, const pj_str_t *contact,
2427 const pj_str_t *mime_type, const pj_str_t *text)
2428{
2429 /* Note: call index may be -1 */
2430 PJ_UNUSED_ARG(call_id);
2431 PJ_UNUSED_ARG(to);
2432 PJ_UNUSED_ARG(contact);
2433 PJ_UNUSED_ARG(mime_type);
2434
Benny Prijonof4b538d2007-05-14 16:45:20 +00002435 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002436 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002437 (int)text->slen, text->ptr,
2438 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002439}
2440
2441
2442/**
2443 * Received typing indication
2444 */
2445static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2446 const pj_str_t *to, const pj_str_t *contact,
2447 pj_bool_t is_typing)
2448{
2449 PJ_UNUSED_ARG(call_id);
2450 PJ_UNUSED_ARG(to);
2451 PJ_UNUSED_ARG(contact);
2452
2453 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2454 (int)from->slen, from->ptr,
2455 (is_typing?"is typing..":"has stopped typing")));
2456}
2457
2458
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002459/**
2460 * Call transfer request status.
2461 */
2462static void on_call_transfer_status(pjsua_call_id call_id,
2463 int status_code,
2464 const pj_str_t *status_text,
2465 pj_bool_t final,
2466 pj_bool_t *p_cont)
2467{
2468 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2469 call_id, status_code,
2470 (int)status_text->slen, status_text->ptr,
2471 (final ? "[final]" : "")));
2472
2473 if (status_code/100 == 2) {
2474 PJ_LOG(3,(THIS_FILE,
2475 "Call %d: call transfered successfully, disconnecting call",
2476 call_id));
2477 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2478 *p_cont = PJ_FALSE;
2479 }
2480}
2481
2482
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002483/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002484 * Notification that call is being replaced.
2485 */
2486static void on_call_replaced(pjsua_call_id old_call_id,
2487 pjsua_call_id new_call_id)
2488{
2489 pjsua_call_info old_ci, new_ci;
2490
2491 pjsua_call_get_info(old_call_id, &old_ci);
2492 pjsua_call_get_info(new_call_id, &new_ci);
2493
2494 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2495 "call %d with %.*s",
2496 old_call_id,
2497 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2498 new_call_id,
2499 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2500}
2501
2502
2503/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002504 * NAT type detection callback.
2505 */
2506static void on_nat_detect(const pj_stun_nat_detect_result *res)
2507{
2508 if (res->status != PJ_SUCCESS) {
2509 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2510 } else {
2511 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2512 }
2513}
2514
2515
2516/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002517 * Print buddy list.
2518 */
2519static void print_buddy_list(void)
2520{
2521 pjsua_buddy_id ids[64];
2522 int i;
2523 unsigned count = PJ_ARRAY_SIZE(ids);
2524
2525 puts("Buddy list:");
2526
2527 pjsua_enum_buddies(ids, &count);
2528
2529 if (count == 0)
2530 puts(" -none-");
2531 else {
2532 for (i=0; i<(int)count; ++i) {
2533 pjsua_buddy_info info;
2534
2535 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2536 continue;
2537
Benny Prijono4461c7d2007-08-25 13:36:15 +00002538 printf(" [%2d] <%.*s> %.*s\n",
2539 ids[i]+1,
2540 (int)info.status_text.slen,
2541 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002542 (int)info.uri.slen,
2543 info.uri.ptr);
2544 }
2545 }
2546 puts("");
2547}
2548
2549
2550/*
2551 * Print account status.
2552 */
2553static void print_acc_status(int acc_id)
2554{
2555 char buf[80];
2556 pjsua_acc_info info;
2557
2558 pjsua_acc_get_info(acc_id, &info);
2559
2560 if (!info.has_registration) {
2561 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2562 (int)info.status_text.slen,
2563 info.status_text.ptr);
2564
2565 } else {
2566 pj_ansi_snprintf(buf, sizeof(buf),
2567 "%d/%.*s (expires=%d)",
2568 info.status,
2569 (int)info.status_text.slen,
2570 info.status_text.ptr,
2571 info.expires);
2572
2573 }
2574
2575 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2576 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002577 printf(" Online status: %.*s\n",
2578 (int)info.online_status_text.slen,
2579 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002580}
2581
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002582/* Playfile done notification, set timer to hangup calls */
2583pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2584{
2585 pj_time_val delay;
2586
2587 PJ_UNUSED_ARG(port);
2588 PJ_UNUSED_ARG(usr_data);
2589
2590 /* Just rewind WAV when it is played outside of call */
2591 if (pjsua_call_get_count() == 0) {
2592 pjsua_player_set_pos(app_config.wav_id, 0);
2593 return PJ_SUCCESS;
2594 }
2595
2596 /* Timer is already active */
2597 if (app_config.auto_hangup_timer.id == 1)
2598 return PJ_SUCCESS;
2599
2600 app_config.auto_hangup_timer.id = 1;
2601 delay.sec = 0;
2602 delay.msec = 200; /* Give 200 ms before hangup */
2603 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2604 &app_config.auto_hangup_timer,
2605 &delay);
2606
2607 return PJ_SUCCESS;
2608}
2609
2610/* Auto hangup timer callback */
2611static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2612 struct pj_timer_entry *entry)
2613{
2614 PJ_UNUSED_ARG(timer_heap);
2615 PJ_UNUSED_ARG(entry);
2616
2617 app_config.auto_hangup_timer.id = 0;
2618 pjsua_call_hangup_all();
2619}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002620
2621/*
2622 * Show a bit of help.
2623 */
2624static void keystroke_help(void)
2625{
2626 pjsua_acc_id acc_ids[16];
2627 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2628 int i;
2629
2630 printf(">>>>\n");
2631
2632 pjsua_enum_accs(acc_ids, &count);
2633
2634 printf("Account list:\n");
2635 for (i=0; i<(int)count; ++i)
2636 print_acc_status(acc_ids[i]);
2637
2638 print_buddy_list();
2639
2640 //puts("Commands:");
2641 puts("+=============================================================================+");
2642 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2643 puts("| | | |");
2644 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2645 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002646 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2647 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2648 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2649 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002650 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2651 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002652 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002653 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002654 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2655 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2656 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2657 puts("| | V Adjust audio Volume | f Save config |");
2658 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002659 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002660 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002661 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002662
2663 i = pjsua_call_get_count();
2664 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002665
2666 if (current_call != PJSUA_INVALID_ID) {
2667 pjsua_call_info ci;
2668 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2669 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2670 (int)ci.remote_info.slen, ci.remote_info.ptr,
2671 (int)ci.state_text.slen, ci.state_text.ptr);
2672 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002673}
2674
2675
2676/*
2677 * Input simple string
2678 */
2679static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2680{
2681 char *p;
2682
2683 printf("%s (empty to cancel): ", title); fflush(stdout);
2684 fgets(buf, len, stdin);
2685
2686 /* Remove trailing newlines. */
2687 for (p=buf; ; ++p) {
2688 if (*p=='\r' || *p=='\n') *p='\0';
2689 else if (!*p) break;
2690 }
2691
2692 if (!*buf)
2693 return PJ_FALSE;
2694
2695 return PJ_TRUE;
2696}
2697
2698
2699#define NO_NB -2
2700struct input_result
2701{
2702 int nb_result;
2703 char *uri_result;
2704};
2705
2706
2707/*
2708 * Input URL.
2709 */
2710static void ui_input_url(const char *title, char *buf, int len,
2711 struct input_result *result)
2712{
2713 result->nb_result = NO_NB;
2714 result->uri_result = NULL;
2715
2716 print_buddy_list();
2717
2718 printf("Choices:\n"
2719 " 0 For current dialog.\n"
2720 " -1 All %d buddies in buddy list\n"
2721 " [1 -%2d] Select from buddy list\n"
2722 " URL An URL\n"
2723 " <Enter> Empty input (or 'q') to cancel\n"
2724 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2725 printf("%s: ", title);
2726
2727 fflush(stdout);
2728 fgets(buf, len, stdin);
2729 len = strlen(buf);
2730
2731 /* Left trim */
2732 while (pj_isspace(*buf)) {
2733 ++buf;
2734 --len;
2735 }
2736
2737 /* Remove trailing newlines */
2738 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2739 buf[--len] = '\0';
2740
2741 if (len == 0 || buf[0]=='q')
2742 return;
2743
2744 if (pj_isdigit(*buf) || *buf=='-') {
2745
2746 int i;
2747
2748 if (*buf=='-')
2749 i = 1;
2750 else
2751 i = 0;
2752
2753 for (; i<len; ++i) {
2754 if (!pj_isdigit(buf[i])) {
2755 puts("Invalid input");
2756 return;
2757 }
2758 }
2759
2760 result->nb_result = my_atoi(buf);
2761
2762 if (result->nb_result >= 0 &&
2763 result->nb_result <= (int)pjsua_get_buddy_count())
2764 {
2765 return;
2766 }
2767 if (result->nb_result == -1)
2768 return;
2769
2770 puts("Invalid input");
2771 result->nb_result = NO_NB;
2772 return;
2773
2774 } else {
2775 pj_status_t status;
2776
2777 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2778 pjsua_perror(THIS_FILE, "Invalid URL", status);
2779 return;
2780 }
2781
2782 result->uri_result = buf;
2783 }
2784}
2785
2786/*
2787 * List the ports in conference bridge
2788 */
2789static void conf_list(void)
2790{
2791 unsigned i, count;
2792 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2793
2794 printf("Conference ports:\n");
2795
2796 count = PJ_ARRAY_SIZE(id);
2797 pjsua_enum_conf_ports(id, &count);
2798
2799 for (i=0; i<count; ++i) {
2800 char txlist[PJSUA_MAX_CALLS*4+10];
2801 unsigned j;
2802 pjsua_conf_port_info info;
2803
2804 pjsua_conf_get_port_info(id[i], &info);
2805
2806 txlist[0] = '\0';
2807 for (j=0; j<info.listener_cnt; ++j) {
2808 char s[10];
2809 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2810 pj_ansi_strcat(txlist, s);
2811 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002812 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002813 info.slot_id,
2814 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002815 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002816 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002817 (int)info.name.slen,
2818 info.name.ptr,
2819 txlist);
2820
2821 }
2822 puts("");
2823}
2824
2825
2826/*
Benny Prijono56315612006-07-18 14:39:40 +00002827 * Send arbitrary request to remote host
2828 */
2829static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2830{
2831 pj_str_t str_method;
2832 pjsip_method method;
2833 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002834 pjsip_endpoint *endpt;
2835 pj_status_t status;
2836
2837 endpt = pjsua_get_pjsip_endpt();
2838
2839 str_method = pj_str(cstr_method);
2840 pjsip_method_init_np(&method, &str_method);
2841
Benny Prijonofff245c2007-04-02 11:44:47 +00002842 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002843
Benny Prijonob988d762007-12-05 04:30:04 +00002844 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002845 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002846 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002847 return;
2848 }
2849}
2850
2851
2852/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002853 * Change extended online status.
2854 */
2855static void change_online_status(void)
2856{
2857 char menuin[32];
2858 pj_bool_t online_status;
2859 pjrpid_element elem;
2860 int i, choice;
2861
2862 enum {
2863 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2864 };
2865
2866 struct opt {
2867 int id;
2868 char *name;
2869 } opts[] = {
2870 { AVAILABLE, "Available" },
2871 { BUSY, "Busy"},
2872 { OTP, "On the phone"},
2873 { IDLE, "Idle"},
2874 { AWAY, "Away"},
2875 { BRB, "Be right back"},
2876 { OFFLINE, "Offline"}
2877 };
2878
2879 printf("\n"
2880 "Choices:\n");
2881 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2882 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2883 }
2884
2885 if (!simple_input("Select status", menuin, sizeof(menuin)))
2886 return;
2887
2888 choice = atoi(menuin) - 1;
2889 if (choice < 0 || choice >= OPT_MAX) {
2890 puts("Invalid selection");
2891 return;
2892 }
2893
2894 pj_bzero(&elem, sizeof(elem));
2895 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2896
2897 online_status = PJ_TRUE;
2898
2899 switch (choice) {
2900 case AVAILABLE:
2901 break;
2902 case BUSY:
2903 elem.activity = PJRPID_ACTIVITY_BUSY;
2904 elem.note = pj_str("Busy");
2905 break;
2906 case OTP:
2907 elem.activity = PJRPID_ACTIVITY_BUSY;
2908 elem.note = pj_str("On the phone");
2909 break;
2910 case IDLE:
2911 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2912 elem.note = pj_str("Idle");
2913 break;
2914 case AWAY:
2915 elem.activity = PJRPID_ACTIVITY_AWAY;
2916 elem.note = pj_str("Away");
2917 break;
2918 case BRB:
2919 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2920 elem.note = pj_str("Be right back");
2921 break;
2922 case OFFLINE:
2923 online_status = PJ_FALSE;
2924 break;
2925 }
2926
2927 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2928}
2929
2930
2931/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002932 * Change codec priorities.
2933 */
2934static void manage_codec_prio(void)
2935{
2936 pjsua_codec_info c[32];
2937 unsigned i, count = PJ_ARRAY_SIZE(c);
2938 char input[32];
2939 char *codec, *prio;
2940 pj_str_t id;
2941 int new_prio;
2942 pj_status_t status;
2943
2944 printf("List of codecs:\n");
2945
2946 pjsua_enum_codecs(c, &count);
2947 for (i=0; i<count; ++i) {
2948 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2949 c[i].codec_id.ptr);
2950 }
2951
2952 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00002953 puts("Enter codec id and its new priority "
2954 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002955
Benny Prijono88accae2008-06-26 15:48:14 +00002956 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002957 fgets(input, sizeof(input), stdin);
2958 if (input[0]=='\r' || input[0]=='\n') {
2959 puts("Done");
2960 return;
2961 }
2962
2963 codec = strtok(input, " \t\r\n");
2964 prio = strtok(NULL, " \r\n");
2965
2966 if (!codec || !prio) {
2967 puts("Invalid input");
2968 return;
2969 }
2970
2971 new_prio = atoi(prio);
2972 if (new_prio < 0)
2973 new_prio = 0;
2974 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2975 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2976
2977 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2978 (pj_uint8_t)new_prio);
2979 if (status != PJ_SUCCESS)
2980 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2981}
2982
2983
2984/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002985 * Main "user interface" loop.
2986 */
2987void console_app_main(const pj_str_t *uri_to_call)
2988{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002989 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002990 char buf[128];
2991 char text[128];
2992 int i, count;
2993 char *uri;
2994 pj_str_t tmp;
2995 struct input_result result;
2996 pjsua_call_info call_info;
2997 pjsua_acc_info acc_info;
2998
2999
3000 /* If user specifies URI to call, then call the URI */
3001 if (uri_to_call->slen) {
3002 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
3003 }
3004
3005 keystroke_help();
3006
3007 for (;;) {
3008
3009 printf(">>> ");
3010 fflush(stdout);
3011
Benny Prijono990042e2007-01-21 19:36:00 +00003012 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
3013 /*
3014 * Be friendly to users who redirect commands into
3015 * program, when file ends, resume with kbd.
3016 * If exit is desired end script with q for quit
3017 */
3018 /* Reopen stdin/stdout/stderr to /dev/console */
3019#if defined(PJ_WIN32) && PJ_WIN32!=0
3020 if (freopen ("CONIN$", "r", stdin) == NULL) {
3021#else
3022 if (1) {
3023#endif
3024 puts("Cannot switch back to console from file redirection");
3025 menuin[0] = 'q';
3026 menuin[1] = '\0';
3027 } else {
3028 puts("Switched back to console from file redirection");
3029 continue;
3030 }
3031 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003032
Benny Prijonoebc32c32008-06-12 13:30:39 +00003033 if (cmd_echo) {
3034 printf("%s", menuin);
3035 }
3036
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003037 switch (menuin[0]) {
3038
3039 case 'm':
3040 /* Make call! : */
3041 printf("(You currently have %d calls)\n",
3042 pjsua_call_get_count());
3043
3044 uri = NULL;
3045 ui_input_url("Make call", buf, sizeof(buf), &result);
3046 if (result.nb_result != NO_NB) {
3047
3048 if (result.nb_result == -1 || result.nb_result == 0) {
3049 puts("You can't do that with make call!");
3050 continue;
3051 } else {
3052 pjsua_buddy_info binfo;
3053 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3054 uri = binfo.uri.ptr;
3055 }
3056
3057 } else if (result.uri_result) {
3058 uri = result.uri_result;
3059 }
3060
3061 tmp = pj_str(uri);
3062 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
3063 break;
3064
3065 case 'M':
3066 /* Make multiple calls! : */
3067 printf("(You currently have %d calls)\n",
3068 pjsua_call_get_count());
3069
3070 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
3071 continue;
3072
3073 count = my_atoi(menuin);
3074 if (count < 1)
3075 continue;
3076
3077 ui_input_url("Make call", buf, sizeof(buf), &result);
3078 if (result.nb_result != NO_NB) {
3079 pjsua_buddy_info binfo;
3080 if (result.nb_result == -1 || result.nb_result == 0) {
3081 puts("You can't do that with make call!");
3082 continue;
3083 }
3084 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3085 uri = binfo.uri.ptr;
3086 } else {
3087 uri = result.uri_result;
3088 }
3089
3090 for (i=0; i<my_atoi(menuin); ++i) {
3091 pj_status_t status;
3092
3093 tmp = pj_str(uri);
3094 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3095 NULL, NULL);
3096 if (status != PJ_SUCCESS)
3097 break;
3098 }
3099 break;
3100
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003101 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003102 i = pjsua_detect_nat_type();
3103 if (i != PJ_SUCCESS)
3104 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003105 break;
3106
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003107 case 'i':
3108 /* Send instant messaeg */
3109
3110 /* i is for call index to send message, if any */
3111 i = -1;
3112
3113 /* Make compiler happy. */
3114 uri = NULL;
3115
3116 /* Input destination. */
3117 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3118 if (result.nb_result != NO_NB) {
3119
3120 if (result.nb_result == -1) {
3121 puts("You can't send broadcast IM like that!");
3122 continue;
3123
3124 } else if (result.nb_result == 0) {
3125
3126 i = current_call;
3127
3128 } else {
3129 pjsua_buddy_info binfo;
3130 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3131 uri = binfo.uri.ptr;
3132 }
3133
3134 } else if (result.uri_result) {
3135 uri = result.uri_result;
3136 }
3137
3138
3139 /* Send typing indication. */
3140 if (i != -1)
3141 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3142 else {
3143 pj_str_t tmp_uri = pj_str(uri);
3144 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3145 }
3146
3147 /* Input the IM . */
3148 if (!simple_input("Message", text, sizeof(text))) {
3149 /*
3150 * Cancelled.
3151 * Send typing notification too, saying we're not typing.
3152 */
3153 if (i != -1)
3154 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3155 else {
3156 pj_str_t tmp_uri = pj_str(uri);
3157 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3158 }
3159 continue;
3160 }
3161
3162 tmp = pj_str(text);
3163
3164 /* Send the IM */
3165 if (i != -1)
3166 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3167 else {
3168 pj_str_t tmp_uri = pj_str(uri);
3169 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3170 }
3171
3172 break;
3173
3174 case 'a':
3175
3176 if (current_call != -1) {
3177 pjsua_call_get_info(current_call, &call_info);
3178 } else {
3179 /* Make compiler happy */
3180 call_info.role = PJSIP_ROLE_UAC;
3181 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3182 }
3183
3184 if (current_call == -1 ||
3185 call_info.role != PJSIP_ROLE_UAS ||
3186 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3187 {
3188 puts("No pending incoming call");
3189 fflush(stdout);
3190 continue;
3191
3192 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003193 int st_code;
3194 char contact[120];
3195 pj_str_t hname = { "Contact", 7 };
3196 pj_str_t hvalue;
3197 pjsip_generic_string_hdr hcontact;
3198 pjsua_msg_data msg_data;
3199
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003200 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3201 continue;
3202
Benny Prijono20d36722007-02-22 14:52:24 +00003203 st_code = my_atoi(buf);
3204 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003205 continue;
3206
Benny Prijono20d36722007-02-22 14:52:24 +00003207 pjsua_msg_data_init(&msg_data);
3208
3209 if (st_code/100 == 3) {
3210 if (!simple_input("Enter URL to be put in Contact",
3211 contact, sizeof(contact)))
3212 continue;
3213 hvalue = pj_str(contact);
3214 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3215
3216 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3217 }
3218
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003219 /*
3220 * Must check again!
3221 * Call may have been disconnected while we're waiting for
3222 * keyboard input.
3223 */
3224 if (current_call == -1) {
3225 puts("Call has been disconnected");
3226 fflush(stdout);
3227 continue;
3228 }
3229
Benny Prijono20d36722007-02-22 14:52:24 +00003230 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003231 }
3232
3233 break;
3234
3235
3236 case 'h':
3237
3238 if (current_call == -1) {
3239 puts("No current call");
3240 fflush(stdout);
3241 continue;
3242
3243 } else if (menuin[1] == 'a') {
3244
3245 /* Hangup all calls */
3246 pjsua_call_hangup_all();
3247
3248 } else {
3249
3250 /* Hangup current calls */
3251 pjsua_call_hangup(current_call, 0, NULL, NULL);
3252 }
3253 break;
3254
3255 case ']':
3256 case '[':
3257 /*
3258 * Cycle next/prev dialog.
3259 */
3260 if (menuin[0] == ']') {
3261 find_next_call();
3262
3263 } else {
3264 find_prev_call();
3265 }
3266
3267 if (current_call != -1) {
3268
3269 pjsua_call_get_info(current_call, &call_info);
3270 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3271 (int)call_info.remote_info.slen,
3272 call_info.remote_info.ptr));
3273
3274 } else {
3275 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3276 }
3277 break;
3278
3279
3280 case '>':
3281 case '<':
3282 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3283 break;
3284
3285 i = my_atoi(buf);
3286 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003287 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003288 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3289 } else {
3290 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3291 }
3292 break;
3293
3294
3295 case '+':
3296 if (menuin[1] == 'b') {
3297
3298 pjsua_buddy_config buddy_cfg;
3299 pjsua_buddy_id buddy_id;
3300 pj_status_t status;
3301
3302 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3303 break;
3304
3305 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3306 printf("Invalid SIP URI '%s'\n", buf);
3307 break;
3308 }
3309
Benny Prijonoac623b32006-07-03 15:19:31 +00003310 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003311
3312 buddy_cfg.uri = pj_str(buf);
3313 buddy_cfg.subscribe = PJ_TRUE;
3314
3315 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3316 if (status == PJ_SUCCESS) {
3317 printf("New buddy '%s' added at index %d\n",
3318 buf, buddy_id+1);
3319 }
3320
3321 } else if (menuin[1] == 'a') {
3322
Benny Prijonofb2b3652007-06-28 07:15:03 +00003323 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3324 pjsua_acc_config acc_cfg;
3325 pj_status_t status;
3326
3327 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3328 break;
3329 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3330 break;
3331 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3332 break;
3333 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3334 break;
3335 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3336 break;
3337
3338 pjsua_acc_config_default(&acc_cfg);
3339 acc_cfg.id = pj_str(id);
3340 acc_cfg.reg_uri = pj_str(registrar);
3341 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003342 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003343 acc_cfg.cred_info[0].realm = pj_str(realm);
3344 acc_cfg.cred_info[0].username = pj_str(uname);
3345 acc_cfg.cred_info[0].data_type = 0;
3346 acc_cfg.cred_info[0].data = pj_str(passwd);
3347
3348 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3349 if (status != PJ_SUCCESS) {
3350 pjsua_perror(THIS_FILE, "Error adding new account", status);
3351 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003352
3353 } else {
3354 printf("Invalid input %s\n", menuin);
3355 }
3356 break;
3357
3358 case '-':
3359 if (menuin[1] == 'b') {
3360 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3361 break;
3362
3363 i = my_atoi(buf) - 1;
3364
3365 if (!pjsua_buddy_is_valid(i)) {
3366 printf("Invalid buddy id %d\n", i);
3367 } else {
3368 pjsua_buddy_del(i);
3369 printf("Buddy %d deleted\n", i);
3370 }
3371
3372 } else if (menuin[1] == 'a') {
3373
3374 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3375 break;
3376
3377 i = my_atoi(buf);
3378
3379 if (!pjsua_acc_is_valid(i)) {
3380 printf("Invalid account id %d\n", i);
3381 } else {
3382 pjsua_acc_del(i);
3383 printf("Account %d deleted\n", i);
3384 }
3385
3386 } else {
3387 printf("Invalid input %s\n", menuin);
3388 }
3389 break;
3390
3391 case 'H':
3392 /*
3393 * Hold call.
3394 */
3395 if (current_call != -1) {
3396
3397 pjsua_call_set_hold(current_call, NULL);
3398
3399 } else {
3400 PJ_LOG(3,(THIS_FILE, "No current call"));
3401 }
3402 break;
3403
3404 case 'v':
3405 /*
3406 * Send re-INVITE (to release hold, etc).
3407 */
3408 if (current_call != -1) {
3409
3410 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3411
3412 } else {
3413 PJ_LOG(3,(THIS_FILE, "No current call"));
3414 }
3415 break;
3416
Benny Prijonoc08682e2007-10-04 06:17:58 +00003417 case 'U':
3418 /*
3419 * Send UPDATE
3420 */
3421 if (current_call != -1) {
3422
3423 pjsua_call_update(current_call, 0, NULL);
3424
3425 } else {
3426 PJ_LOG(3,(THIS_FILE, "No current call"));
3427 }
3428 break;
3429
3430 case 'C':
3431 if (menuin[1] == 'p') {
3432 manage_codec_prio();
3433 }
3434 break;
3435
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003436 case 'x':
3437 /*
3438 * Transfer call.
3439 */
3440 if (current_call == -1) {
3441
3442 PJ_LOG(3,(THIS_FILE, "No current call"));
3443
3444 } else {
3445 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003446 pjsua_msg_data msg_data;
3447 pjsip_generic_string_hdr refer_sub;
3448 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3449 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003450 pjsua_call_info ci;
3451
3452 pjsua_call_get_info(current_call, &ci);
3453 printf("Transfering current call [%d] %.*s\n",
3454 current_call,
3455 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003456
3457 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3458
3459 /* Check if call is still there. */
3460
3461 if (call != current_call) {
3462 puts("Call has been disconnected");
3463 continue;
3464 }
3465
Benny Prijonod524e822006-09-22 12:48:18 +00003466 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003467 if (app_config.no_refersub) {
3468 /* Add Refer-Sub: false in outgoing REFER request */
3469 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3470 &STR_FALSE);
3471 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3472 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003473 if (result.nb_result != NO_NB) {
3474 if (result.nb_result == -1 || result.nb_result == 0)
3475 puts("You can't do that with transfer call!");
3476 else {
3477 pjsua_buddy_info binfo;
3478 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003479 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003480 }
3481
3482 } else if (result.uri_result) {
3483 pj_str_t tmp;
3484 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003485 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003486 }
3487 }
3488 break;
3489
Benny Prijonof7b1c392006-11-11 16:46:34 +00003490 case 'X':
3491 /*
3492 * Transfer call with replaces.
3493 */
3494 if (current_call == -1) {
3495
3496 PJ_LOG(3,(THIS_FILE, "No current call"));
3497
3498 } else {
3499 int call = current_call;
3500 int dst_call;
3501 pjsua_msg_data msg_data;
3502 pjsip_generic_string_hdr refer_sub;
3503 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3504 pj_str_t STR_FALSE = { "false", 5 };
3505 pjsua_call_id ids[PJSUA_MAX_CALLS];
3506 pjsua_call_info ci;
3507 unsigned i, count;
3508
3509 count = PJ_ARRAY_SIZE(ids);
3510 pjsua_enum_calls(ids, &count);
3511
3512 if (count <= 1) {
3513 puts("There are no other calls");
3514 continue;
3515 }
3516
3517 pjsua_call_get_info(current_call, &ci);
3518 printf("Transfer call [%d] %.*s to one of the following:\n",
3519 current_call,
3520 (int)ci.remote_info.slen, ci.remote_info.ptr);
3521
3522 for (i=0; i<count; ++i) {
3523 pjsua_call_info call_info;
3524
3525 if (ids[i] == call)
3526 continue;
3527
3528 pjsua_call_get_info(ids[i], &call_info);
3529 printf("%d %.*s [%.*s]\n",
3530 ids[i],
3531 (int)call_info.remote_info.slen,
3532 call_info.remote_info.ptr,
3533 (int)call_info.state_text.slen,
3534 call_info.state_text.ptr);
3535 }
3536
3537 if (!simple_input("Enter call number to be replaced",
3538 buf, sizeof(buf)))
3539 continue;
3540
3541 dst_call = my_atoi(buf);
3542
3543 /* Check if call is still there. */
3544
3545 if (call != current_call) {
3546 puts("Call has been disconnected");
3547 continue;
3548 }
3549
3550 /* Check that destination call is valid. */
3551 if (dst_call == call) {
3552 puts("Destination call number must not be the same "
3553 "as the call being transfered");
3554 continue;
3555 }
3556 if (dst_call >= PJSUA_MAX_CALLS) {
3557 puts("Invalid destination call number");
3558 continue;
3559 }
3560 if (!pjsua_call_is_active(dst_call)) {
3561 puts("Invalid destination call number");
3562 continue;
3563 }
3564
3565 pjsua_msg_data_init(&msg_data);
3566 if (app_config.no_refersub) {
3567 /* Add Refer-Sub: false in outgoing REFER request */
3568 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3569 &STR_FALSE);
3570 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3571 }
3572
3573 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3574 }
3575 break;
3576
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003577 case '#':
3578 /*
3579 * Send DTMF strings.
3580 */
3581 if (current_call == -1) {
3582
3583 PJ_LOG(3,(THIS_FILE, "No current call"));
3584
3585 } else if (!pjsua_call_has_media(current_call)) {
3586
3587 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3588
3589 } else {
3590 pj_str_t digits;
3591 int call = current_call;
3592 pj_status_t status;
3593
3594 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3595 sizeof(buf)))
3596 {
3597 break;
3598 }
3599
3600 if (call != current_call) {
3601 puts("Call has been disconnected");
3602 continue;
3603 }
3604
3605 digits = pj_str(buf);
3606 status = pjsua_call_dial_dtmf(current_call, &digits);
3607 if (status != PJ_SUCCESS) {
3608 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3609 } else {
3610 puts("DTMF digits enqueued for transmission");
3611 }
3612 }
3613 break;
3614
Benny Prijonofeb69f42007-10-05 09:12:26 +00003615 case '*':
3616 /* Send DTMF with INFO */
3617 if (current_call == -1) {
3618
3619 PJ_LOG(3,(THIS_FILE, "No current call"));
3620
3621 } else {
3622 const pj_str_t SIP_INFO = pj_str("INFO");
3623 pj_str_t digits;
3624 int call = current_call;
3625 int i;
3626 pj_status_t status;
3627
3628 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3629 sizeof(buf)))
3630 {
3631 break;
3632 }
3633
3634 if (call != current_call) {
3635 puts("Call has been disconnected");
3636 continue;
3637 }
3638
3639 digits = pj_str(buf);
3640 for (i=0; i<digits.slen; ++i) {
3641 pjsua_msg_data msg_data;
3642 char body[80];
3643
3644 pjsua_msg_data_init(&msg_data);
3645 msg_data.content_type = pj_str("application/dtmf-relay");
3646
3647 pj_ansi_snprintf(body, sizeof(body),
3648 "Signal=%c\r\n"
3649 "Duration=160",
3650 buf[i]);
3651 msg_data.msg_body = pj_str(body);
3652
3653 status = pjsua_call_send_request(current_call, &SIP_INFO,
3654 &msg_data);
3655 if (status != PJ_SUCCESS) {
3656 break;
3657 }
3658 }
3659 }
3660 break;
3661
Benny Prijono56315612006-07-18 14:39:40 +00003662 case 'S':
3663 /*
3664 * Send arbitrary request
3665 */
3666 if (pjsua_acc_get_count() == 0) {
3667 puts("Sorry, need at least one account configured");
3668 break;
3669 }
3670
3671 puts("Send arbitrary request to remote host");
3672
3673 /* Input METHOD */
3674 if (!simple_input("Request method:",text,sizeof(text)))
3675 break;
3676
3677 /* Input destination URI */
3678 uri = NULL;
3679 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3680 if (result.nb_result != NO_NB) {
3681
3682 if (result.nb_result == -1 || result.nb_result == 0) {
3683 puts("Sorry you can't do that!");
3684 continue;
3685 } else {
3686 pjsua_buddy_info binfo;
3687 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3688 uri = binfo.uri.ptr;
3689 }
3690
3691 } else if (result.uri_result) {
3692 uri = result.uri_result;
3693 }
3694
3695 tmp = pj_str(uri);
3696
3697 send_request(text, &tmp);
3698 break;
3699
Benny Prijonoebc32c32008-06-12 13:30:39 +00003700 case 'e':
3701 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3702 pj_str_t tmp;
3703
3704 tmp.ptr = menuin+5;
3705 tmp.slen = pj_ansi_strlen(menuin)-6;
3706
3707 if (tmp.slen < 1) {
3708 puts("Usage: echo [0|1]");
3709 break;
3710 }
3711
Benny Prijonoddd02de2008-06-26 22:20:11 +00003712 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003713 }
3714 break;
3715
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003716 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003717 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3718 pj_str_t tmp;
3719 int delay;
3720
3721 tmp.ptr = menuin+6;
3722 tmp.slen = pj_ansi_strlen(menuin)-7;
3723
3724 if (tmp.slen < 1) {
3725 puts("Usage: sleep MSEC");
3726 break;
3727 }
3728
3729 delay = pj_strtoul(&tmp);
3730 if (delay < 0) delay = 0;
3731 pj_thread_sleep(delay);
3732 break;
3733 }
3734 /* Continue below */
3735
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003736 case 'u':
3737 /*
3738 * Subscribe/unsubscribe presence.
3739 */
3740 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3741 if (result.nb_result != NO_NB) {
3742 if (result.nb_result == -1) {
3743 int i, count;
3744 count = pjsua_get_buddy_count();
3745 for (i=0; i<count; ++i)
3746 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3747 } else if (result.nb_result == 0) {
3748 puts("Sorry, can only subscribe to buddy's presence, "
3749 "not from existing call");
3750 } else {
3751 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3752 }
3753
3754 } else if (result.uri_result) {
3755 puts("Sorry, can only subscribe to buddy's presence, "
3756 "not arbitrary URL (for now)");
3757 }
3758
3759 break;
3760
3761 case 'r':
3762 switch (menuin[1]) {
3763 case 'r':
3764 /*
3765 * Re-Register.
3766 */
3767 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3768 break;
3769 case 'u':
3770 /*
3771 * Unregister
3772 */
3773 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3774 break;
3775 }
3776 break;
3777
3778 case 't':
3779 pjsua_acc_get_info(current_acc, &acc_info);
3780 acc_info.online_status = !acc_info.online_status;
3781 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3782 printf("Setting %s online status to %s\n",
3783 acc_info.acc_uri.ptr,
3784 (acc_info.online_status?"online":"offline"));
3785 break;
3786
Benny Prijono4461c7d2007-08-25 13:36:15 +00003787 case 'T':
3788 change_online_status();
3789 break;
3790
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003791 case 'c':
3792 switch (menuin[1]) {
3793 case 'l':
3794 conf_list();
3795 break;
3796 case 'c':
3797 case 'd':
3798 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003799 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003800 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003801 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003802 const char *src_title, *dst_title;
3803
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003804 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003805
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003806 if (cnt != 3) {
3807 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003808
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003809 src_title = (menuin[1]=='c'?
3810 "Connect src port #":
3811 "Disconnect src port #");
3812 dst_title = (menuin[1]=='c'?
3813 "To dst port #":
3814 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003815
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003816 if (!simple_input(src_title, src_port, sizeof(src_port)))
3817 break;
3818
3819 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3820 break;
3821 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003822
3823 if (menuin[1]=='c') {
3824 status = pjsua_conf_connect(my_atoi(src_port),
3825 my_atoi(dst_port));
3826 } else {
3827 status = pjsua_conf_disconnect(my_atoi(src_port),
3828 my_atoi(dst_port));
3829 }
3830 if (status == PJ_SUCCESS) {
3831 puts("Success");
3832 } else {
3833 puts("ERROR!!");
3834 }
3835 }
3836 break;
3837 }
3838 break;
3839
Benny Prijono6dd967c2006-12-26 02:27:14 +00003840 case 'V':
3841 /* Adjust audio volume */
3842 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3843 if (simple_input(buf,text,sizeof(text))) {
3844 char *err;
3845 app_config.mic_level = (float)strtod(text, &err);
3846 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3847 }
3848 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3849 app_config.speaker_level);
3850 if (simple_input(buf,text,sizeof(text))) {
3851 char *err;
3852 app_config.speaker_level = (float)strtod(text, &err);
3853 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3854 }
3855
3856 break;
3857
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003858 case 'd':
3859 if (menuin[1] == 'c') {
3860 char settings[2000];
3861 int len;
3862
3863 len = write_settings(&app_config, settings, sizeof(settings));
3864 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003865 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003866 else
3867 PJ_LOG(3,(THIS_FILE,
3868 "Dumping configuration (%d bytes):\n%s\n",
3869 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003870
3871 } else if (menuin[1] == 'q') {
3872
3873 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003874 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003875 } else {
3876 PJ_LOG(3,(THIS_FILE, "No current call"));
3877 }
3878
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003879 } else {
3880 app_dump(menuin[1]=='d');
3881 }
3882 break;
3883
3884
3885 case 'f':
3886 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3887 char settings[2000];
3888 int len;
3889
3890 len = write_settings(&app_config, settings, sizeof(settings));
3891 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003892 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003893 else {
3894 pj_oshandle_t fd;
3895 pj_status_t status;
3896
3897 status = pj_file_open(app_config.pool, buf,
3898 PJ_O_WRONLY, &fd);
3899 if (status != PJ_SUCCESS) {
3900 pjsua_perror(THIS_FILE, "Unable to open file", status);
3901 } else {
3902 pj_ssize_t size = len;
3903 pj_file_write(fd, settings, &size);
3904 pj_file_close(fd);
3905
3906 printf("Settings successfully written to '%s'\n", buf);
3907 }
3908 }
3909
3910 }
3911 break;
3912
3913
3914 case 'q':
3915 goto on_exit;
3916
3917
3918 default:
3919 if (menuin[0] != '\n' && menuin[0] != '\r') {
3920 printf("Invalid input %s", menuin);
3921 }
3922 keystroke_help();
3923 break;
3924 }
3925 }
3926
3927on_exit:
3928 ;
3929}
3930
3931
3932/*****************************************************************************
3933 * Public API
3934 */
3935
3936pj_status_t app_init(int argc, char *argv[])
3937{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003938 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003939 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003940 unsigned i;
3941 pj_status_t status;
3942
3943 /* Create pjsua */
3944 status = pjsua_create();
3945 if (status != PJ_SUCCESS)
3946 return status;
3947
3948 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00003949 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003950
3951 /* Initialize default config */
3952 default_config(&app_config);
3953
3954 /* Parse the arguments */
3955 status = parse_args(argc, argv, &app_config, &uri_arg);
3956 if (status != PJ_SUCCESS)
3957 return status;
3958
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003959 /* Initialize application callbacks */
3960 app_config.cfg.cb.on_call_state = &on_call_state;
3961 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3962 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003963 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003964 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003965 app_config.cfg.cb.on_reg_state = &on_reg_state;
Benny Prijono563cd7a2008-08-19 20:02:15 +00003966 app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003967 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3968 app_config.cfg.cb.on_pager = &on_pager;
3969 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003970 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003971 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003972 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003973
3974 /* Initialize pjsua */
3975 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3976 &app_config.media_cfg);
3977 if (status != PJ_SUCCESS)
3978 return status;
3979
Benny Prijonoe909eac2006-07-27 22:04:56 +00003980#ifdef STEREO_DEMO
3981 stereo_demo();
3982#endif
3983
Benny Prijono804ff0a2006-09-14 11:17:48 +00003984 /* Initialize calls data */
3985 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3986 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3987 app_config.call_data[i].timer.cb = &call_timeout_callback;
3988 }
3989
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003990 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003991 for (i=0; i<app_config.wav_count; ++i) {
3992 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003993 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00003994
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003995 if (app_config.auto_play_hangup)
3996 play_options |= PJMEDIA_FILE_NO_LOOP;
3997
3998 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00003999 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004000 if (status != PJ_SUCCESS)
4001 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00004002
Benny Prijono72c04d22007-02-17 22:20:58 +00004003 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00004004 app_config.wav_id = wav_id;
4005 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004006 if (app_config.auto_play_hangup) {
4007 pjmedia_port *port;
4008
4009 pjsua_player_get_port(app_config.wav_id, &port);
4010 status = pjmedia_wav_player_set_eof_cb(port, NULL,
4011 &on_playfile_done);
4012 if (status != PJ_SUCCESS)
4013 goto on_error;
4014
4015 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
4016 &hangup_timeout_callback);
4017 }
Benny Prijono32e4f492007-01-24 00:44:26 +00004018 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004019 }
4020
Benny Prijono4af234b2007-01-24 02:02:09 +00004021 /* Optionally registers tone players */
4022 for (i=0; i<app_config.tone_count; ++i) {
4023 pjmedia_port *tport;
4024 char name[80];
4025 pj_str_t label;
4026 pj_status_t status;
4027
4028 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
4029 app_config.tones[i].freq1,
4030 app_config.tones[i].freq2);
4031 label = pj_str(name);
4032 status = pjmedia_tonegen_create2(app_config.pool, &label,
4033 8000, 1, 160, 16,
4034 PJMEDIA_TONEGEN_LOOP, &tport);
4035 if (status != PJ_SUCCESS) {
4036 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
4037 goto on_error;
4038 }
4039
4040 status = pjsua_conf_add_port(app_config.pool, tport,
4041 &app_config.tone_slots[i]);
4042 pj_assert(status == PJ_SUCCESS);
4043
4044 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
4045 pj_assert(status == PJ_SUCCESS);
4046 }
4047
Benny Prijono1ebd6142006-10-19 15:48:02 +00004048 /* Optionally create recorder file, if any. */
4049 if (app_config.rec_file.slen) {
4050 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
4051 &app_config.rec_id);
4052 if (status != PJ_SUCCESS)
4053 goto on_error;
4054
4055 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
4056 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004057
Benny Prijonoe347cb02007-02-14 14:36:13 +00004058 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
4059
Benny Prijono91d20f42008-06-14 19:42:37 +00004060 /* Create ringback tones */
4061 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00004062 unsigned i, samples_per_frame;
4063 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00004064 pj_str_t name;
4065
Benny Prijono4f966892008-06-14 22:43:56 +00004066 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
4067 app_config.media_cfg.clock_rate *
4068 app_config.media_cfg.channel_count / 1000;
4069
Benny Prijono91d20f42008-06-14 19:42:37 +00004070 /* Ringback tone (call is ringing) */
4071 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00004072 status = pjmedia_tonegen_create2(app_config.pool, &name,
4073 app_config.media_cfg.clock_rate,
4074 app_config.media_cfg.channel_count,
4075 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004076 16, PJMEDIA_TONEGEN_LOOP,
4077 &app_config.ringback_port);
4078 if (status != PJ_SUCCESS)
4079 goto on_error;
4080
Benny Prijonoe28cec82008-06-14 20:40:53 +00004081 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004082 for (i=0; i<RINGBACK_CNT; ++i) {
4083 tone[i].freq1 = RINGBACK_FREQ1;
4084 tone[i].freq2 = RINGBACK_FREQ2;
4085 tone[i].on_msec = RINGBACK_ON;
4086 tone[i].off_msec = RINGBACK_OFF;
4087 }
4088 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004089
Benny Prijono4f966892008-06-14 22:43:56 +00004090 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004091 PJMEDIA_TONEGEN_LOOP);
4092
4093
Benny Prijono91d20f42008-06-14 19:42:37 +00004094 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4095 &app_config.ringback_slot);
4096 if (status != PJ_SUCCESS)
4097 goto on_error;
4098
4099 /* Ring (to alert incoming call) */
4100 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004101 status = pjmedia_tonegen_create2(app_config.pool, &name,
4102 app_config.media_cfg.clock_rate,
4103 app_config.media_cfg.channel_count,
4104 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004105 16, PJMEDIA_TONEGEN_LOOP,
4106 &app_config.ring_port);
4107 if (status != PJ_SUCCESS)
4108 goto on_error;
4109
Benny Prijono4f966892008-06-14 22:43:56 +00004110 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004111 tone[i].freq1 = RING_FREQ1;
4112 tone[i].freq2 = RING_FREQ2;
4113 tone[i].on_msec = RING_ON;
4114 tone[i].off_msec = RING_OFF;
4115 }
Benny Prijono4f966892008-06-14 22:43:56 +00004116 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004117
Benny Prijono4f966892008-06-14 22:43:56 +00004118 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004119 tone, PJMEDIA_TONEGEN_LOOP);
4120
Benny Prijono91d20f42008-06-14 19:42:37 +00004121 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4122 &app_config.ring_slot);
4123 if (status != PJ_SUCCESS)
4124 goto on_error;
4125
4126 }
4127
Benny Prijono87ef89a2007-01-14 00:39:45 +00004128 /* Add UDP transport unless it's disabled. */
4129 if (!app_config.no_udp) {
4130 pjsua_acc_id aid;
4131
4132 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4133 &app_config.udp_cfg,
4134 &transport_id);
4135 if (status != PJ_SUCCESS)
4136 goto on_error;
4137
4138 /* Add local account */
4139 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4140 //pjsua_acc_set_transport(aid, transport_id);
4141 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004142
4143 if (app_config.udp_cfg.port == 0) {
4144 pjsua_transport_info ti;
4145 pj_sockaddr_in *a;
4146
4147 pjsua_transport_get_info(transport_id, &ti);
4148 a = (pj_sockaddr_in*)&ti.local_addr;
4149
4150 tcp_cfg.port = pj_ntohs(a->sin_port);
4151 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004152 }
4153
Benny Prijonoe93e2872006-06-28 16:46:49 +00004154 /* Add TCP transport unless it's disabled */
4155 if (!app_config.no_tcp) {
4156 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004157 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004158 &transport_id);
4159 if (status != PJ_SUCCESS)
4160 goto on_error;
4161
4162 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004163 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004164 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4165
4166 }
4167
Benny Prijonoe93e2872006-06-28 16:46:49 +00004168
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004169#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4170 /* Add TLS transport when application wants one */
4171 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004172
4173 pjsua_acc_id acc_id;
4174
4175 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004176 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004177 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004178 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004179 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004180 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004181 if (status != PJ_SUCCESS)
4182 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004183
4184 /* Add local account */
4185 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4186 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004187 }
4188#endif
4189
Benny Prijonoe93e2872006-06-28 16:46:49 +00004190 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004191 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004192 status = -1;
4193 goto on_error;
4194 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004195
4196
4197 /* Add accounts */
4198 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004199 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004200 if (status != PJ_SUCCESS)
4201 goto on_error;
4202 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4203 }
4204
4205 /* Add buddies */
4206 for (i=0; i<app_config.buddy_cnt; ++i) {
4207 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4208 if (status != PJ_SUCCESS)
4209 goto on_error;
4210 }
4211
4212 /* Optionally set codec orders */
4213 for (i=0; i<app_config.codec_cnt; ++i) {
4214 pjsua_codec_set_priority(&app_config.codec_arg[i],
4215 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4216 }
4217
Benny Prijonofce28542007-12-09 15:41:10 +00004218 /* Optionally disable some codec */
4219 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4220 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4221 }
4222
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004223 /* Add RTP transports */
Benny Prijono40860c32008-09-04 13:55:33 +00004224#ifdef TRANSPORT_ADAPTER_SAMPLE
4225 status = transport_adapter_sample();
4226
4227#else
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004228 status = pjsua_media_transports_create(&app_config.rtp_cfg);
Benny Prijono40860c32008-09-04 13:55:33 +00004229#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004230 if (status != PJ_SUCCESS)
4231 goto on_error;
4232
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004233 /* Set sound device latency */
4234 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4235
Benny Prijono22a300a2006-06-14 20:04:55 +00004236 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004237#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004238 if (app_config.null_audio) {
4239 status = pjsua_set_null_snd_dev();
4240 if (status != PJ_SUCCESS)
4241 return status;
4242 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004243#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004244
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004245 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4246 app_config.playback_dev != PJSUA_INVALID_ID)
4247 {
4248 status = pjsua_set_snd_dev(app_config.capture_dev,
4249 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004250 if (status != PJ_SUCCESS)
4251 goto on_error;
4252 }
4253
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004254 return PJ_SUCCESS;
4255
4256on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004257 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004258 return status;
4259}
4260
4261
Benny Prijonoebc32c32008-06-12 13:30:39 +00004262static int stdout_refresh_proc(void *arg)
4263{
4264 PJ_UNUSED_ARG(arg);
4265
4266 /* Set thread to lowest priority so that it doesn't clobber
4267 * stdout output
4268 */
4269 pj_thread_set_prio(pj_thread_this(),
4270 pj_thread_get_prio_min(pj_thread_this()));
4271
4272 while (!stdout_refresh_quit) {
4273 pj_thread_sleep(stdout_refresh * 1000);
4274 puts(stdout_refresh_text);
4275 fflush(stdout);
4276 }
4277
4278 return 0;
4279}
4280
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004281pj_status_t app_main(void)
4282{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004283 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004284 pj_status_t status;
4285
4286 /* Start pjsua */
4287 status = pjsua_start();
4288 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004289 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004290 return status;
4291 }
4292
Benny Prijonoebc32c32008-06-12 13:30:39 +00004293 /* Start console refresh thread */
4294 if (stdout_refresh > 0) {
4295 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4296 NULL, 0, 0, &stdout_refresh_thread);
4297 }
4298
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004299 console_app_main(&uri_arg);
4300
Benny Prijonoebc32c32008-06-12 13:30:39 +00004301 if (stdout_refresh_thread) {
4302 stdout_refresh_quit = PJ_TRUE;
4303 pj_thread_join(stdout_refresh_thread);
4304 pj_thread_destroy(stdout_refresh_thread);
4305 }
4306
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004307 return PJ_SUCCESS;
4308}
4309
4310pj_status_t app_destroy(void)
4311{
Benny Prijonof762ee72006-12-01 11:14:37 +00004312 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004313 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004314
Benny Prijonoe909eac2006-07-27 22:04:56 +00004315#ifdef STEREO_DEMO
4316 if (app_config.snd) {
4317 pjmedia_snd_port_destroy(app_config.snd);
4318 app_config.snd = NULL;
4319 }
4320#endif
4321
Benny Prijono91d20f42008-06-14 19:42:37 +00004322 /* Close ringback port */
4323 if (app_config.ringback_port &&
4324 app_config.ringback_slot != PJSUA_INVALID_ID)
4325 {
4326 pjsua_conf_remove_port(app_config.ringback_slot);
4327 app_config.ringback_slot = PJSUA_INVALID_ID;
4328 pjmedia_port_destroy(app_config.ringback_port);
4329 app_config.ringback_port = NULL;
4330 }
4331
4332 /* Close ring port */
4333 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4334 pjsua_conf_remove_port(app_config.ring_slot);
4335 app_config.ring_slot = PJSUA_INVALID_ID;
4336 pjmedia_port_destroy(app_config.ring_port);
4337 app_config.ring_port = NULL;
4338 }
4339
Benny Prijono4af234b2007-01-24 02:02:09 +00004340 /* Close tone generators */
4341 for (i=0; i<app_config.tone_count; ++i) {
4342 pjsua_conf_remove_port(app_config.tone_slots[i]);
4343 }
4344
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004345 if (app_config.pool) {
4346 pj_pool_release(app_config.pool);
4347 app_config.pool = NULL;
4348 }
4349
Benny Prijonof762ee72006-12-01 11:14:37 +00004350 status = pjsua_destroy();
4351
4352 pj_bzero(&app_config, sizeof(app_config));
4353
4354 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004355}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004356
4357
4358#ifdef STEREO_DEMO
4359static void stereo_demo()
4360{
4361 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004362 pj_status_t status;
4363
4364 /* Disable existing sound device */
4365 conf = pjsua_set_no_snd_dev();
4366
Benny Prijonoe909eac2006-07-27 22:04:56 +00004367 /* Create stereo-mono splitter/combiner */
4368 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004369 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004370 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004371 2 * conf->info.samples_per_frame,
4372 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004373 0 /* options */,
4374 &splitter);
4375 pj_assert(status == PJ_SUCCESS);
4376
4377 /* Connect channel0 (left channel?) to conference port slot0 */
4378 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4379 0 /*options*/,
4380 conf);
4381 pj_assert(status == PJ_SUCCESS);
4382
4383 /* Create reverse channel for channel1 (right channel?)... */
4384 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4385 splitter,
4386 1 /* ch1 */,
4387 0 /* options */,
4388 &ch1);
4389 pj_assert(status == PJ_SUCCESS);
4390
4391 /* .. and register it to conference bridge (it would be slot1
4392 * if there's no other devices connected to the bridge)
4393 */
4394 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4395 pj_assert(status == PJ_SUCCESS);
4396
4397 /* Create sound device */
4398 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004399 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004400 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004401 2 * conf->info.samples_per_frame,
4402 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004403 0, &app_config.snd);
4404 pj_assert(status == PJ_SUCCESS);
4405
4406
4407 /* Connect the splitter to the sound device */
4408 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4409 pj_assert(status == PJ_SUCCESS);
4410
4411}
4412#endif
4413
Benny Prijono40860c32008-09-04 13:55:33 +00004414#ifdef TRANSPORT_ADAPTER_SAMPLE
4415static pj_status_t create_transport_adapter(pjmedia_endpt *med_endpt, int port,
4416 pjmedia_transport **p_tp)
4417{
4418 pjmedia_transport *udp;
4419 pj_status_t status;
4420
4421 /* Create the UDP media transport */
4422 status = pjmedia_transport_udp_create(med_endpt, NULL, port, 0, &udp);
4423 if (status != PJ_SUCCESS)
4424 return status;
4425
4426 /* Create the adapter */
4427 status = pjmedia_tp_adapter_create(med_endpt, NULL, udp, p_tp);
4428 if (status != PJ_SUCCESS) {
4429 pjmedia_transport_close(udp);
4430 return status;
4431 }
4432
4433 return PJ_SUCCESS;
4434}
4435
4436static pj_status_t transport_adapter_sample(void)
4437{
4438 pjsua_media_transport tp[PJSUA_MAX_CALLS];
4439 pj_status_t status;
4440 int port = 7000;
4441 unsigned i;
4442
4443 for (i=0; i<app_config.cfg.max_calls; ++i) {
4444 status = create_transport_adapter(pjsua_get_pjmedia_endpt(),
4445 port + i*10,
4446 &tp[i].transport);
4447 if (status != PJ_SUCCESS)
4448 return status;
4449 }
4450
4451 return pjsua_media_transports_attach(tp, i, PJ_TRUE);
4452}
4453#endif
4454