blob: 53b96415b89a88845dba31ac0cac86c054925310 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 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
22#define THIS_FILE "pjsua.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 Prijonoeebe9af2006-06-13 22:57:13 +000026
Benny Prijono804ff0a2006-09-14 11:17:48 +000027/* Call specific data */
28struct call_data
29{
30 pj_timer_entry timer;
31};
32
Benny Prijonoeebe9af2006-06-13 22:57:13 +000033
34/* Pjsua application data */
35static struct app_config
36{
37 pjsua_config cfg;
38 pjsua_logging_config log_cfg;
39 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000040 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000041 pj_bool_t no_tcp;
42 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000043 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000044 pjsua_transport_config udp_cfg;
45 pjsua_transport_config rtp_cfg;
46
47 unsigned acc_cnt;
48 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
49
50 unsigned buddy_cnt;
51 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
52
Benny Prijono804ff0a2006-09-14 11:17:48 +000053 struct call_data call_data[PJSUA_MAX_CALLS];
54
Benny Prijonoeebe9af2006-06-13 22:57:13 +000055 pj_pool_t *pool;
56 /* Compatibility with older pjsua */
57
58 unsigned codec_cnt;
59 pj_str_t codec_arg[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000060 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000061 unsigned wav_count;
62 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000063 unsigned tone_count;
64 pjmedia_tone_desc tones[32];
65 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000066 pjsua_player_id wav_id;
67 pjsua_conf_port_id wav_port;
68 pj_bool_t auto_play;
69 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000070 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000071 pj_str_t rec_file;
72 pj_bool_t auto_rec;
73 pjsua_recorder_id rec_id;
74 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000075 unsigned ptime;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000076 unsigned auto_answer;
77 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +000078
79#ifdef STEREO_DEMO
80 pjmedia_snd_port *snd;
81#endif
82
Benny Prijono6dd967c2006-12-26 02:27:14 +000083 float mic_level,
84 speaker_level;
85
Benny Prijono4e5d5512007-03-06 18:11:30 +000086 int capture_dev, playback_dev;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000087} app_config;
88
89
Benny Prijono21b9ad92006-08-15 13:11:22 +000090//static pjsua_acc_id current_acc;
91#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +000092static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000093static pj_str_t uri_arg;
94
Benny Prijono594e4c52006-09-14 18:51:01 +000095#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +000096static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +000097#endif
Benny Prijonoe909eac2006-07-27 22:04:56 +000098
Benny Prijonoeebe9af2006-06-13 22:57:13 +000099/*****************************************************************************
100 * Configuration manipulation
101 */
102
103/* Show usage */
104static void usage(void)
105{
106 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000107 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000108 puts ("");
109 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000110 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111 puts (" --help Display this help screen");
112 puts (" --version Display version info");
113 puts ("");
114 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000115 puts (" --log-file=fname Log to filename (default stderr)");
116 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
117 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
118 puts ("");
119 puts ("SIP Account options:");
120 puts (" --registrar=url Set the URL of registrar server");
121 puts (" --id=url Set the URL of local ID (used in From header)");
122 puts (" --contact=url Optionally override the Contact information");
123 puts (" --proxy=url Optional URL of proxy server to visit");
124 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000125 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000126 puts (" --realm=string Set realm");
127 puts (" --username=string Set authentication username");
128 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000129 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000130 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000131 puts ("");
132 puts ("SIP Account Control:");
133 puts (" --next-account Add more account");
134 puts ("");
135 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000136 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
137 puts (" TCP and UDP transports on the specified port, unless");
138 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000139 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
140 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000141 puts (" --no-tcp Disable TCP transport.");
142 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000143 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
144 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000145 puts (" --outbound=url Set the URL of global outbound proxy server");
146 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000147 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000148 puts ("");
149 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000150 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000151 puts (" --tls-ca-file Specify TLS CA file (default=none)");
152 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
153 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
154 puts (" --tls-password Specify TLS password to private key file (default=none)");
155 puts (" --tls-verify-server Verify server's certificate (default=no)");
156 puts (" --tls-verify-client Verify client's certificate (default=no)");
157 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
158
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000159 puts ("");
160 puts ("Media Options:");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000161 puts (" --use-ice Enable ICE (default:no)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000162 puts (" --add-codec=name Manually add codec (default is to enable all)");
163 puts (" --clock-rate=N Override sound device clock rate");
164 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000165 puts (" --play-file=file Register WAV file in conference bridge.");
166 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000167 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
168 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
169 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000170 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000171 puts (" --auto-play Automatically play the file (to incoming calls only)");
172 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000173 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000174 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
175 puts (" --auto-rec Automatically record conversation");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000176 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
Benny Prijono00cae612006-07-31 15:19:36 +0000177 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000179 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000180 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono00cae612006-07-31 15:19:36 +0000181 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
182 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
183 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000184 puts (" --capture-dev=id Audio capture device ID (default=-1)");
185 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000186
Benny Prijono0a12f002006-07-26 17:05:39 +0000187
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000188 puts ("");
189 puts ("Buddy List (can be more than one):");
190 puts (" --add-buddy url Add the specified URL to the buddy list.");
191 puts ("");
192 puts ("User Agent options:");
193 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
194 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000195 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000196 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000197 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000198
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000200 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
201 puts ("");
202
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000203 fflush(stdout);
204}
205
206
207/* Set default config. */
208static void default_config(struct app_config *cfg)
209{
Benny Prijono56315612006-07-18 14:39:40 +0000210 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000211 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000212
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000213 pjsua_config_default(&cfg->cfg);
Benny Prijono56315612006-07-18 14:39:40 +0000214 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", PJ_VERSION, PJ_OS_NAME);
215 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
216
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000217 pjsua_logging_config_default(&cfg->log_cfg);
218 pjsua_media_config_default(&cfg->media_cfg);
219 pjsua_transport_config_default(&cfg->udp_cfg);
220 cfg->udp_cfg.port = 5060;
221 pjsua_transport_config_default(&cfg->rtp_cfg);
222 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000223 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000224 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000225 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000226 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000227 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000228 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000229 cfg->capture_dev = PJSUA_INVALID_ID;
230 cfg->playback_dev = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000231
232 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
233 pjsua_acc_config_default(&cfg->acc_cfg[i]);
234
235 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
236 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237}
238
239
240/*
241 * Read command arguments from config file.
242 */
243static int read_config_file(pj_pool_t *pool, const char *filename,
244 int *app_argc, char ***app_argv)
245{
246 int i;
247 FILE *fhnd;
248 char line[200];
249 int argc = 0;
250 char **argv;
251 enum { MAX_ARGS = 64 };
252
253 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
254 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
255 argv[argc++] = *app_argv[0];
256
257 /* Open config file. */
258 fhnd = fopen(filename, "rt");
259 if (!fhnd) {
260 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
261 fflush(stdout);
262 return -1;
263 }
264
265 /* Scan tokens in the file. */
266 while (argc < MAX_ARGS && !feof(fhnd)) {
267 char *token, *p = line;
268
269 if (fgets(line, sizeof(line), fhnd) == NULL) break;
270
271 for (token = strtok(p, " \t\r\n"); argc < MAX_ARGS;
272 token = strtok(NULL, " \t\r\n"))
273 {
274 int token_len;
275
276 if (!token) break;
277 if (*token == '#') break;
278
279 token_len = strlen(token);
280 if (!token_len)
281 continue;
282 argv[argc] = pj_pool_alloc(pool, token_len+1);
283 pj_memcpy(argv[argc], token, token_len+1);
284 ++argc;
285 }
286 }
287
288 /* Copy arguments from command line */
289 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
290 argv[argc++] = (*app_argv)[i];
291
292 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
293 PJ_LOG(1,(THIS_FILE,
294 "Too many arguments specified in cmd line/config file"));
295 fflush(stdout);
296 fclose(fhnd);
297 return -1;
298 }
299
300 fclose(fhnd);
301
302 /* Assign the new command line back to the original command line. */
303 *app_argc = argc;
304 *app_argv = argv;
305 return 0;
306
307}
308
309static int my_atoi(const char *cs)
310{
311 pj_str_t s;
312 return pj_strtoul(pj_cstr(&s, cs));
313}
314
315
316/* Parse arguments. */
317static pj_status_t parse_args(int argc, char *argv[],
318 struct app_config *cfg,
319 pj_str_t *uri_to_call)
320{
321 int c;
322 int option_index;
323 enum { OPT_CONFIG_FILE, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
324 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000325 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
326 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000327 OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000328 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
330 OPT_AUTO_ANSWER, OPT_AUTO_HANGUP, OPT_AUTO_PLAY, OPT_AUTO_LOOP,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000331 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_USE_ICE,
Benny Prijono4af234b2007-01-24 02:02:09 +0000332 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
333 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000334 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijonod79f25c2006-08-02 19:41:37 +0000335 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000336 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000337 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000338 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000339 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
340 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
341 OPT_TLS_NEG_TIMEOUT,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000342 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000343 };
344 struct pj_getopt_option long_options[] = {
345 { "config-file",1, 0, OPT_CONFIG_FILE},
346 { "log-file", 1, 0, OPT_LOG_FILE},
347 { "log-level", 1, 0, OPT_LOG_LEVEL},
348 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
349 { "help", 0, 0, OPT_HELP},
350 { "version", 0, 0, OPT_VERSION},
351 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
352 { "null-audio", 0, 0, OPT_NULL_AUDIO},
353 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000354 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000355 { "no-tcp", 0, 0, OPT_NO_TCP},
356 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000357 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000358 { "proxy", 1, 0, OPT_PROXY},
359 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
360 { "registrar", 1, 0, OPT_REGISTRAR},
361 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000362 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363 { "id", 1, 0, OPT_ID},
364 { "contact", 1, 0, OPT_CONTACT},
365 { "realm", 1, 0, OPT_REALM},
366 { "username", 1, 0, OPT_USERNAME},
367 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000368 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000369 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000370 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000371 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
372 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
373 { "no-presence", 0, 0, OPT_NO_PRESENCE},
374 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
375 { "auto-hangup",1, 0, OPT_AUTO_HANGUP},
376 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000377 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
379 { "auto-conf", 0, 0, OPT_AUTO_CONF},
380 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000381 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000382 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000383 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000384 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000385 { "add-codec", 1, 0, OPT_ADD_CODEC},
386 { "complexity", 1, 0, OPT_COMPLEXITY},
387 { "quality", 1, 0, OPT_QUALITY},
388 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000389 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000390 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono00cae612006-07-31 15:19:36 +0000391 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
392 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
393 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000394 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000395 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000396 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000397 { "duration", 1, 0, OPT_DURATION},
398 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000399 { "use-tls", 0, 0, OPT_USE_TLS},
400 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000401 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
402 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000403 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000404 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
405 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
406 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000407 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
408 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000409 { NULL, 0, 0, 0}
410 };
411 pj_status_t status;
412 pjsua_acc_config *cur_acc;
413 char *config_file = NULL;
414 unsigned i;
415
416 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000417 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000418 while ((c=pj_getopt_long(argc, argv, "", long_options,
419 &option_index)) != -1)
420 {
421 switch (c) {
422 case OPT_CONFIG_FILE:
423 config_file = pj_optarg;
424 break;
425 }
426 if (config_file)
427 break;
428 }
429
430 if (config_file) {
431 status = read_config_file(app_config.pool, config_file, &argc, &argv);
432 if (status != 0)
433 return status;
434 }
435
436 cfg->acc_cnt = 0;
437 cur_acc = &cfg->acc_cfg[0];
438
439
440 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
441 * read from config file.
442 */
443 pj_optind = 0;
444 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 pj_str_t tmp;
446 long lval;
447
448 switch (c) {
449
Benny Prijono6f137482006-06-15 11:31:36 +0000450 case OPT_CONFIG_FILE:
451 /* Ignore as this has been processed before */
452 break;
453
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000454 case OPT_LOG_FILE:
455 cfg->log_cfg.log_filename = pj_str(pj_optarg);
456 break;
457
458 case OPT_LOG_LEVEL:
459 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
460 if (c < 0 || c > 6) {
461 PJ_LOG(1,(THIS_FILE,
462 "Error: expecting integer value 0-6 "
463 "for --log-level"));
464 return PJ_EINVAL;
465 }
466 cfg->log_cfg.level = c;
467 pj_log_set_level( c );
468 break;
469
470 case OPT_APP_LOG_LEVEL:
471 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
472 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
473 PJ_LOG(1,(THIS_FILE,
474 "Error: expecting integer value 0-6 "
475 "for --app-log-level"));
476 return PJ_EINVAL;
477 }
478 break;
479
480 case OPT_HELP:
481 usage();
482 return PJ_EINVAL;
483
484 case OPT_VERSION: /* version */
485 pj_dump_config();
486 return PJ_EINVAL;
487
488 case OPT_NULL_AUDIO:
489 cfg->null_audio = PJ_TRUE;
490 break;
491
492 case OPT_CLOCK_RATE:
493 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
494 if (lval < 8000 || lval > 48000) {
495 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
496 "8000-48000 for clock rate"));
497 return PJ_EINVAL;
498 }
499 cfg->media_cfg.clock_rate = lval;
500 break;
501
502 case OPT_LOCAL_PORT: /* local-port */
503 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000504 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 PJ_LOG(1,(THIS_FILE,
506 "Error: expecting integer value for "
507 "--local-port"));
508 return PJ_EINVAL;
509 }
510 cfg->udp_cfg.port = (pj_uint16_t)lval;
511 break;
512
Benny Prijono0a5cad82006-09-26 13:21:02 +0000513 case OPT_IP_ADDR: /* ip-addr */
514 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
515 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
516 break;
517
Benny Prijonoe93e2872006-06-28 16:46:49 +0000518 case OPT_NO_UDP: /* no-udp */
519 if (cfg->no_tcp) {
520 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
521 return PJ_EINVAL;
522 }
523
524 cfg->no_udp = PJ_TRUE;
525 break;
526
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000527 case OPT_NOREFERSUB: /* norefersub */
528 cfg->no_refersub = PJ_TRUE;
529 break;
530
Benny Prijonoe93e2872006-06-28 16:46:49 +0000531 case OPT_NO_TCP: /* no-tcp */
532 if (cfg->no_udp) {
533 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
534 return PJ_EINVAL;
535 }
536
537 cfg->no_tcp = PJ_TRUE;
538 break;
539
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000540 case OPT_PROXY: /* proxy */
541 if (pjsua_verify_sip_url(pj_optarg) != 0) {
542 PJ_LOG(1,(THIS_FILE,
543 "Error: invalid SIP URL '%s' "
544 "in proxy argument", pj_optarg));
545 return PJ_EINVAL;
546 }
547 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
548 break;
549
550 case OPT_OUTBOUND_PROXY: /* outbound proxy */
551 if (pjsua_verify_sip_url(pj_optarg) != 0) {
552 PJ_LOG(1,(THIS_FILE,
553 "Error: invalid SIP URL '%s' "
554 "in outbound proxy argument", pj_optarg));
555 return PJ_EINVAL;
556 }
557 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
558 break;
559
560 case OPT_REGISTRAR: /* registrar */
561 if (pjsua_verify_sip_url(pj_optarg) != 0) {
562 PJ_LOG(1,(THIS_FILE,
563 "Error: invalid SIP URL '%s' in "
564 "registrar argument", pj_optarg));
565 return PJ_EINVAL;
566 }
567 cur_acc->reg_uri = pj_str(pj_optarg);
568 break;
569
570 case OPT_REG_TIMEOUT: /* reg-timeout */
571 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
572 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
573 PJ_LOG(1,(THIS_FILE,
574 "Error: invalid value for --reg-timeout "
575 "(expecting 1-3600)"));
576 return PJ_EINVAL;
577 }
578 break;
579
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000580 case OPT_PUBLISH: /* publish */
581 cur_acc->publish_enabled = PJ_TRUE;
582 break;
583
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000584 case OPT_ID: /* id */
585 if (pjsua_verify_sip_url(pj_optarg) != 0) {
586 PJ_LOG(1,(THIS_FILE,
587 "Error: invalid SIP URL '%s' "
588 "in local id argument", pj_optarg));
589 return PJ_EINVAL;
590 }
591 cur_acc->id = pj_str(pj_optarg);
592 break;
593
594 case OPT_CONTACT: /* contact */
595 if (pjsua_verify_sip_url(pj_optarg) != 0) {
596 PJ_LOG(1,(THIS_FILE,
597 "Error: invalid SIP URL '%s' "
598 "in contact argument", pj_optarg));
599 return PJ_EINVAL;
600 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000601 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000602 break;
603
604 case OPT_NEXT_ACCOUNT: /* Add more account. */
605 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000606 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000607 break;
608
609 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000610 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
611 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000612 break;
613
614 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000615 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000616 break;
617
618 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000619 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
620 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
621 break;
622
623 case OPT_NEXT_CRED: /* next credential */
624 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000625 break;
626
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000627 case OPT_NAMESERVER: /* nameserver */
628 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
629 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
630 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
631 return PJ_ETOOMANY;
632 }
633 break;
634
Benny Prijonoebbf6892007-03-24 17:37:25 +0000635 case OPT_STUN_DOMAIN: /* STUN domain */
636 cfg->cfg.stun_domain = pj_str(pj_optarg);
637 break;
638
Benny Prijonoc97608e2007-03-23 16:34:20 +0000639 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000640 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000641 break;
642
643 case OPT_ADD_BUDDY: /* Add to buddy list. */
644 if (pjsua_verify_sip_url(pj_optarg) != 0) {
645 PJ_LOG(1,(THIS_FILE,
646 "Error: invalid URL '%s' in "
647 "--add-buddy option", pj_optarg));
648 return -1;
649 }
650 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
651 PJ_LOG(1,(THIS_FILE,
652 "Error: too many buddies in buddy list."));
653 return -1;
654 }
655 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
656 cfg->buddy_cnt++;
657 break;
658
659 case OPT_AUTO_PLAY:
660 cfg->auto_play = 1;
661 break;
662
Benny Prijono1ebd6142006-10-19 15:48:02 +0000663 case OPT_AUTO_REC:
664 cfg->auto_rec = 1;
665 break;
666
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000667 case OPT_AUTO_LOOP:
668 cfg->auto_loop = 1;
669 break;
670
Benny Prijono7ca96da2006-08-07 12:11:40 +0000671 case OPT_AUTO_CONF:
672 cfg->auto_conf = 1;
673 break;
674
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000675 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000676 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000677 break;
678
Benny Prijono4af234b2007-01-24 02:02:09 +0000679 case OPT_PLAY_TONE:
680 {
681 int f1, f2, on, off;
682 int n;
683
684 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
685 if (n != 4) {
686 puts("Expecting f1,f2,on,off in --play-tone");
687 return -1;
688 }
689
690 cfg->tones[cfg->tone_count].freq1 = (short)f1;
691 cfg->tones[cfg->tone_count].freq2 = (short)f2;
692 cfg->tones[cfg->tone_count].on_msec = (short)on;
693 cfg->tones[cfg->tone_count].off_msec = (short)off;
694 ++cfg->tone_count;
695 }
696 break;
697
Benny Prijono1ebd6142006-10-19 15:48:02 +0000698 case OPT_REC_FILE:
699 cfg->rec_file = pj_str(pj_optarg);
700 break;
701
Benny Prijonoc97608e2007-03-23 16:34:20 +0000702 case OPT_USE_ICE:
703 cfg->media_cfg.enable_ice = PJ_TRUE;
704 break;
705
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000706 case OPT_RTP_PORT:
707 cfg->rtp_cfg.port = my_atoi(pj_optarg);
708 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
709 PJ_LOG(1,(THIS_FILE,
710 "Error: rtp-port argument value "
711 "(expecting 1-65535"));
712 return -1;
713 }
714 break;
715
716 case OPT_ADD_CODEC:
717 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
718 break;
719
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000720 /* These options were no longer valid after new pjsua */
721 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000722 case OPT_COMPLEXITY:
723 cfg->complexity = my_atoi(pj_optarg);
724 if (cfg->complexity < 0 || cfg->complexity > 10) {
725 PJ_LOG(1,(THIS_FILE,
726 "Error: invalid --complexity (expecting 0-10"));
727 return -1;
728 }
729 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000730 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000731
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000732 case OPT_DURATION:
733 cfg->duration = my_atoi(pj_optarg);
734 break;
735
Benny Prijonof521eb02006-08-06 23:07:25 +0000736 case OPT_THREAD_CNT:
737 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
738 if (cfg->cfg.thread_cnt > 128) {
739 PJ_LOG(1,(THIS_FILE,
740 "Error: invalid --thread-cnt option"));
741 return -1;
742 }
743 break;
744
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000745 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +0000746 cfg->media_cfg.ptime = my_atoi(pj_optarg);
747 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000748 PJ_LOG(1,(THIS_FILE,
749 "Error: invalid --ptime option"));
750 return -1;
751 }
752 break;
753
Benny Prijono0a12f002006-07-26 17:05:39 +0000754 case OPT_NO_VAD:
755 cfg->media_cfg.no_vad = PJ_TRUE;
756 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000757
Benny Prijonod79f25c2006-08-02 19:41:37 +0000758 case OPT_EC_TAIL:
759 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
760 if (cfg->media_cfg.ec_tail_len > 1000) {
761 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
762 "is too big"));
763 return -1;
764 }
765 break;
766
Benny Prijono0498d902006-06-19 14:49:14 +0000767 case OPT_QUALITY:
768 cfg->media_cfg.quality = my_atoi(pj_optarg);
769 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
770 PJ_LOG(1,(THIS_FILE,
771 "Error: invalid --quality (expecting 0-10"));
772 return -1;
773 }
774 break;
775
Benny Prijono00cae612006-07-31 15:19:36 +0000776 case OPT_ILBC_MODE:
777 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
778 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
779 PJ_LOG(1,(THIS_FILE,
780 "Error: invalid --ilbc-mode (expecting 20 or 30"));
781 return -1;
782 }
783 break;
784
785 case OPT_RX_DROP_PCT:
786 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
787 if (cfg->media_cfg.rx_drop_pct > 100) {
788 PJ_LOG(1,(THIS_FILE,
789 "Error: invalid --rx-drop-pct (expecting <= 100"));
790 return -1;
791 }
792 break;
793
794 case OPT_TX_DROP_PCT:
795 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
796 if (cfg->media_cfg.tx_drop_pct > 100) {
797 PJ_LOG(1,(THIS_FILE,
798 "Error: invalid --tx-drop-pct (expecting <= 100"));
799 return -1;
800 }
801 break;
802
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000803 case OPT_AUTO_ANSWER:
804 cfg->auto_answer = my_atoi(pj_optarg);
805 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
806 PJ_LOG(1,(THIS_FILE,
807 "Error: invalid code in --auto-answer "
808 "(expecting 100-699"));
809 return -1;
810 }
811 break;
812
813 case OPT_MAX_CALLS:
814 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +0000815 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +0000816 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
817 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +0000818 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000819 return -1;
820 }
821 break;
822
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000823 case OPT_USE_TLS:
824 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +0000825#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
826 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
827 return -1;
828#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000829 break;
830
831 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +0000832 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
833#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
834 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
835 return -1;
836#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000837 break;
838
Benny Prijonof3bbc132006-12-25 06:43:59 +0000839 case OPT_TLS_CERT_FILE:
840 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
841#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
842 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
843 return -1;
844#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000845 break;
846
Benny Prijonof3bbc132006-12-25 06:43:59 +0000847 case OPT_TLS_PRIV_FILE:
848 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
849 break;
850
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000851 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +0000852 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
853#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
854 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
855 return -1;
856#endif
857 break;
858
859 case OPT_TLS_VERIFY_SERVER:
860 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
861 break;
862
863 case OPT_TLS_VERIFY_CLIENT:
864 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
865 break;
866
867 case OPT_TLS_NEG_TIMEOUT:
868 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000869 break;
870
Benny Prijono4e5d5512007-03-06 18:11:30 +0000871 case OPT_CAPTURE_DEV:
872 cfg->capture_dev = atoi(pj_optarg);
873 break;
874
875 case OPT_PLAYBACK_DEV:
876 cfg->playback_dev = atoi(pj_optarg);
877 break;
878
Benny Prijono22a300a2006-06-14 20:04:55 +0000879 default:
Benny Prijono787b8692006-06-19 12:40:03 +0000880 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +0000881 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijono6dd967c2006-12-26 02:27:14 +0000882 argv[pj_optind]));
Benny Prijono22a300a2006-06-14 20:04:55 +0000883 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000884 }
885 }
886
887 if (pj_optind != argc) {
888 pj_str_t uri_arg;
889
890 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
891 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
892 return -1;
893 }
894 uri_arg = pj_str(argv[pj_optind]);
895 if (uri_to_call)
896 *uri_to_call = uri_arg;
897 pj_optind++;
898
899 /* Add URI to call to buddy list if it's not already there */
900 for (i=0; i<cfg->buddy_cnt; ++i) {
901 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
902 break;
903 }
904 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
905 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
906 }
907
908 } else {
909 if (uri_to_call)
910 uri_to_call->slen = 0;
911 }
912
913 if (pj_optind != argc) {
914 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
915 return PJ_EINVAL;
916 }
917
Benny Prijono56315612006-07-18 14:39:40 +0000918 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
919 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000920
921 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000922 if (cfg->acc_cfg[i].cred_info[cfg->acc_cfg[i].cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000923 {
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000924 cfg->acc_cfg[i].cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000925 }
926 }
927
928
929 return PJ_SUCCESS;
930}
931
932
933/*
934 * Save account settings
935 */
936static void write_account_settings(int acc_index, pj_str_t *result)
937{
938 unsigned i;
939 char line[128];
940 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
941
942
943 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
944 pj_strcat2(result, line);
945
946
947 /* Identity */
948 if (acc_cfg->id.slen) {
949 pj_ansi_sprintf(line, "--id %.*s\n",
950 (int)acc_cfg->id.slen,
951 acc_cfg->id.ptr);
952 pj_strcat2(result, line);
953 }
954
955 /* Registrar server */
956 if (acc_cfg->reg_uri.slen) {
957 pj_ansi_sprintf(line, "--registrar %.*s\n",
958 (int)acc_cfg->reg_uri.slen,
959 acc_cfg->reg_uri.ptr);
960 pj_strcat2(result, line);
961
962 pj_ansi_sprintf(line, "--reg-timeout %u\n",
963 acc_cfg->reg_timeout);
964 pj_strcat2(result, line);
965 }
966
967 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000968 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000969 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +0000970 (int)acc_cfg->force_contact.slen,
971 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000972 pj_strcat2(result, line);
973 }
974
975 /* Proxy */
976 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
977 pj_ansi_sprintf(line, "--proxy %.*s\n",
978 (int)acc_cfg->proxy[i].slen,
979 acc_cfg->proxy[i].ptr);
980 pj_strcat2(result, line);
981 }
982
983 /* Credentials */
984 for (i=0; i<acc_cfg->cred_count; ++i) {
985 if (acc_cfg->cred_info[i].realm.slen) {
986 pj_ansi_sprintf(line, "--realm %.*s\n",
987 (int)acc_cfg->cred_info[i].realm.slen,
988 acc_cfg->cred_info[i].realm.ptr);
989 pj_strcat2(result, line);
990 }
991
992 if (acc_cfg->cred_info[i].username.slen) {
993 pj_ansi_sprintf(line, "--username %.*s\n",
994 (int)acc_cfg->cred_info[i].username.slen,
995 acc_cfg->cred_info[i].username.ptr);
996 pj_strcat2(result, line);
997 }
998
999 if (acc_cfg->cred_info[i].data.slen) {
1000 pj_ansi_sprintf(line, "--password %.*s\n",
1001 (int)acc_cfg->cred_info[i].data.slen,
1002 acc_cfg->cred_info[i].data.ptr);
1003 pj_strcat2(result, line);
1004 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001005
1006 if (i != acc_cfg->cred_count - 1)
1007 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001008 }
1009
1010}
1011
1012
1013/*
1014 * Write settings.
1015 */
1016static int write_settings(const struct app_config *config,
1017 char *buf, pj_size_t max)
1018{
1019 unsigned acc_index;
1020 unsigned i;
1021 pj_str_t cfg;
1022 char line[128];
1023
1024 PJ_UNUSED_ARG(max);
1025
1026 cfg.ptr = buf;
1027 cfg.slen = 0;
1028
1029 /* Logging. */
1030 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1031 pj_ansi_sprintf(line, "--log-level %d\n",
1032 config->log_cfg.level);
1033 pj_strcat2(&cfg, line);
1034
1035 pj_ansi_sprintf(line, "--app-log-level %d\n",
1036 config->log_cfg.console_level);
1037 pj_strcat2(&cfg, line);
1038
1039 if (config->log_cfg.log_filename.slen) {
1040 pj_ansi_sprintf(line, "--log-file %.*s\n",
1041 (int)config->log_cfg.log_filename.slen,
1042 config->log_cfg.log_filename.ptr);
1043 pj_strcat2(&cfg, line);
1044 }
1045
1046
1047 /* Save account settings. */
1048 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1049
1050 write_account_settings(acc_index, &cfg);
1051
1052 if (acc_index < config->acc_cnt-1)
1053 pj_strcat2(&cfg, "--next-account\n");
1054 }
1055
1056
1057 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1058
1059 /* Outbound proxy */
1060 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1061 pj_ansi_sprintf(line, "--outbound %.*s\n",
1062 (int)config->cfg.outbound_proxy[i].slen,
1063 config->cfg.outbound_proxy[i].ptr);
1064 pj_strcat2(&cfg, line);
1065 }
1066
1067
1068 /* UDP Transport. */
1069 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1070 pj_strcat2(&cfg, line);
1071
Benny Prijono0a5cad82006-09-26 13:21:02 +00001072 /* IP address, if any. */
1073 if (config->udp_cfg.public_addr.slen) {
1074 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1075 (int)config->udp_cfg.public_addr.slen,
1076 config->udp_cfg.public_addr.ptr);
1077 pj_strcat2(&cfg, line);
1078 }
1079
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001080 /* No TCP ? */
1081 if (config->no_tcp) {
1082 pj_strcat2(&cfg, "--no-tcp\n");
1083 }
1084
1085 /* No UDP ? */
1086 if (config->no_udp) {
1087 pj_strcat2(&cfg, "--no-udp\n");
1088 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089
1090 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001091 if (config->cfg.stun_domain.slen) {
1092 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1093 (int)config->cfg.stun_domain.slen,
1094 config->cfg.stun_domain.ptr);
1095 pj_strcat2(&cfg, line);
1096 }
1097 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001098 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001099 (int)config->cfg.stun_host.slen,
1100 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001101 pj_strcat2(&cfg, line);
1102 }
1103
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001104
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001105 /* TLS */
1106 if (config->use_tls)
1107 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001108 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001109 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001110 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1111 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001112 pj_strcat2(&cfg, line);
1113 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001114 if (config->udp_cfg.tls_setting.cert_file.slen) {
1115 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1116 (int)config->udp_cfg.tls_setting.cert_file.slen,
1117 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001118 pj_strcat2(&cfg, line);
1119 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001120 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1121 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1122 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1123 config->udp_cfg.tls_setting.privkey_file.ptr);
1124 pj_strcat2(&cfg, line);
1125 }
1126
1127 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001128 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001129 (int)config->udp_cfg.tls_setting.password.slen,
1130 config->udp_cfg.tls_setting.password.ptr);
1131 pj_strcat2(&cfg, line);
1132 }
1133
1134 if (config->udp_cfg.tls_setting.verify_server)
1135 pj_strcat2(&cfg, "--tls-verify-server\n");
1136
1137 if (config->udp_cfg.tls_setting.verify_client)
1138 pj_strcat2(&cfg, "--tls-verify-client\n");
1139
1140 if (config->udp_cfg.tls_setting.timeout.sec) {
1141 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001142 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001143 pj_strcat2(&cfg, line);
1144 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001145
1146 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1147
1148
1149 /* Media */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001150 if (config->media_cfg.enable_ice)
1151 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001152 if (config->null_audio)
1153 pj_strcat2(&cfg, "--null-audio\n");
1154 if (config->auto_play)
1155 pj_strcat2(&cfg, "--auto-play\n");
1156 if (config->auto_loop)
1157 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001158 if (config->auto_conf)
1159 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001160 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001161 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001162 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001163 pj_strcat2(&cfg, line);
1164 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001165 for (i=0; i<config->tone_count; ++i) {
1166 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1167 config->tones[i].freq1, config->tones[i].freq2,
1168 config->tones[i].on_msec, config->tones[i].off_msec);
1169 pj_strcat2(&cfg, line);
1170 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001171 if (config->rec_file.slen) {
1172 pj_ansi_sprintf(line, "--rec-file %s\n",
1173 config->rec_file.ptr);
1174 pj_strcat2(&cfg, line);
1175 }
1176 if (config->auto_rec)
1177 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001178 if (config->capture_dev != PJSUA_INVALID_ID) {
1179 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1180 pj_strcat2(&cfg, line);
1181 }
1182 if (config->playback_dev != PJSUA_INVALID_ID) {
1183 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1184 pj_strcat2(&cfg, line);
1185 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001186
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001187 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001188 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001189 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001190 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001191 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001192 } else {
1193 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1194 config->media_cfg.clock_rate);
1195 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001196 }
Benny Prijono70972992006-08-05 11:13:58 +00001197
1198 /* quality */
1199 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001200 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001201 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001202 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001203 } else {
1204 pj_ansi_sprintf(line, "#using default --quality %d\n",
1205 config->media_cfg.quality);
1206 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001207 }
Benny Prijono0498d902006-06-19 14:49:14 +00001208
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001209
1210 /* ptime */
1211 if (config->ptime) {
1212 pj_ansi_sprintf(line, "--ptime %d\n",
1213 config->ptime);
1214 pj_strcat2(&cfg, line);
1215 }
1216
Benny Prijono70972992006-08-05 11:13:58 +00001217 /* no-vad */
1218 if (config->media_cfg.no_vad) {
1219 pj_strcat2(&cfg, "--no-vad\n");
1220 }
1221
1222 /* ec-tail */
1223 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1224 pj_ansi_sprintf(line, "--ec-tail %d\n",
1225 config->media_cfg.ec_tail_len);
1226 pj_strcat2(&cfg, line);
1227 } else {
1228 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1229 config->media_cfg.ec_tail_len);
1230 pj_strcat2(&cfg, line);
1231 }
1232
1233
1234 /* ilbc-mode */
1235 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1236 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1237 config->media_cfg.ilbc_mode);
1238 pj_strcat2(&cfg, line);
1239 } else {
1240 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1241 config->media_cfg.ilbc_mode);
1242 pj_strcat2(&cfg, line);
1243 }
1244
1245 /* RTP drop */
1246 if (config->media_cfg.tx_drop_pct) {
1247 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1248 config->media_cfg.tx_drop_pct);
1249 pj_strcat2(&cfg, line);
1250
1251 }
1252 if (config->media_cfg.rx_drop_pct) {
1253 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1254 config->media_cfg.rx_drop_pct);
1255 pj_strcat2(&cfg, line);
1256
1257 }
1258
1259
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001260 /* Start RTP port. */
1261 pj_ansi_sprintf(line, "--rtp-port %d\n",
1262 config->rtp_cfg.port);
1263 pj_strcat2(&cfg, line);
1264
1265 /* Add codec. */
1266 for (i=0; i<config->codec_cnt; ++i) {
1267 pj_ansi_sprintf(line, "--add-codec %s\n",
1268 config->codec_arg[i].ptr);
1269 pj_strcat2(&cfg, line);
1270 }
1271
1272 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1273
1274 /* Auto-answer. */
1275 if (config->auto_answer != 0) {
1276 pj_ansi_sprintf(line, "--auto-answer %d\n",
1277 config->auto_answer);
1278 pj_strcat2(&cfg, line);
1279 }
1280
1281 /* Max calls. */
1282 pj_ansi_sprintf(line, "--max-calls %d\n",
1283 config->cfg.max_calls);
1284 pj_strcat2(&cfg, line);
1285
1286 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001287 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001288 pj_ansi_sprintf(line, "--duration %d\n",
1289 config->duration);
1290 pj_strcat2(&cfg, line);
1291 }
1292
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001293 /* norefersub ? */
1294 if (config->no_refersub) {
1295 pj_strcat2(&cfg, "--norefersub\n");
1296 }
1297
1298
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001299 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1300
1301 /* Add buddies. */
1302 for (i=0; i<config->buddy_cnt; ++i) {
1303 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1304 (int)config->buddy_cfg[i].uri.slen,
1305 config->buddy_cfg[i].uri.ptr);
1306 pj_strcat2(&cfg, line);
1307 }
1308
1309
1310 *(cfg.ptr + cfg.slen) = '\0';
1311 return cfg.slen;
1312}
1313
1314
1315/*
1316 * Dump application states.
1317 */
1318static void app_dump(pj_bool_t detail)
1319{
1320 unsigned old_decor;
1321 char buf[1024];
1322
1323 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
1324
1325 old_decor = pj_log_get_decor();
1326 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1327
1328 if (detail)
1329 pj_dump_config();
1330
1331 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
1332 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
1333 pjsip_tsx_layer_dump(detail);
1334 pjsip_ua_dump(detail);
1335
1336
1337 /* Dump all invite sessions: */
1338 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
1339
1340 if (pjsua_call_get_count() == 0) {
1341
1342 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
1343
1344 } else {
1345 unsigned i;
1346
1347 for (i=0; i<app_config.cfg.max_calls; ++i) {
1348 if (pjsua_call_is_active(i)) {
1349 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
1350 PJ_LOG(3,(THIS_FILE, "%s", buf));
1351 }
1352 }
1353 }
1354
1355 /* Dump presence status */
1356 pjsua_pres_dump(detail);
1357
1358 pj_log_set_decor(old_decor);
1359 PJ_LOG(3,(THIS_FILE, "Dump complete"));
1360}
1361
1362
1363/*****************************************************************************
1364 * Console application
1365 */
1366
1367/*
1368 * Find next call when current call is disconnected or when user
1369 * press ']'
1370 */
1371static pj_bool_t find_next_call(void)
1372{
1373 int i, max;
1374
1375 max = pjsua_call_get_max_count();
1376 for (i=current_call+1; i<max; ++i) {
1377 if (pjsua_call_is_active(i)) {
1378 current_call = i;
1379 return PJ_TRUE;
1380 }
1381 }
1382
1383 for (i=0; i<current_call; ++i) {
1384 if (pjsua_call_is_active(i)) {
1385 current_call = i;
1386 return PJ_TRUE;
1387 }
1388 }
1389
1390 current_call = PJSUA_INVALID_ID;
1391 return PJ_FALSE;
1392}
1393
1394
1395/*
1396 * Find previous call when user press '['
1397 */
1398static pj_bool_t find_prev_call(void)
1399{
1400 int i, max;
1401
1402 max = pjsua_call_get_max_count();
1403 for (i=current_call-1; i>=0; --i) {
1404 if (pjsua_call_is_active(i)) {
1405 current_call = i;
1406 return PJ_TRUE;
1407 }
1408 }
1409
1410 for (i=max-1; i>current_call; --i) {
1411 if (pjsua_call_is_active(i)) {
1412 current_call = i;
1413 return PJ_TRUE;
1414 }
1415 }
1416
1417 current_call = PJSUA_INVALID_ID;
1418 return PJ_FALSE;
1419}
1420
1421
Benny Prijono804ff0a2006-09-14 11:17:48 +00001422/* Callback from timer when the maximum call duration has been
1423 * exceeded.
1424 */
1425static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1426 struct pj_timer_entry *entry)
1427{
1428 pjsua_call_id call_id = entry->id;
1429 pjsua_msg_data msg_data;
1430 pjsip_generic_string_hdr warn;
1431 pj_str_t hname = pj_str("Warning");
1432 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1433
1434 PJ_UNUSED_ARG(timer_heap);
1435
Benny Prijono148c9dd2006-09-19 13:37:53 +00001436 if (call_id == PJSUA_INVALID_ID) {
1437 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1438 return;
1439 }
1440
Benny Prijono804ff0a2006-09-14 11:17:48 +00001441 /* Add warning header */
1442 pjsua_msg_data_init(&msg_data);
1443 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1444 pj_list_push_back(&msg_data.hdr_list, &warn);
1445
1446 /* Call duration has been exceeded; disconnect the call */
1447 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1448 "for call %d, disconnecting the call",
1449 app_config.duration, call_id));
1450 entry->id = PJSUA_INVALID_ID;
1451 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1452}
1453
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001454
1455/*
1456 * Handler when invite state has changed.
1457 */
1458static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1459{
1460 pjsua_call_info call_info;
1461
1462 PJ_UNUSED_ARG(e);
1463
1464 pjsua_call_get_info(call_id, &call_info);
1465
1466 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1467
Benny Prijono804ff0a2006-09-14 11:17:48 +00001468 /* Cancel duration timer, if any */
1469 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1470 struct call_data *cd = &app_config.call_data[call_id];
1471 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1472
1473 cd->timer.id = PJSUA_INVALID_ID;
1474 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1475 }
1476
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001477 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1478 call_id,
1479 call_info.last_status,
1480 call_info.last_status_text.ptr));
1481
1482 if (call_id == current_call) {
1483 find_next_call();
1484 }
1485
Benny Prijono4be63b52006-11-25 14:50:25 +00001486 /* Dump media state upon disconnected */
1487 if (1) {
1488 char buf[1024];
1489 pjsua_call_dump(call_id, PJ_TRUE, buf,
1490 sizeof(buf), " ");
1491 PJ_LOG(5,(THIS_FILE,
1492 "Call %d disconnected, dumping media stats\n%s",
1493 call_id, buf));
1494 }
1495
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001496 } else {
1497
Benny Prijono804ff0a2006-09-14 11:17:48 +00001498 if (app_config.duration!=NO_LIMIT &&
1499 call_info.state == PJSIP_INV_STATE_CONFIRMED)
1500 {
1501 /* Schedule timer to hangup call after the specified duration */
1502 struct call_data *cd = &app_config.call_data[call_id];
1503 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1504 pj_time_val delay;
1505
1506 cd->timer.id = call_id;
1507 delay.sec = app_config.duration;
1508 delay.msec = 0;
1509 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
1510 }
1511
Benny Prijono4be63b52006-11-25 14:50:25 +00001512 if (call_info.state == PJSIP_INV_STATE_EARLY) {
1513 int code;
1514 pj_str_t reason;
1515 pjsip_msg *msg;
1516
1517 /* This can only occur because of TX or RX message */
1518 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
1519
1520 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1521 msg = e->body.tsx_state.src.rdata->msg_info.msg;
1522 } else {
1523 msg = e->body.tsx_state.src.tdata->msg;
1524 }
1525
1526 code = msg->line.status.code;
1527 reason = msg->line.status.reason;
1528
1529 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
1530 call_id, call_info.state_text.ptr,
1531 code, (int)reason.slen, reason.ptr));
1532 } else {
1533 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
1534 call_id,
1535 call_info.state_text.ptr));
1536 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001537
1538 if (current_call==PJSUA_INVALID_ID)
1539 current_call = call_id;
1540
1541 }
1542}
1543
1544
1545/**
1546 * Handler when there is incoming call.
1547 */
1548static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
1549 pjsip_rx_data *rdata)
1550{
1551 pjsua_call_info call_info;
1552
1553 PJ_UNUSED_ARG(acc_id);
1554 PJ_UNUSED_ARG(rdata);
1555
1556 pjsua_call_get_info(call_id, &call_info);
1557
1558 if (app_config.auto_answer > 0) {
1559 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
1560 }
1561
1562 if (app_config.auto_answer < 200) {
1563 PJ_LOG(3,(THIS_FILE,
1564 "Incoming call for account %d!\n"
1565 "From: %s\n"
1566 "To: %s\n"
1567 "Press a to answer or h to reject call",
1568 acc_id,
1569 call_info.remote_info.ptr,
1570 call_info.local_info.ptr));
1571 }
1572}
1573
1574
1575/*
1576 * Callback on media state changed event.
1577 * The action may connect the call to sound device, to file, or
1578 * to loop the call.
1579 */
1580static void on_call_media_state(pjsua_call_id call_id)
1581{
1582 pjsua_call_info call_info;
1583
1584 pjsua_call_get_info(call_id, &call_info);
1585
1586 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
1587 pj_bool_t connect_sound = PJ_TRUE;
1588
1589 /* Loopback sound, if desired */
1590 if (app_config.auto_loop) {
1591 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
1592 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00001593
1594 /* Automatically record conversation, if desired */
1595 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1596 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1597 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001598 }
1599
1600 /* Stream a file, if desired */
1601 if (app_config.auto_play && app_config.wav_port != PJSUA_INVALID_ID) {
1602 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
1603 connect_sound = PJ_FALSE;
1604 }
1605
Benny Prijono7ca96da2006-08-07 12:11:40 +00001606 /* Put call in conference with other calls, if desired */
1607 if (app_config.auto_conf) {
1608 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00001609 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00001610 unsigned i;
1611
1612 /* Get all calls, and establish media connection between
1613 * this call and other calls.
1614 */
1615 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00001616
Benny Prijono7ca96da2006-08-07 12:11:40 +00001617 for (i=0; i<call_cnt; ++i) {
1618 if (call_ids[i] == call_id)
1619 continue;
1620
1621 if (!pjsua_call_has_media(call_ids[i]))
1622 continue;
1623
1624 pjsua_conf_connect(call_info.conf_slot,
1625 pjsua_call_get_conf_port(call_ids[i]));
1626 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1627 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001628
1629 /* Automatically record conversation, if desired */
1630 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1631 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1632 app_config.rec_port);
1633 }
1634
Benny Prijono7ca96da2006-08-07 12:11:40 +00001635 }
1636
1637 /* Also connect call to local sound device */
1638 connect_sound = PJ_TRUE;
1639 }
1640
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001641 /* Otherwise connect to sound device */
1642 if (connect_sound) {
1643 pjsua_conf_connect(call_info.conf_slot, 0);
1644 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001645
1646 /* Automatically record conversation, if desired */
1647 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1648 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1649 pjsua_conf_connect(0, app_config.rec_port);
1650 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001651 }
1652
1653 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
1654
1655 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
1656 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
1657 call_id));
1658 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
1659 PJ_LOG(3,(THIS_FILE,
1660 "Media for call %d is suspended (hold) by remote",
1661 call_id));
1662 } else {
1663 PJ_LOG(3,(THIS_FILE,
1664 "Media for call %d is inactive",
1665 call_id));
1666 }
1667}
1668
Benny Prijono0875ae82006-12-26 00:11:48 +00001669/*
1670 * DTMF callback.
1671 */
1672static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
1673{
1674 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
1675}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001676
1677/*
1678 * Handler registration status has changed.
1679 */
1680static void on_reg_state(pjsua_acc_id acc_id)
1681{
1682 PJ_UNUSED_ARG(acc_id);
1683
1684 // Log already written.
1685}
1686
1687
1688/*
1689 * Handler on buddy state changed.
1690 */
1691static void on_buddy_state(pjsua_buddy_id buddy_id)
1692{
1693 pjsua_buddy_info info;
1694 pjsua_buddy_get_info(buddy_id, &info);
1695
1696 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
1697 (int)info.uri.slen,
1698 info.uri.ptr,
1699 (int)info.status_text.slen,
1700 info.status_text.ptr));
1701}
1702
1703
1704/**
1705 * Incoming IM message (i.e. MESSAGE request)!
1706 */
1707static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
1708 const pj_str_t *to, const pj_str_t *contact,
1709 const pj_str_t *mime_type, const pj_str_t *text)
1710{
1711 /* Note: call index may be -1 */
1712 PJ_UNUSED_ARG(call_id);
1713 PJ_UNUSED_ARG(to);
1714 PJ_UNUSED_ARG(contact);
1715 PJ_UNUSED_ARG(mime_type);
1716
1717 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s",
1718 (int)from->slen, from->ptr,
1719 (int)text->slen, text->ptr));
1720}
1721
1722
1723/**
1724 * Received typing indication
1725 */
1726static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
1727 const pj_str_t *to, const pj_str_t *contact,
1728 pj_bool_t is_typing)
1729{
1730 PJ_UNUSED_ARG(call_id);
1731 PJ_UNUSED_ARG(to);
1732 PJ_UNUSED_ARG(contact);
1733
1734 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
1735 (int)from->slen, from->ptr,
1736 (is_typing?"is typing..":"has stopped typing")));
1737}
1738
1739
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001740/**
1741 * Call transfer request status.
1742 */
1743static void on_call_transfer_status(pjsua_call_id call_id,
1744 int status_code,
1745 const pj_str_t *status_text,
1746 pj_bool_t final,
1747 pj_bool_t *p_cont)
1748{
1749 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
1750 call_id, status_code,
1751 (int)status_text->slen, status_text->ptr,
1752 (final ? "[final]" : "")));
1753
1754 if (status_code/100 == 2) {
1755 PJ_LOG(3,(THIS_FILE,
1756 "Call %d: call transfered successfully, disconnecting call",
1757 call_id));
1758 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
1759 *p_cont = PJ_FALSE;
1760 }
1761}
1762
1763
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001764/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00001765 * Notification that call is being replaced.
1766 */
1767static void on_call_replaced(pjsua_call_id old_call_id,
1768 pjsua_call_id new_call_id)
1769{
1770 pjsua_call_info old_ci, new_ci;
1771
1772 pjsua_call_get_info(old_call_id, &old_ci);
1773 pjsua_call_get_info(new_call_id, &new_ci);
1774
1775 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
1776 "call %d with %.*s",
1777 old_call_id,
1778 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
1779 new_call_id,
1780 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
1781}
1782
1783
1784/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001785 * Print buddy list.
1786 */
1787static void print_buddy_list(void)
1788{
1789 pjsua_buddy_id ids[64];
1790 int i;
1791 unsigned count = PJ_ARRAY_SIZE(ids);
1792
1793 puts("Buddy list:");
1794
1795 pjsua_enum_buddies(ids, &count);
1796
1797 if (count == 0)
1798 puts(" -none-");
1799 else {
1800 for (i=0; i<(int)count; ++i) {
1801 pjsua_buddy_info info;
1802
1803 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
1804 continue;
1805
1806 printf(" [%2d] <%7s> %.*s\n",
1807 ids[i]+1, info.status_text.ptr,
1808 (int)info.uri.slen,
1809 info.uri.ptr);
1810 }
1811 }
1812 puts("");
1813}
1814
1815
1816/*
1817 * Print account status.
1818 */
1819static void print_acc_status(int acc_id)
1820{
1821 char buf[80];
1822 pjsua_acc_info info;
1823
1824 pjsua_acc_get_info(acc_id, &info);
1825
1826 if (!info.has_registration) {
1827 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
1828 (int)info.status_text.slen,
1829 info.status_text.ptr);
1830
1831 } else {
1832 pj_ansi_snprintf(buf, sizeof(buf),
1833 "%d/%.*s (expires=%d)",
1834 info.status,
1835 (int)info.status_text.slen,
1836 info.status_text.ptr,
1837 info.expires);
1838
1839 }
1840
1841 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
1842 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
1843 printf(" Online status: %s\n",
1844 (info.online_status ? "Online" : "Invisible"));
1845}
1846
1847
1848/*
1849 * Show a bit of help.
1850 */
1851static void keystroke_help(void)
1852{
1853 pjsua_acc_id acc_ids[16];
1854 unsigned count = PJ_ARRAY_SIZE(acc_ids);
1855 int i;
1856
1857 printf(">>>>\n");
1858
1859 pjsua_enum_accs(acc_ids, &count);
1860
1861 printf("Account list:\n");
1862 for (i=0; i<(int)count; ++i)
1863 print_acc_status(acc_ids[i]);
1864
1865 print_buddy_list();
1866
1867 //puts("Commands:");
1868 puts("+=============================================================================+");
1869 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
1870 puts("| | | |");
1871 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
1872 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
1873 puts("| a Answer call | !b Modify buddy | !a Modify accnt. |");
1874 puts("| h Hangup call (ha=all) | i Send IM | rr (Re-)register |");
1875 puts("| H Hold call | s Subscribe presence | ru Unregister |");
1876 puts("| v re-inVite (release hold) | u Unsubscribe presence | > Cycle next ac.|");
1877 puts("| ] Select next dialog | t ToGgle Online status | < Cycle prev ac.|");
1878 puts("| [ Select previous dialog +--------------------------+-------------------+");
1879 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00001880 puts("| X Xfer with Replaces | | |");
1881 puts("| # Send DTMF string | cl List ports | d Dump status |");
1882 puts("| dq Dump curr. call quality | cc Connect port | dd Dump detailed |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001883 puts("| | cd Disconnect port | dc Dump config |");
Benny Prijono6dd967c2006-12-26 02:27:14 +00001884 puts("| S Send arbitrary REQUEST | V Adjust audio Volume | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001885 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijono990042e2007-01-21 19:36:00 +00001886 puts("| q QUIT sleep N: console sleep for N ms |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001887 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00001888
1889 i = pjsua_call_get_count();
1890 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00001891
1892 if (current_call != PJSUA_INVALID_ID) {
1893 pjsua_call_info ci;
1894 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
1895 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
1896 (int)ci.remote_info.slen, ci.remote_info.ptr,
1897 (int)ci.state_text.slen, ci.state_text.ptr);
1898 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001899}
1900
1901
1902/*
1903 * Input simple string
1904 */
1905static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
1906{
1907 char *p;
1908
1909 printf("%s (empty to cancel): ", title); fflush(stdout);
1910 fgets(buf, len, stdin);
1911
1912 /* Remove trailing newlines. */
1913 for (p=buf; ; ++p) {
1914 if (*p=='\r' || *p=='\n') *p='\0';
1915 else if (!*p) break;
1916 }
1917
1918 if (!*buf)
1919 return PJ_FALSE;
1920
1921 return PJ_TRUE;
1922}
1923
1924
1925#define NO_NB -2
1926struct input_result
1927{
1928 int nb_result;
1929 char *uri_result;
1930};
1931
1932
1933/*
1934 * Input URL.
1935 */
1936static void ui_input_url(const char *title, char *buf, int len,
1937 struct input_result *result)
1938{
1939 result->nb_result = NO_NB;
1940 result->uri_result = NULL;
1941
1942 print_buddy_list();
1943
1944 printf("Choices:\n"
1945 " 0 For current dialog.\n"
1946 " -1 All %d buddies in buddy list\n"
1947 " [1 -%2d] Select from buddy list\n"
1948 " URL An URL\n"
1949 " <Enter> Empty input (or 'q') to cancel\n"
1950 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
1951 printf("%s: ", title);
1952
1953 fflush(stdout);
1954 fgets(buf, len, stdin);
1955 len = strlen(buf);
1956
1957 /* Left trim */
1958 while (pj_isspace(*buf)) {
1959 ++buf;
1960 --len;
1961 }
1962
1963 /* Remove trailing newlines */
1964 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
1965 buf[--len] = '\0';
1966
1967 if (len == 0 || buf[0]=='q')
1968 return;
1969
1970 if (pj_isdigit(*buf) || *buf=='-') {
1971
1972 int i;
1973
1974 if (*buf=='-')
1975 i = 1;
1976 else
1977 i = 0;
1978
1979 for (; i<len; ++i) {
1980 if (!pj_isdigit(buf[i])) {
1981 puts("Invalid input");
1982 return;
1983 }
1984 }
1985
1986 result->nb_result = my_atoi(buf);
1987
1988 if (result->nb_result >= 0 &&
1989 result->nb_result <= (int)pjsua_get_buddy_count())
1990 {
1991 return;
1992 }
1993 if (result->nb_result == -1)
1994 return;
1995
1996 puts("Invalid input");
1997 result->nb_result = NO_NB;
1998 return;
1999
2000 } else {
2001 pj_status_t status;
2002
2003 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2004 pjsua_perror(THIS_FILE, "Invalid URL", status);
2005 return;
2006 }
2007
2008 result->uri_result = buf;
2009 }
2010}
2011
2012/*
2013 * List the ports in conference bridge
2014 */
2015static void conf_list(void)
2016{
2017 unsigned i, count;
2018 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2019
2020 printf("Conference ports:\n");
2021
2022 count = PJ_ARRAY_SIZE(id);
2023 pjsua_enum_conf_ports(id, &count);
2024
2025 for (i=0; i<count; ++i) {
2026 char txlist[PJSUA_MAX_CALLS*4+10];
2027 unsigned j;
2028 pjsua_conf_port_info info;
2029
2030 pjsua_conf_get_port_info(id[i], &info);
2031
2032 txlist[0] = '\0';
2033 for (j=0; j<info.listener_cnt; ++j) {
2034 char s[10];
2035 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2036 pj_ansi_strcat(txlist, s);
2037 }
2038 printf("Port #%02d[%2dKHz/%dms] %20.*s transmitting to: %s\n",
2039 info.slot_id,
2040 info.clock_rate/1000,
2041 info.samples_per_frame * 1000 / info.clock_rate,
2042 (int)info.name.slen,
2043 info.name.ptr,
2044 txlist);
2045
2046 }
2047 puts("");
2048}
2049
2050
2051/*
Benny Prijono56315612006-07-18 14:39:40 +00002052 * Send arbitrary request to remote host
2053 */
2054static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2055{
2056 pj_str_t str_method;
2057 pjsip_method method;
2058 pjsip_tx_data *tdata;
2059 pjsua_acc_info acc_info;
2060 pjsip_endpoint *endpt;
2061 pj_status_t status;
2062
2063 endpt = pjsua_get_pjsip_endpt();
2064
2065 str_method = pj_str(cstr_method);
2066 pjsip_method_init_np(&method, &str_method);
2067
2068 pjsua_acc_get_info(current_acc, &acc_info);
2069
2070 status = pjsip_endpt_create_request(endpt, &method, dst_uri,
2071 &acc_info.acc_uri, dst_uri,
2072 NULL, NULL, -1, NULL, &tdata);
2073 if (status != PJ_SUCCESS) {
2074 pjsua_perror(THIS_FILE, "Unable to create request", status);
2075 return;
2076 }
2077
2078 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
2079 if (status != PJ_SUCCESS) {
2080 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002081 return;
2082 }
2083}
2084
2085
2086/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002087 * Main "user interface" loop.
2088 */
2089void console_app_main(const pj_str_t *uri_to_call)
2090{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002091 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002092 char buf[128];
2093 char text[128];
2094 int i, count;
2095 char *uri;
2096 pj_str_t tmp;
2097 struct input_result result;
2098 pjsua_call_info call_info;
2099 pjsua_acc_info acc_info;
2100
2101
2102 /* If user specifies URI to call, then call the URI */
2103 if (uri_to_call->slen) {
2104 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2105 }
2106
2107 keystroke_help();
2108
2109 for (;;) {
2110
2111 printf(">>> ");
2112 fflush(stdout);
2113
Benny Prijono990042e2007-01-21 19:36:00 +00002114 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2115 /*
2116 * Be friendly to users who redirect commands into
2117 * program, when file ends, resume with kbd.
2118 * If exit is desired end script with q for quit
2119 */
2120 /* Reopen stdin/stdout/stderr to /dev/console */
2121#if defined(PJ_WIN32) && PJ_WIN32!=0
2122 if (freopen ("CONIN$", "r", stdin) == NULL) {
2123#else
2124 if (1) {
2125#endif
2126 puts("Cannot switch back to console from file redirection");
2127 menuin[0] = 'q';
2128 menuin[1] = '\0';
2129 } else {
2130 puts("Switched back to console from file redirection");
2131 continue;
2132 }
2133 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002134
2135 switch (menuin[0]) {
2136
2137 case 'm':
2138 /* Make call! : */
2139 printf("(You currently have %d calls)\n",
2140 pjsua_call_get_count());
2141
2142 uri = NULL;
2143 ui_input_url("Make call", buf, sizeof(buf), &result);
2144 if (result.nb_result != NO_NB) {
2145
2146 if (result.nb_result == -1 || result.nb_result == 0) {
2147 puts("You can't do that with make call!");
2148 continue;
2149 } else {
2150 pjsua_buddy_info binfo;
2151 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2152 uri = binfo.uri.ptr;
2153 }
2154
2155 } else if (result.uri_result) {
2156 uri = result.uri_result;
2157 }
2158
2159 tmp = pj_str(uri);
2160 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2161 break;
2162
2163 case 'M':
2164 /* Make multiple calls! : */
2165 printf("(You currently have %d calls)\n",
2166 pjsua_call_get_count());
2167
2168 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2169 continue;
2170
2171 count = my_atoi(menuin);
2172 if (count < 1)
2173 continue;
2174
2175 ui_input_url("Make call", buf, sizeof(buf), &result);
2176 if (result.nb_result != NO_NB) {
2177 pjsua_buddy_info binfo;
2178 if (result.nb_result == -1 || result.nb_result == 0) {
2179 puts("You can't do that with make call!");
2180 continue;
2181 }
2182 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2183 uri = binfo.uri.ptr;
2184 } else {
2185 uri = result.uri_result;
2186 }
2187
2188 for (i=0; i<my_atoi(menuin); ++i) {
2189 pj_status_t status;
2190
2191 tmp = pj_str(uri);
2192 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2193 NULL, NULL);
2194 if (status != PJ_SUCCESS)
2195 break;
2196 }
2197 break;
2198
2199 case 'i':
2200 /* Send instant messaeg */
2201
2202 /* i is for call index to send message, if any */
2203 i = -1;
2204
2205 /* Make compiler happy. */
2206 uri = NULL;
2207
2208 /* Input destination. */
2209 ui_input_url("Send IM to", buf, sizeof(buf), &result);
2210 if (result.nb_result != NO_NB) {
2211
2212 if (result.nb_result == -1) {
2213 puts("You can't send broadcast IM like that!");
2214 continue;
2215
2216 } else if (result.nb_result == 0) {
2217
2218 i = current_call;
2219
2220 } else {
2221 pjsua_buddy_info binfo;
2222 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2223 uri = binfo.uri.ptr;
2224 }
2225
2226 } else if (result.uri_result) {
2227 uri = result.uri_result;
2228 }
2229
2230
2231 /* Send typing indication. */
2232 if (i != -1)
2233 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
2234 else {
2235 pj_str_t tmp_uri = pj_str(uri);
2236 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
2237 }
2238
2239 /* Input the IM . */
2240 if (!simple_input("Message", text, sizeof(text))) {
2241 /*
2242 * Cancelled.
2243 * Send typing notification too, saying we're not typing.
2244 */
2245 if (i != -1)
2246 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
2247 else {
2248 pj_str_t tmp_uri = pj_str(uri);
2249 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
2250 }
2251 continue;
2252 }
2253
2254 tmp = pj_str(text);
2255
2256 /* Send the IM */
2257 if (i != -1)
2258 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
2259 else {
2260 pj_str_t tmp_uri = pj_str(uri);
2261 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
2262 }
2263
2264 break;
2265
2266 case 'a':
2267
2268 if (current_call != -1) {
2269 pjsua_call_get_info(current_call, &call_info);
2270 } else {
2271 /* Make compiler happy */
2272 call_info.role = PJSIP_ROLE_UAC;
2273 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
2274 }
2275
2276 if (current_call == -1 ||
2277 call_info.role != PJSIP_ROLE_UAS ||
2278 call_info.state >= PJSIP_INV_STATE_CONNECTING)
2279 {
2280 puts("No pending incoming call");
2281 fflush(stdout);
2282 continue;
2283
2284 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00002285 int st_code;
2286 char contact[120];
2287 pj_str_t hname = { "Contact", 7 };
2288 pj_str_t hvalue;
2289 pjsip_generic_string_hdr hcontact;
2290 pjsua_msg_data msg_data;
2291
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002292 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
2293 continue;
2294
Benny Prijono20d36722007-02-22 14:52:24 +00002295 st_code = my_atoi(buf);
2296 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002297 continue;
2298
Benny Prijono20d36722007-02-22 14:52:24 +00002299 pjsua_msg_data_init(&msg_data);
2300
2301 if (st_code/100 == 3) {
2302 if (!simple_input("Enter URL to be put in Contact",
2303 contact, sizeof(contact)))
2304 continue;
2305 hvalue = pj_str(contact);
2306 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
2307
2308 pj_list_push_back(&msg_data.hdr_list, &hcontact);
2309 }
2310
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002311 /*
2312 * Must check again!
2313 * Call may have been disconnected while we're waiting for
2314 * keyboard input.
2315 */
2316 if (current_call == -1) {
2317 puts("Call has been disconnected");
2318 fflush(stdout);
2319 continue;
2320 }
2321
Benny Prijono20d36722007-02-22 14:52:24 +00002322 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002323 }
2324
2325 break;
2326
2327
2328 case 'h':
2329
2330 if (current_call == -1) {
2331 puts("No current call");
2332 fflush(stdout);
2333 continue;
2334
2335 } else if (menuin[1] == 'a') {
2336
2337 /* Hangup all calls */
2338 pjsua_call_hangup_all();
2339
2340 } else {
2341
2342 /* Hangup current calls */
2343 pjsua_call_hangup(current_call, 0, NULL, NULL);
2344 }
2345 break;
2346
2347 case ']':
2348 case '[':
2349 /*
2350 * Cycle next/prev dialog.
2351 */
2352 if (menuin[0] == ']') {
2353 find_next_call();
2354
2355 } else {
2356 find_prev_call();
2357 }
2358
2359 if (current_call != -1) {
2360
2361 pjsua_call_get_info(current_call, &call_info);
2362 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
2363 (int)call_info.remote_info.slen,
2364 call_info.remote_info.ptr));
2365
2366 } else {
2367 PJ_LOG(3,(THIS_FILE,"No current dialog"));
2368 }
2369 break;
2370
2371
2372 case '>':
2373 case '<':
2374 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
2375 break;
2376
2377 i = my_atoi(buf);
2378 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00002379 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002380 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
2381 } else {
2382 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
2383 }
2384 break;
2385
2386
2387 case '+':
2388 if (menuin[1] == 'b') {
2389
2390 pjsua_buddy_config buddy_cfg;
2391 pjsua_buddy_id buddy_id;
2392 pj_status_t status;
2393
2394 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
2395 break;
2396
2397 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
2398 printf("Invalid SIP URI '%s'\n", buf);
2399 break;
2400 }
2401
Benny Prijonoac623b32006-07-03 15:19:31 +00002402 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002403
2404 buddy_cfg.uri = pj_str(buf);
2405 buddy_cfg.subscribe = PJ_TRUE;
2406
2407 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
2408 if (status == PJ_SUCCESS) {
2409 printf("New buddy '%s' added at index %d\n",
2410 buf, buddy_id+1);
2411 }
2412
2413 } else if (menuin[1] == 'a') {
2414
2415 printf("Sorry, this command is not supported yet\n");
2416
2417 } else {
2418 printf("Invalid input %s\n", menuin);
2419 }
2420 break;
2421
2422 case '-':
2423 if (menuin[1] == 'b') {
2424 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
2425 break;
2426
2427 i = my_atoi(buf) - 1;
2428
2429 if (!pjsua_buddy_is_valid(i)) {
2430 printf("Invalid buddy id %d\n", i);
2431 } else {
2432 pjsua_buddy_del(i);
2433 printf("Buddy %d deleted\n", i);
2434 }
2435
2436 } else if (menuin[1] == 'a') {
2437
2438 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
2439 break;
2440
2441 i = my_atoi(buf);
2442
2443 if (!pjsua_acc_is_valid(i)) {
2444 printf("Invalid account id %d\n", i);
2445 } else {
2446 pjsua_acc_del(i);
2447 printf("Account %d deleted\n", i);
2448 }
2449
2450 } else {
2451 printf("Invalid input %s\n", menuin);
2452 }
2453 break;
2454
2455 case 'H':
2456 /*
2457 * Hold call.
2458 */
2459 if (current_call != -1) {
2460
2461 pjsua_call_set_hold(current_call, NULL);
2462
2463 } else {
2464 PJ_LOG(3,(THIS_FILE, "No current call"));
2465 }
2466 break;
2467
2468 case 'v':
2469 /*
2470 * Send re-INVITE (to release hold, etc).
2471 */
2472 if (current_call != -1) {
2473
2474 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
2475
2476 } else {
2477 PJ_LOG(3,(THIS_FILE, "No current call"));
2478 }
2479 break;
2480
2481 case 'x':
2482 /*
2483 * Transfer call.
2484 */
2485 if (current_call == -1) {
2486
2487 PJ_LOG(3,(THIS_FILE, "No current call"));
2488
2489 } else {
2490 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00002491 pjsua_msg_data msg_data;
2492 pjsip_generic_string_hdr refer_sub;
2493 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2494 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00002495 pjsua_call_info ci;
2496
2497 pjsua_call_get_info(current_call, &ci);
2498 printf("Transfering current call [%d] %.*s\n",
2499 current_call,
2500 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002501
2502 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
2503
2504 /* Check if call is still there. */
2505
2506 if (call != current_call) {
2507 puts("Call has been disconnected");
2508 continue;
2509 }
2510
Benny Prijonod524e822006-09-22 12:48:18 +00002511 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002512 if (app_config.no_refersub) {
2513 /* Add Refer-Sub: false in outgoing REFER request */
2514 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
2515 &STR_FALSE);
2516 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
2517 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002518 if (result.nb_result != NO_NB) {
2519 if (result.nb_result == -1 || result.nb_result == 0)
2520 puts("You can't do that with transfer call!");
2521 else {
2522 pjsua_buddy_info binfo;
2523 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00002524 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002525 }
2526
2527 } else if (result.uri_result) {
2528 pj_str_t tmp;
2529 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00002530 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002531 }
2532 }
2533 break;
2534
Benny Prijonof7b1c392006-11-11 16:46:34 +00002535 case 'X':
2536 /*
2537 * Transfer call with replaces.
2538 */
2539 if (current_call == -1) {
2540
2541 PJ_LOG(3,(THIS_FILE, "No current call"));
2542
2543 } else {
2544 int call = current_call;
2545 int dst_call;
2546 pjsua_msg_data msg_data;
2547 pjsip_generic_string_hdr refer_sub;
2548 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2549 pj_str_t STR_FALSE = { "false", 5 };
2550 pjsua_call_id ids[PJSUA_MAX_CALLS];
2551 pjsua_call_info ci;
2552 unsigned i, count;
2553
2554 count = PJ_ARRAY_SIZE(ids);
2555 pjsua_enum_calls(ids, &count);
2556
2557 if (count <= 1) {
2558 puts("There are no other calls");
2559 continue;
2560 }
2561
2562 pjsua_call_get_info(current_call, &ci);
2563 printf("Transfer call [%d] %.*s to one of the following:\n",
2564 current_call,
2565 (int)ci.remote_info.slen, ci.remote_info.ptr);
2566
2567 for (i=0; i<count; ++i) {
2568 pjsua_call_info call_info;
2569
2570 if (ids[i] == call)
2571 continue;
2572
2573 pjsua_call_get_info(ids[i], &call_info);
2574 printf("%d %.*s [%.*s]\n",
2575 ids[i],
2576 (int)call_info.remote_info.slen,
2577 call_info.remote_info.ptr,
2578 (int)call_info.state_text.slen,
2579 call_info.state_text.ptr);
2580 }
2581
2582 if (!simple_input("Enter call number to be replaced",
2583 buf, sizeof(buf)))
2584 continue;
2585
2586 dst_call = my_atoi(buf);
2587
2588 /* Check if call is still there. */
2589
2590 if (call != current_call) {
2591 puts("Call has been disconnected");
2592 continue;
2593 }
2594
2595 /* Check that destination call is valid. */
2596 if (dst_call == call) {
2597 puts("Destination call number must not be the same "
2598 "as the call being transfered");
2599 continue;
2600 }
2601 if (dst_call >= PJSUA_MAX_CALLS) {
2602 puts("Invalid destination call number");
2603 continue;
2604 }
2605 if (!pjsua_call_is_active(dst_call)) {
2606 puts("Invalid destination call number");
2607 continue;
2608 }
2609
2610 pjsua_msg_data_init(&msg_data);
2611 if (app_config.no_refersub) {
2612 /* Add Refer-Sub: false in outgoing REFER request */
2613 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
2614 &STR_FALSE);
2615 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
2616 }
2617
2618 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
2619 }
2620 break;
2621
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002622 case '#':
2623 /*
2624 * Send DTMF strings.
2625 */
2626 if (current_call == -1) {
2627
2628 PJ_LOG(3,(THIS_FILE, "No current call"));
2629
2630 } else if (!pjsua_call_has_media(current_call)) {
2631
2632 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
2633
2634 } else {
2635 pj_str_t digits;
2636 int call = current_call;
2637 pj_status_t status;
2638
2639 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
2640 sizeof(buf)))
2641 {
2642 break;
2643 }
2644
2645 if (call != current_call) {
2646 puts("Call has been disconnected");
2647 continue;
2648 }
2649
2650 digits = pj_str(buf);
2651 status = pjsua_call_dial_dtmf(current_call, &digits);
2652 if (status != PJ_SUCCESS) {
2653 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
2654 } else {
2655 puts("DTMF digits enqueued for transmission");
2656 }
2657 }
2658 break;
2659
Benny Prijono56315612006-07-18 14:39:40 +00002660 case 'S':
2661 /*
2662 * Send arbitrary request
2663 */
2664 if (pjsua_acc_get_count() == 0) {
2665 puts("Sorry, need at least one account configured");
2666 break;
2667 }
2668
2669 puts("Send arbitrary request to remote host");
2670
2671 /* Input METHOD */
2672 if (!simple_input("Request method:",text,sizeof(text)))
2673 break;
2674
2675 /* Input destination URI */
2676 uri = NULL;
2677 ui_input_url("Destination URI", buf, sizeof(buf), &result);
2678 if (result.nb_result != NO_NB) {
2679
2680 if (result.nb_result == -1 || result.nb_result == 0) {
2681 puts("Sorry you can't do that!");
2682 continue;
2683 } else {
2684 pjsua_buddy_info binfo;
2685 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2686 uri = binfo.uri.ptr;
2687 }
2688
2689 } else if (result.uri_result) {
2690 uri = result.uri_result;
2691 }
2692
2693 tmp = pj_str(uri);
2694
2695 send_request(text, &tmp);
2696 break;
2697
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002698 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00002699 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
2700 pj_str_t tmp;
2701 int delay;
2702
2703 tmp.ptr = menuin+6;
2704 tmp.slen = pj_ansi_strlen(menuin)-7;
2705
2706 if (tmp.slen < 1) {
2707 puts("Usage: sleep MSEC");
2708 break;
2709 }
2710
2711 delay = pj_strtoul(&tmp);
2712 if (delay < 0) delay = 0;
2713 pj_thread_sleep(delay);
2714 break;
2715 }
2716 /* Continue below */
2717
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002718 case 'u':
2719 /*
2720 * Subscribe/unsubscribe presence.
2721 */
2722 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
2723 if (result.nb_result != NO_NB) {
2724 if (result.nb_result == -1) {
2725 int i, count;
2726 count = pjsua_get_buddy_count();
2727 for (i=0; i<count; ++i)
2728 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
2729 } else if (result.nb_result == 0) {
2730 puts("Sorry, can only subscribe to buddy's presence, "
2731 "not from existing call");
2732 } else {
2733 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
2734 }
2735
2736 } else if (result.uri_result) {
2737 puts("Sorry, can only subscribe to buddy's presence, "
2738 "not arbitrary URL (for now)");
2739 }
2740
2741 break;
2742
2743 case 'r':
2744 switch (menuin[1]) {
2745 case 'r':
2746 /*
2747 * Re-Register.
2748 */
2749 pjsua_acc_set_registration(current_acc, PJ_TRUE);
2750 break;
2751 case 'u':
2752 /*
2753 * Unregister
2754 */
2755 pjsua_acc_set_registration(current_acc, PJ_FALSE);
2756 break;
2757 }
2758 break;
2759
2760 case 't':
2761 pjsua_acc_get_info(current_acc, &acc_info);
2762 acc_info.online_status = !acc_info.online_status;
2763 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
2764 printf("Setting %s online status to %s\n",
2765 acc_info.acc_uri.ptr,
2766 (acc_info.online_status?"online":"offline"));
2767 break;
2768
2769 case 'c':
2770 switch (menuin[1]) {
2771 case 'l':
2772 conf_list();
2773 break;
2774 case 'c':
2775 case 'd':
2776 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002777 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002778 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002779 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002780 const char *src_title, *dst_title;
2781
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002782 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002783
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002784 if (cnt != 3) {
2785 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002786
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002787 src_title = (menuin[1]=='c'?
2788 "Connect src port #":
2789 "Disconnect src port #");
2790 dst_title = (menuin[1]=='c'?
2791 "To dst port #":
2792 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002793
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002794 if (!simple_input(src_title, src_port, sizeof(src_port)))
2795 break;
2796
2797 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
2798 break;
2799 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002800
2801 if (menuin[1]=='c') {
2802 status = pjsua_conf_connect(my_atoi(src_port),
2803 my_atoi(dst_port));
2804 } else {
2805 status = pjsua_conf_disconnect(my_atoi(src_port),
2806 my_atoi(dst_port));
2807 }
2808 if (status == PJ_SUCCESS) {
2809 puts("Success");
2810 } else {
2811 puts("ERROR!!");
2812 }
2813 }
2814 break;
2815 }
2816 break;
2817
Benny Prijono6dd967c2006-12-26 02:27:14 +00002818 case 'V':
2819 /* Adjust audio volume */
2820 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
2821 if (simple_input(buf,text,sizeof(text))) {
2822 char *err;
2823 app_config.mic_level = (float)strtod(text, &err);
2824 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
2825 }
2826 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
2827 app_config.speaker_level);
2828 if (simple_input(buf,text,sizeof(text))) {
2829 char *err;
2830 app_config.speaker_level = (float)strtod(text, &err);
2831 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
2832 }
2833
2834 break;
2835
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002836 case 'd':
2837 if (menuin[1] == 'c') {
2838 char settings[2000];
2839 int len;
2840
2841 len = write_settings(&app_config, settings, sizeof(settings));
2842 if (len < 1)
2843 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
2844 else
2845 PJ_LOG(3,(THIS_FILE,
2846 "Dumping configuration (%d bytes):\n%s\n",
2847 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00002848
2849 } else if (menuin[1] == 'q') {
2850
2851 if (current_call != PJSUA_INVALID_ID) {
2852 char buf[1024];
2853 pjsua_call_dump(current_call, PJ_TRUE, buf,
2854 sizeof(buf), " ");
2855 PJ_LOG(3,(THIS_FILE, "\n%s", buf));
2856 } else {
2857 PJ_LOG(3,(THIS_FILE, "No current call"));
2858 }
2859
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002860 } else {
2861 app_dump(menuin[1]=='d');
2862 }
2863 break;
2864
2865
2866 case 'f':
2867 if (simple_input("Enter output filename", buf, sizeof(buf))) {
2868 char settings[2000];
2869 int len;
2870
2871 len = write_settings(&app_config, settings, sizeof(settings));
2872 if (len < 1)
2873 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
2874 else {
2875 pj_oshandle_t fd;
2876 pj_status_t status;
2877
2878 status = pj_file_open(app_config.pool, buf,
2879 PJ_O_WRONLY, &fd);
2880 if (status != PJ_SUCCESS) {
2881 pjsua_perror(THIS_FILE, "Unable to open file", status);
2882 } else {
2883 pj_ssize_t size = len;
2884 pj_file_write(fd, settings, &size);
2885 pj_file_close(fd);
2886
2887 printf("Settings successfully written to '%s'\n", buf);
2888 }
2889 }
2890
2891 }
2892 break;
2893
2894
2895 case 'q':
2896 goto on_exit;
2897
2898
2899 default:
2900 if (menuin[0] != '\n' && menuin[0] != '\r') {
2901 printf("Invalid input %s", menuin);
2902 }
2903 keystroke_help();
2904 break;
2905 }
2906 }
2907
2908on_exit:
2909 ;
2910}
2911
2912
2913/*****************************************************************************
2914 * Public API
2915 */
2916
2917pj_status_t app_init(int argc, char *argv[])
2918{
Benny Prijonoe93e2872006-06-28 16:46:49 +00002919 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00002920 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002921 unsigned i;
2922 pj_status_t status;
2923
2924 /* Create pjsua */
2925 status = pjsua_create();
2926 if (status != PJ_SUCCESS)
2927 return status;
2928
2929 /* Create pool for application */
2930 app_config.pool = pjsua_pool_create("pjsua", 4000, 4000);
2931
2932 /* Initialize default config */
2933 default_config(&app_config);
2934
2935 /* Parse the arguments */
2936 status = parse_args(argc, argv, &app_config, &uri_arg);
2937 if (status != PJ_SUCCESS)
2938 return status;
2939
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002940 /* Initialize application callbacks */
2941 app_config.cfg.cb.on_call_state = &on_call_state;
2942 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
2943 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijono0875ae82006-12-26 00:11:48 +00002944 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002945 app_config.cfg.cb.on_reg_state = &on_reg_state;
2946 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
2947 app_config.cfg.cb.on_pager = &on_pager;
2948 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002949 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00002950 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002951
2952 /* Initialize pjsua */
2953 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
2954 &app_config.media_cfg);
2955 if (status != PJ_SUCCESS)
2956 return status;
2957
Benny Prijonoe909eac2006-07-27 22:04:56 +00002958#ifdef STEREO_DEMO
2959 stereo_demo();
2960#endif
2961
Benny Prijono804ff0a2006-09-14 11:17:48 +00002962 /* Initialize calls data */
2963 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
2964 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
2965 app_config.call_data[i].timer.cb = &call_timeout_callback;
2966 }
2967
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002968 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00002969 for (i=0; i<app_config.wav_count; ++i) {
2970 pjsua_player_id wav_id;
2971
2972 status = pjsua_player_create(&app_config.wav_files[i], 0,
2973 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002974 if (status != PJ_SUCCESS)
2975 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00002976
Benny Prijono72c04d22007-02-17 22:20:58 +00002977 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00002978 app_config.wav_id = wav_id;
2979 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
2980 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002981 }
2982
Benny Prijono4af234b2007-01-24 02:02:09 +00002983 /* Optionally registers tone players */
2984 for (i=0; i<app_config.tone_count; ++i) {
2985 pjmedia_port *tport;
2986 char name[80];
2987 pj_str_t label;
2988 pj_status_t status;
2989
2990 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
2991 app_config.tones[i].freq1,
2992 app_config.tones[i].freq2);
2993 label = pj_str(name);
2994 status = pjmedia_tonegen_create2(app_config.pool, &label,
2995 8000, 1, 160, 16,
2996 PJMEDIA_TONEGEN_LOOP, &tport);
2997 if (status != PJ_SUCCESS) {
2998 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
2999 goto on_error;
3000 }
3001
3002 status = pjsua_conf_add_port(app_config.pool, tport,
3003 &app_config.tone_slots[i]);
3004 pj_assert(status == PJ_SUCCESS);
3005
3006 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3007 pj_assert(status == PJ_SUCCESS);
3008 }
3009
Benny Prijono1ebd6142006-10-19 15:48:02 +00003010 /* Optionally create recorder file, if any. */
3011 if (app_config.rec_file.slen) {
3012 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3013 &app_config.rec_id);
3014 if (status != PJ_SUCCESS)
3015 goto on_error;
3016
3017 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3018 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003019
Benny Prijonoe347cb02007-02-14 14:36:13 +00003020 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3021
Benny Prijono87ef89a2007-01-14 00:39:45 +00003022 /* Add UDP transport unless it's disabled. */
3023 if (!app_config.no_udp) {
3024 pjsua_acc_id aid;
3025
3026 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
3027 &app_config.udp_cfg,
3028 &transport_id);
3029 if (status != PJ_SUCCESS)
3030 goto on_error;
3031
3032 /* Add local account */
3033 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
3034 //pjsua_acc_set_transport(aid, transport_id);
3035 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00003036
3037 if (app_config.udp_cfg.port == 0) {
3038 pjsua_transport_info ti;
3039 pj_sockaddr_in *a;
3040
3041 pjsua_transport_get_info(transport_id, &ti);
3042 a = (pj_sockaddr_in*)&ti.local_addr;
3043
3044 tcp_cfg.port = pj_ntohs(a->sin_port);
3045 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00003046 }
3047
Benny Prijonoe93e2872006-06-28 16:46:49 +00003048 /* Add TCP transport unless it's disabled */
3049 if (!app_config.no_tcp) {
3050 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00003051 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00003052 &transport_id);
3053 if (status != PJ_SUCCESS)
3054 goto on_error;
3055
3056 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00003057 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00003058 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3059
3060 }
3061
Benny Prijonoe93e2872006-06-28 16:46:49 +00003062
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003063#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
3064 /* Add TLS transport when application wants one */
3065 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00003066
3067 pjsua_acc_id acc_id;
3068
3069 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00003070 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003071 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00003072 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003073 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00003074 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003075 if (status != PJ_SUCCESS)
3076 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00003077
3078 /* Add local account */
3079 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
3080 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003081 }
3082#endif
3083
Benny Prijonoe93e2872006-06-28 16:46:49 +00003084 if (transport_id == -1) {
3085 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
3086 status = -1;
3087 goto on_error;
3088 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003089
3090
3091 /* Add accounts */
3092 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003093 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003094 if (status != PJ_SUCCESS)
3095 goto on_error;
3096 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3097 }
3098
3099 /* Add buddies */
3100 for (i=0; i<app_config.buddy_cnt; ++i) {
3101 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
3102 if (status != PJ_SUCCESS)
3103 goto on_error;
3104 }
3105
3106 /* Optionally set codec orders */
3107 for (i=0; i<app_config.codec_cnt; ++i) {
3108 pjsua_codec_set_priority(&app_config.codec_arg[i],
3109 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
3110 }
3111
3112 /* Add RTP transports */
3113 status = pjsua_media_transports_create(&app_config.rtp_cfg);
3114 if (status != PJ_SUCCESS)
3115 goto on_error;
3116
Benny Prijono22a300a2006-06-14 20:04:55 +00003117 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00003118#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00003119 if (app_config.null_audio) {
3120 status = pjsua_set_null_snd_dev();
3121 if (status != PJ_SUCCESS)
3122 return status;
3123 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00003124#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00003125
Benny Prijono4e5d5512007-03-06 18:11:30 +00003126 if (app_config.capture_dev != PJSUA_INVALID_ID
3127 || app_config.playback_dev != PJSUA_INVALID_ID) {
3128 status
3129 = pjsua_set_snd_dev(app_config.capture_dev, app_config.playback_dev);
3130 if (status != PJ_SUCCESS)
3131 goto on_error;
3132 }
3133
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003134 return PJ_SUCCESS;
3135
3136on_error:
3137 pjsua_destroy();
3138 return status;
3139}
3140
3141
3142pj_status_t app_main(void)
3143{
3144 pj_status_t status;
3145
3146 /* Start pjsua */
3147 status = pjsua_start();
3148 if (status != PJ_SUCCESS) {
3149 pjsua_destroy();
3150 return status;
3151 }
3152
3153 console_app_main(&uri_arg);
3154
3155 return PJ_SUCCESS;
3156}
3157
3158pj_status_t app_destroy(void)
3159{
Benny Prijonof762ee72006-12-01 11:14:37 +00003160 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00003161 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00003162
Benny Prijonoe909eac2006-07-27 22:04:56 +00003163#ifdef STEREO_DEMO
3164 if (app_config.snd) {
3165 pjmedia_snd_port_destroy(app_config.snd);
3166 app_config.snd = NULL;
3167 }
3168#endif
3169
Benny Prijono4af234b2007-01-24 02:02:09 +00003170 /* Close tone generators */
3171 for (i=0; i<app_config.tone_count; ++i) {
3172 pjsua_conf_remove_port(app_config.tone_slots[i]);
3173 }
3174
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003175 if (app_config.pool) {
3176 pj_pool_release(app_config.pool);
3177 app_config.pool = NULL;
3178 }
3179
Benny Prijonof762ee72006-12-01 11:14:37 +00003180 status = pjsua_destroy();
3181
3182 pj_bzero(&app_config, sizeof(app_config));
3183
3184 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003185}
Benny Prijonoe909eac2006-07-27 22:04:56 +00003186
3187
3188#ifdef STEREO_DEMO
3189static void stereo_demo()
3190{
3191 pjmedia_port *conf, *splitter, *ch1;
3192 unsigned clock;
3193 pj_status_t status;
3194
3195 /* Disable existing sound device */
3196 conf = pjsua_set_no_snd_dev();
3197
3198 clock = app_config.media_cfg.clock_rate;
3199
3200 /* Create stereo-mono splitter/combiner */
3201 status = pjmedia_splitcomb_create(app_config.pool,
3202 clock /* clock rate */,
3203 2 /* stereo */,
3204 clock*2*10/1000/* 10ms samples * 2ch */,
3205 16 /* bits */,
3206 0 /* options */,
3207 &splitter);
3208 pj_assert(status == PJ_SUCCESS);
3209
3210 /* Connect channel0 (left channel?) to conference port slot0 */
3211 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
3212 0 /*options*/,
3213 conf);
3214 pj_assert(status == PJ_SUCCESS);
3215
3216 /* Create reverse channel for channel1 (right channel?)... */
3217 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
3218 splitter,
3219 1 /* ch1 */,
3220 0 /* options */,
3221 &ch1);
3222 pj_assert(status == PJ_SUCCESS);
3223
3224 /* .. and register it to conference bridge (it would be slot1
3225 * if there's no other devices connected to the bridge)
3226 */
3227 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
3228 pj_assert(status == PJ_SUCCESS);
3229
3230 /* Create sound device */
3231 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
3232 clock /* clock rate */,
3233 2 /* stereo */,
3234 clock*2*10/1000 /* 10 ms samples * 2ch */,
3235 16 /* bits */,
3236 0, &app_config.snd);
3237 pj_assert(status == PJ_SUCCESS);
3238
3239
3240 /* Connect the splitter to the sound device */
3241 status = pjmedia_snd_port_connect(app_config.snd, splitter);
3242 pj_assert(status == PJ_SUCCESS);
3243
3244}
3245#endif
3246