blob: 6fd74c6237966b04e6b81a0a9f75365d75ca4539 [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 auto_answer;
76 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +000077
78#ifdef STEREO_DEMO
79 pjmedia_snd_port *snd;
80#endif
81
Benny Prijono6dd967c2006-12-26 02:27:14 +000082 float mic_level,
83 speaker_level;
84
Benny Prijono4e5d5512007-03-06 18:11:30 +000085 int capture_dev, playback_dev;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000086} app_config;
87
88
Benny Prijono21b9ad92006-08-15 13:11:22 +000089//static pjsua_acc_id current_acc;
90#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +000091static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000092static pj_str_t uri_arg;
93
Benny Prijono594e4c52006-09-14 18:51:01 +000094#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +000095static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +000096#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +000097pj_status_t app_destroy(void);
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;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000323 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000324 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 Prijonoaf09dc32007-04-22 12:48:30 +0000882 argv[pj_optind-1]));
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 */
Benny Prijono2adfe292007-05-11 10:36:08 +00001211 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001212 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001213 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001214 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{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001320 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001321}
1322
1323
1324/*****************************************************************************
1325 * Console application
1326 */
1327
1328/*
1329 * Find next call when current call is disconnected or when user
1330 * press ']'
1331 */
1332static pj_bool_t find_next_call(void)
1333{
1334 int i, max;
1335
1336 max = pjsua_call_get_max_count();
1337 for (i=current_call+1; i<max; ++i) {
1338 if (pjsua_call_is_active(i)) {
1339 current_call = i;
1340 return PJ_TRUE;
1341 }
1342 }
1343
1344 for (i=0; i<current_call; ++i) {
1345 if (pjsua_call_is_active(i)) {
1346 current_call = i;
1347 return PJ_TRUE;
1348 }
1349 }
1350
1351 current_call = PJSUA_INVALID_ID;
1352 return PJ_FALSE;
1353}
1354
1355
1356/*
1357 * Find previous call when user press '['
1358 */
1359static pj_bool_t find_prev_call(void)
1360{
1361 int i, max;
1362
1363 max = pjsua_call_get_max_count();
1364 for (i=current_call-1; i>=0; --i) {
1365 if (pjsua_call_is_active(i)) {
1366 current_call = i;
1367 return PJ_TRUE;
1368 }
1369 }
1370
1371 for (i=max-1; i>current_call; --i) {
1372 if (pjsua_call_is_active(i)) {
1373 current_call = i;
1374 return PJ_TRUE;
1375 }
1376 }
1377
1378 current_call = PJSUA_INVALID_ID;
1379 return PJ_FALSE;
1380}
1381
1382
Benny Prijono804ff0a2006-09-14 11:17:48 +00001383/* Callback from timer when the maximum call duration has been
1384 * exceeded.
1385 */
1386static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1387 struct pj_timer_entry *entry)
1388{
1389 pjsua_call_id call_id = entry->id;
1390 pjsua_msg_data msg_data;
1391 pjsip_generic_string_hdr warn;
1392 pj_str_t hname = pj_str("Warning");
1393 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1394
1395 PJ_UNUSED_ARG(timer_heap);
1396
Benny Prijono148c9dd2006-09-19 13:37:53 +00001397 if (call_id == PJSUA_INVALID_ID) {
1398 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1399 return;
1400 }
1401
Benny Prijono804ff0a2006-09-14 11:17:48 +00001402 /* Add warning header */
1403 pjsua_msg_data_init(&msg_data);
1404 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1405 pj_list_push_back(&msg_data.hdr_list, &warn);
1406
1407 /* Call duration has been exceeded; disconnect the call */
1408 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1409 "for call %d, disconnecting the call",
1410 app_config.duration, call_id));
1411 entry->id = PJSUA_INVALID_ID;
1412 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1413}
1414
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001415
1416/*
1417 * Handler when invite state has changed.
1418 */
1419static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1420{
1421 pjsua_call_info call_info;
1422
1423 PJ_UNUSED_ARG(e);
1424
1425 pjsua_call_get_info(call_id, &call_info);
1426
1427 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1428
Benny Prijono804ff0a2006-09-14 11:17:48 +00001429 /* Cancel duration timer, if any */
1430 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1431 struct call_data *cd = &app_config.call_data[call_id];
1432 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1433
1434 cd->timer.id = PJSUA_INVALID_ID;
1435 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1436 }
1437
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001438 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1439 call_id,
1440 call_info.last_status,
1441 call_info.last_status_text.ptr));
1442
1443 if (call_id == current_call) {
1444 find_next_call();
1445 }
1446
Benny Prijono4be63b52006-11-25 14:50:25 +00001447 /* Dump media state upon disconnected */
1448 if (1) {
1449 char buf[1024];
1450 pjsua_call_dump(call_id, PJ_TRUE, buf,
1451 sizeof(buf), " ");
1452 PJ_LOG(5,(THIS_FILE,
1453 "Call %d disconnected, dumping media stats\n%s",
1454 call_id, buf));
1455 }
1456
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001457 } else {
1458
Benny Prijono804ff0a2006-09-14 11:17:48 +00001459 if (app_config.duration!=NO_LIMIT &&
1460 call_info.state == PJSIP_INV_STATE_CONFIRMED)
1461 {
1462 /* Schedule timer to hangup call after the specified duration */
1463 struct call_data *cd = &app_config.call_data[call_id];
1464 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1465 pj_time_val delay;
1466
1467 cd->timer.id = call_id;
1468 delay.sec = app_config.duration;
1469 delay.msec = 0;
1470 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
1471 }
1472
Benny Prijono4be63b52006-11-25 14:50:25 +00001473 if (call_info.state == PJSIP_INV_STATE_EARLY) {
1474 int code;
1475 pj_str_t reason;
1476 pjsip_msg *msg;
1477
1478 /* This can only occur because of TX or RX message */
1479 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
1480
1481 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1482 msg = e->body.tsx_state.src.rdata->msg_info.msg;
1483 } else {
1484 msg = e->body.tsx_state.src.tdata->msg;
1485 }
1486
1487 code = msg->line.status.code;
1488 reason = msg->line.status.reason;
1489
1490 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
1491 call_id, call_info.state_text.ptr,
1492 code, (int)reason.slen, reason.ptr));
1493 } else {
1494 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
1495 call_id,
1496 call_info.state_text.ptr));
1497 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001498
1499 if (current_call==PJSUA_INVALID_ID)
1500 current_call = call_id;
1501
1502 }
1503}
1504
1505
1506/**
1507 * Handler when there is incoming call.
1508 */
1509static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
1510 pjsip_rx_data *rdata)
1511{
1512 pjsua_call_info call_info;
1513
1514 PJ_UNUSED_ARG(acc_id);
1515 PJ_UNUSED_ARG(rdata);
1516
1517 pjsua_call_get_info(call_id, &call_info);
1518
1519 if (app_config.auto_answer > 0) {
1520 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
1521 }
1522
1523 if (app_config.auto_answer < 200) {
1524 PJ_LOG(3,(THIS_FILE,
1525 "Incoming call for account %d!\n"
1526 "From: %s\n"
1527 "To: %s\n"
1528 "Press a to answer or h to reject call",
1529 acc_id,
1530 call_info.remote_info.ptr,
1531 call_info.local_info.ptr));
1532 }
1533}
1534
1535
1536/*
1537 * Callback on media state changed event.
1538 * The action may connect the call to sound device, to file, or
1539 * to loop the call.
1540 */
1541static void on_call_media_state(pjsua_call_id call_id)
1542{
1543 pjsua_call_info call_info;
1544
1545 pjsua_call_get_info(call_id, &call_info);
1546
1547 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
1548 pj_bool_t connect_sound = PJ_TRUE;
1549
1550 /* Loopback sound, if desired */
1551 if (app_config.auto_loop) {
1552 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
1553 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00001554
1555 /* Automatically record conversation, if desired */
1556 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1557 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1558 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001559 }
1560
1561 /* Stream a file, if desired */
1562 if (app_config.auto_play && app_config.wav_port != PJSUA_INVALID_ID) {
1563 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
1564 connect_sound = PJ_FALSE;
1565 }
1566
Benny Prijono7ca96da2006-08-07 12:11:40 +00001567 /* Put call in conference with other calls, if desired */
1568 if (app_config.auto_conf) {
1569 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00001570 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00001571 unsigned i;
1572
1573 /* Get all calls, and establish media connection between
1574 * this call and other calls.
1575 */
1576 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00001577
Benny Prijono7ca96da2006-08-07 12:11:40 +00001578 for (i=0; i<call_cnt; ++i) {
1579 if (call_ids[i] == call_id)
1580 continue;
1581
1582 if (!pjsua_call_has_media(call_ids[i]))
1583 continue;
1584
1585 pjsua_conf_connect(call_info.conf_slot,
1586 pjsua_call_get_conf_port(call_ids[i]));
1587 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1588 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001589
1590 /* Automatically record conversation, if desired */
1591 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1592 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1593 app_config.rec_port);
1594 }
1595
Benny Prijono7ca96da2006-08-07 12:11:40 +00001596 }
1597
1598 /* Also connect call to local sound device */
1599 connect_sound = PJ_TRUE;
1600 }
1601
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001602 /* Otherwise connect to sound device */
1603 if (connect_sound) {
1604 pjsua_conf_connect(call_info.conf_slot, 0);
1605 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001606
1607 /* Automatically record conversation, if desired */
1608 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1609 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1610 pjsua_conf_connect(0, app_config.rec_port);
1611 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001612 }
1613
1614 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
1615
1616 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
1617 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
1618 call_id));
1619 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
1620 PJ_LOG(3,(THIS_FILE,
1621 "Media for call %d is suspended (hold) by remote",
1622 call_id));
1623 } else {
1624 PJ_LOG(3,(THIS_FILE,
1625 "Media for call %d is inactive",
1626 call_id));
1627 }
1628}
1629
Benny Prijono0875ae82006-12-26 00:11:48 +00001630/*
1631 * DTMF callback.
1632 */
1633static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
1634{
1635 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
1636}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001637
1638/*
1639 * Handler registration status has changed.
1640 */
1641static void on_reg_state(pjsua_acc_id acc_id)
1642{
1643 PJ_UNUSED_ARG(acc_id);
1644
1645 // Log already written.
1646}
1647
1648
1649/*
1650 * Handler on buddy state changed.
1651 */
1652static void on_buddy_state(pjsua_buddy_id buddy_id)
1653{
1654 pjsua_buddy_info info;
1655 pjsua_buddy_get_info(buddy_id, &info);
1656
1657 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
1658 (int)info.uri.slen,
1659 info.uri.ptr,
1660 (int)info.status_text.slen,
1661 info.status_text.ptr));
1662}
1663
1664
1665/**
1666 * Incoming IM message (i.e. MESSAGE request)!
1667 */
1668static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
1669 const pj_str_t *to, const pj_str_t *contact,
1670 const pj_str_t *mime_type, const pj_str_t *text)
1671{
1672 /* Note: call index may be -1 */
1673 PJ_UNUSED_ARG(call_id);
1674 PJ_UNUSED_ARG(to);
1675 PJ_UNUSED_ARG(contact);
1676 PJ_UNUSED_ARG(mime_type);
1677
1678 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s",
1679 (int)from->slen, from->ptr,
1680 (int)text->slen, text->ptr));
1681}
1682
1683
1684/**
1685 * Received typing indication
1686 */
1687static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
1688 const pj_str_t *to, const pj_str_t *contact,
1689 pj_bool_t is_typing)
1690{
1691 PJ_UNUSED_ARG(call_id);
1692 PJ_UNUSED_ARG(to);
1693 PJ_UNUSED_ARG(contact);
1694
1695 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
1696 (int)from->slen, from->ptr,
1697 (is_typing?"is typing..":"has stopped typing")));
1698}
1699
1700
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001701/**
1702 * Call transfer request status.
1703 */
1704static void on_call_transfer_status(pjsua_call_id call_id,
1705 int status_code,
1706 const pj_str_t *status_text,
1707 pj_bool_t final,
1708 pj_bool_t *p_cont)
1709{
1710 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
1711 call_id, status_code,
1712 (int)status_text->slen, status_text->ptr,
1713 (final ? "[final]" : "")));
1714
1715 if (status_code/100 == 2) {
1716 PJ_LOG(3,(THIS_FILE,
1717 "Call %d: call transfered successfully, disconnecting call",
1718 call_id));
1719 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
1720 *p_cont = PJ_FALSE;
1721 }
1722}
1723
1724
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001725/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00001726 * Notification that call is being replaced.
1727 */
1728static void on_call_replaced(pjsua_call_id old_call_id,
1729 pjsua_call_id new_call_id)
1730{
1731 pjsua_call_info old_ci, new_ci;
1732
1733 pjsua_call_get_info(old_call_id, &old_ci);
1734 pjsua_call_get_info(new_call_id, &new_ci);
1735
1736 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
1737 "call %d with %.*s",
1738 old_call_id,
1739 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
1740 new_call_id,
1741 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
1742}
1743
1744
1745/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001746 * Print buddy list.
1747 */
1748static void print_buddy_list(void)
1749{
1750 pjsua_buddy_id ids[64];
1751 int i;
1752 unsigned count = PJ_ARRAY_SIZE(ids);
1753
1754 puts("Buddy list:");
1755
1756 pjsua_enum_buddies(ids, &count);
1757
1758 if (count == 0)
1759 puts(" -none-");
1760 else {
1761 for (i=0; i<(int)count; ++i) {
1762 pjsua_buddy_info info;
1763
1764 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
1765 continue;
1766
1767 printf(" [%2d] <%7s> %.*s\n",
1768 ids[i]+1, info.status_text.ptr,
1769 (int)info.uri.slen,
1770 info.uri.ptr);
1771 }
1772 }
1773 puts("");
1774}
1775
1776
1777/*
1778 * Print account status.
1779 */
1780static void print_acc_status(int acc_id)
1781{
1782 char buf[80];
1783 pjsua_acc_info info;
1784
1785 pjsua_acc_get_info(acc_id, &info);
1786
1787 if (!info.has_registration) {
1788 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
1789 (int)info.status_text.slen,
1790 info.status_text.ptr);
1791
1792 } else {
1793 pj_ansi_snprintf(buf, sizeof(buf),
1794 "%d/%.*s (expires=%d)",
1795 info.status,
1796 (int)info.status_text.slen,
1797 info.status_text.ptr,
1798 info.expires);
1799
1800 }
1801
1802 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
1803 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
1804 printf(" Online status: %s\n",
1805 (info.online_status ? "Online" : "Invisible"));
1806}
1807
1808
1809/*
1810 * Show a bit of help.
1811 */
1812static void keystroke_help(void)
1813{
1814 pjsua_acc_id acc_ids[16];
1815 unsigned count = PJ_ARRAY_SIZE(acc_ids);
1816 int i;
1817
1818 printf(">>>>\n");
1819
1820 pjsua_enum_accs(acc_ids, &count);
1821
1822 printf("Account list:\n");
1823 for (i=0; i<(int)count; ++i)
1824 print_acc_status(acc_ids[i]);
1825
1826 print_buddy_list();
1827
1828 //puts("Commands:");
1829 puts("+=============================================================================+");
1830 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
1831 puts("| | | |");
1832 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
1833 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
1834 puts("| a Answer call | !b Modify buddy | !a Modify accnt. |");
1835 puts("| h Hangup call (ha=all) | i Send IM | rr (Re-)register |");
1836 puts("| H Hold call | s Subscribe presence | ru Unregister |");
1837 puts("| v re-inVite (release hold) | u Unsubscribe presence | > Cycle next ac.|");
1838 puts("| ] Select next dialog | t ToGgle Online status | < Cycle prev ac.|");
1839 puts("| [ Select previous dialog +--------------------------+-------------------+");
1840 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00001841 puts("| X Xfer with Replaces | | |");
1842 puts("| # Send DTMF string | cl List ports | d Dump status |");
1843 puts("| dq Dump curr. call quality | cc Connect port | dd Dump detailed |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001844 puts("| | cd Disconnect port | dc Dump config |");
Benny Prijono6dd967c2006-12-26 02:27:14 +00001845 puts("| S Send arbitrary REQUEST | V Adjust audio Volume | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001846 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijono990042e2007-01-21 19:36:00 +00001847 puts("| q QUIT sleep N: console sleep for N ms |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001848 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00001849
1850 i = pjsua_call_get_count();
1851 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00001852
1853 if (current_call != PJSUA_INVALID_ID) {
1854 pjsua_call_info ci;
1855 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
1856 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
1857 (int)ci.remote_info.slen, ci.remote_info.ptr,
1858 (int)ci.state_text.slen, ci.state_text.ptr);
1859 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001860}
1861
1862
1863/*
1864 * Input simple string
1865 */
1866static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
1867{
1868 char *p;
1869
1870 printf("%s (empty to cancel): ", title); fflush(stdout);
1871 fgets(buf, len, stdin);
1872
1873 /* Remove trailing newlines. */
1874 for (p=buf; ; ++p) {
1875 if (*p=='\r' || *p=='\n') *p='\0';
1876 else if (!*p) break;
1877 }
1878
1879 if (!*buf)
1880 return PJ_FALSE;
1881
1882 return PJ_TRUE;
1883}
1884
1885
1886#define NO_NB -2
1887struct input_result
1888{
1889 int nb_result;
1890 char *uri_result;
1891};
1892
1893
1894/*
1895 * Input URL.
1896 */
1897static void ui_input_url(const char *title, char *buf, int len,
1898 struct input_result *result)
1899{
1900 result->nb_result = NO_NB;
1901 result->uri_result = NULL;
1902
1903 print_buddy_list();
1904
1905 printf("Choices:\n"
1906 " 0 For current dialog.\n"
1907 " -1 All %d buddies in buddy list\n"
1908 " [1 -%2d] Select from buddy list\n"
1909 " URL An URL\n"
1910 " <Enter> Empty input (or 'q') to cancel\n"
1911 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
1912 printf("%s: ", title);
1913
1914 fflush(stdout);
1915 fgets(buf, len, stdin);
1916 len = strlen(buf);
1917
1918 /* Left trim */
1919 while (pj_isspace(*buf)) {
1920 ++buf;
1921 --len;
1922 }
1923
1924 /* Remove trailing newlines */
1925 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
1926 buf[--len] = '\0';
1927
1928 if (len == 0 || buf[0]=='q')
1929 return;
1930
1931 if (pj_isdigit(*buf) || *buf=='-') {
1932
1933 int i;
1934
1935 if (*buf=='-')
1936 i = 1;
1937 else
1938 i = 0;
1939
1940 for (; i<len; ++i) {
1941 if (!pj_isdigit(buf[i])) {
1942 puts("Invalid input");
1943 return;
1944 }
1945 }
1946
1947 result->nb_result = my_atoi(buf);
1948
1949 if (result->nb_result >= 0 &&
1950 result->nb_result <= (int)pjsua_get_buddy_count())
1951 {
1952 return;
1953 }
1954 if (result->nb_result == -1)
1955 return;
1956
1957 puts("Invalid input");
1958 result->nb_result = NO_NB;
1959 return;
1960
1961 } else {
1962 pj_status_t status;
1963
1964 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
1965 pjsua_perror(THIS_FILE, "Invalid URL", status);
1966 return;
1967 }
1968
1969 result->uri_result = buf;
1970 }
1971}
1972
1973/*
1974 * List the ports in conference bridge
1975 */
1976static void conf_list(void)
1977{
1978 unsigned i, count;
1979 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
1980
1981 printf("Conference ports:\n");
1982
1983 count = PJ_ARRAY_SIZE(id);
1984 pjsua_enum_conf_ports(id, &count);
1985
1986 for (i=0; i<count; ++i) {
1987 char txlist[PJSUA_MAX_CALLS*4+10];
1988 unsigned j;
1989 pjsua_conf_port_info info;
1990
1991 pjsua_conf_get_port_info(id[i], &info);
1992
1993 txlist[0] = '\0';
1994 for (j=0; j<info.listener_cnt; ++j) {
1995 char s[10];
1996 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
1997 pj_ansi_strcat(txlist, s);
1998 }
1999 printf("Port #%02d[%2dKHz/%dms] %20.*s transmitting to: %s\n",
2000 info.slot_id,
2001 info.clock_rate/1000,
2002 info.samples_per_frame * 1000 / info.clock_rate,
2003 (int)info.name.slen,
2004 info.name.ptr,
2005 txlist);
2006
2007 }
2008 puts("");
2009}
2010
2011
2012/*
Benny Prijono56315612006-07-18 14:39:40 +00002013 * Send arbitrary request to remote host
2014 */
2015static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2016{
2017 pj_str_t str_method;
2018 pjsip_method method;
2019 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002020 pjsip_endpoint *endpt;
2021 pj_status_t status;
2022
2023 endpt = pjsua_get_pjsip_endpt();
2024
2025 str_method = pj_str(cstr_method);
2026 pjsip_method_init_np(&method, &str_method);
2027
Benny Prijonofff245c2007-04-02 11:44:47 +00002028 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002029
2030 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
2031 if (status != PJ_SUCCESS) {
2032 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002033 return;
2034 }
2035}
2036
2037
2038/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002039 * Main "user interface" loop.
2040 */
2041void console_app_main(const pj_str_t *uri_to_call)
2042{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002043 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002044 char buf[128];
2045 char text[128];
2046 int i, count;
2047 char *uri;
2048 pj_str_t tmp;
2049 struct input_result result;
2050 pjsua_call_info call_info;
2051 pjsua_acc_info acc_info;
2052
2053
2054 /* If user specifies URI to call, then call the URI */
2055 if (uri_to_call->slen) {
2056 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2057 }
2058
2059 keystroke_help();
2060
2061 for (;;) {
2062
2063 printf(">>> ");
2064 fflush(stdout);
2065
Benny Prijono990042e2007-01-21 19:36:00 +00002066 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2067 /*
2068 * Be friendly to users who redirect commands into
2069 * program, when file ends, resume with kbd.
2070 * If exit is desired end script with q for quit
2071 */
2072 /* Reopen stdin/stdout/stderr to /dev/console */
2073#if defined(PJ_WIN32) && PJ_WIN32!=0
2074 if (freopen ("CONIN$", "r", stdin) == NULL) {
2075#else
2076 if (1) {
2077#endif
2078 puts("Cannot switch back to console from file redirection");
2079 menuin[0] = 'q';
2080 menuin[1] = '\0';
2081 } else {
2082 puts("Switched back to console from file redirection");
2083 continue;
2084 }
2085 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002086
2087 switch (menuin[0]) {
2088
2089 case 'm':
2090 /* Make call! : */
2091 printf("(You currently have %d calls)\n",
2092 pjsua_call_get_count());
2093
2094 uri = NULL;
2095 ui_input_url("Make call", buf, sizeof(buf), &result);
2096 if (result.nb_result != NO_NB) {
2097
2098 if (result.nb_result == -1 || result.nb_result == 0) {
2099 puts("You can't do that with make call!");
2100 continue;
2101 } else {
2102 pjsua_buddy_info binfo;
2103 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2104 uri = binfo.uri.ptr;
2105 }
2106
2107 } else if (result.uri_result) {
2108 uri = result.uri_result;
2109 }
2110
2111 tmp = pj_str(uri);
2112 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2113 break;
2114
2115 case 'M':
2116 /* Make multiple calls! : */
2117 printf("(You currently have %d calls)\n",
2118 pjsua_call_get_count());
2119
2120 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2121 continue;
2122
2123 count = my_atoi(menuin);
2124 if (count < 1)
2125 continue;
2126
2127 ui_input_url("Make call", buf, sizeof(buf), &result);
2128 if (result.nb_result != NO_NB) {
2129 pjsua_buddy_info binfo;
2130 if (result.nb_result == -1 || result.nb_result == 0) {
2131 puts("You can't do that with make call!");
2132 continue;
2133 }
2134 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2135 uri = binfo.uri.ptr;
2136 } else {
2137 uri = result.uri_result;
2138 }
2139
2140 for (i=0; i<my_atoi(menuin); ++i) {
2141 pj_status_t status;
2142
2143 tmp = pj_str(uri);
2144 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2145 NULL, NULL);
2146 if (status != PJ_SUCCESS)
2147 break;
2148 }
2149 break;
2150
2151 case 'i':
2152 /* Send instant messaeg */
2153
2154 /* i is for call index to send message, if any */
2155 i = -1;
2156
2157 /* Make compiler happy. */
2158 uri = NULL;
2159
2160 /* Input destination. */
2161 ui_input_url("Send IM to", buf, sizeof(buf), &result);
2162 if (result.nb_result != NO_NB) {
2163
2164 if (result.nb_result == -1) {
2165 puts("You can't send broadcast IM like that!");
2166 continue;
2167
2168 } else if (result.nb_result == 0) {
2169
2170 i = current_call;
2171
2172 } else {
2173 pjsua_buddy_info binfo;
2174 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2175 uri = binfo.uri.ptr;
2176 }
2177
2178 } else if (result.uri_result) {
2179 uri = result.uri_result;
2180 }
2181
2182
2183 /* Send typing indication. */
2184 if (i != -1)
2185 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
2186 else {
2187 pj_str_t tmp_uri = pj_str(uri);
2188 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
2189 }
2190
2191 /* Input the IM . */
2192 if (!simple_input("Message", text, sizeof(text))) {
2193 /*
2194 * Cancelled.
2195 * Send typing notification too, saying we're not typing.
2196 */
2197 if (i != -1)
2198 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
2199 else {
2200 pj_str_t tmp_uri = pj_str(uri);
2201 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
2202 }
2203 continue;
2204 }
2205
2206 tmp = pj_str(text);
2207
2208 /* Send the IM */
2209 if (i != -1)
2210 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
2211 else {
2212 pj_str_t tmp_uri = pj_str(uri);
2213 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
2214 }
2215
2216 break;
2217
2218 case 'a':
2219
2220 if (current_call != -1) {
2221 pjsua_call_get_info(current_call, &call_info);
2222 } else {
2223 /* Make compiler happy */
2224 call_info.role = PJSIP_ROLE_UAC;
2225 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
2226 }
2227
2228 if (current_call == -1 ||
2229 call_info.role != PJSIP_ROLE_UAS ||
2230 call_info.state >= PJSIP_INV_STATE_CONNECTING)
2231 {
2232 puts("No pending incoming call");
2233 fflush(stdout);
2234 continue;
2235
2236 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00002237 int st_code;
2238 char contact[120];
2239 pj_str_t hname = { "Contact", 7 };
2240 pj_str_t hvalue;
2241 pjsip_generic_string_hdr hcontact;
2242 pjsua_msg_data msg_data;
2243
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002244 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
2245 continue;
2246
Benny Prijono20d36722007-02-22 14:52:24 +00002247 st_code = my_atoi(buf);
2248 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002249 continue;
2250
Benny Prijono20d36722007-02-22 14:52:24 +00002251 pjsua_msg_data_init(&msg_data);
2252
2253 if (st_code/100 == 3) {
2254 if (!simple_input("Enter URL to be put in Contact",
2255 contact, sizeof(contact)))
2256 continue;
2257 hvalue = pj_str(contact);
2258 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
2259
2260 pj_list_push_back(&msg_data.hdr_list, &hcontact);
2261 }
2262
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002263 /*
2264 * Must check again!
2265 * Call may have been disconnected while we're waiting for
2266 * keyboard input.
2267 */
2268 if (current_call == -1) {
2269 puts("Call has been disconnected");
2270 fflush(stdout);
2271 continue;
2272 }
2273
Benny Prijono20d36722007-02-22 14:52:24 +00002274 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002275 }
2276
2277 break;
2278
2279
2280 case 'h':
2281
2282 if (current_call == -1) {
2283 puts("No current call");
2284 fflush(stdout);
2285 continue;
2286
2287 } else if (menuin[1] == 'a') {
2288
2289 /* Hangup all calls */
2290 pjsua_call_hangup_all();
2291
2292 } else {
2293
2294 /* Hangup current calls */
2295 pjsua_call_hangup(current_call, 0, NULL, NULL);
2296 }
2297 break;
2298
2299 case ']':
2300 case '[':
2301 /*
2302 * Cycle next/prev dialog.
2303 */
2304 if (menuin[0] == ']') {
2305 find_next_call();
2306
2307 } else {
2308 find_prev_call();
2309 }
2310
2311 if (current_call != -1) {
2312
2313 pjsua_call_get_info(current_call, &call_info);
2314 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
2315 (int)call_info.remote_info.slen,
2316 call_info.remote_info.ptr));
2317
2318 } else {
2319 PJ_LOG(3,(THIS_FILE,"No current dialog"));
2320 }
2321 break;
2322
2323
2324 case '>':
2325 case '<':
2326 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
2327 break;
2328
2329 i = my_atoi(buf);
2330 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00002331 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002332 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
2333 } else {
2334 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
2335 }
2336 break;
2337
2338
2339 case '+':
2340 if (menuin[1] == 'b') {
2341
2342 pjsua_buddy_config buddy_cfg;
2343 pjsua_buddy_id buddy_id;
2344 pj_status_t status;
2345
2346 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
2347 break;
2348
2349 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
2350 printf("Invalid SIP URI '%s'\n", buf);
2351 break;
2352 }
2353
Benny Prijonoac623b32006-07-03 15:19:31 +00002354 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002355
2356 buddy_cfg.uri = pj_str(buf);
2357 buddy_cfg.subscribe = PJ_TRUE;
2358
2359 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
2360 if (status == PJ_SUCCESS) {
2361 printf("New buddy '%s' added at index %d\n",
2362 buf, buddy_id+1);
2363 }
2364
2365 } else if (menuin[1] == 'a') {
2366
2367 printf("Sorry, this command is not supported yet\n");
2368
2369 } else {
2370 printf("Invalid input %s\n", menuin);
2371 }
2372 break;
2373
2374 case '-':
2375 if (menuin[1] == 'b') {
2376 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
2377 break;
2378
2379 i = my_atoi(buf) - 1;
2380
2381 if (!pjsua_buddy_is_valid(i)) {
2382 printf("Invalid buddy id %d\n", i);
2383 } else {
2384 pjsua_buddy_del(i);
2385 printf("Buddy %d deleted\n", i);
2386 }
2387
2388 } else if (menuin[1] == 'a') {
2389
2390 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
2391 break;
2392
2393 i = my_atoi(buf);
2394
2395 if (!pjsua_acc_is_valid(i)) {
2396 printf("Invalid account id %d\n", i);
2397 } else {
2398 pjsua_acc_del(i);
2399 printf("Account %d deleted\n", i);
2400 }
2401
2402 } else {
2403 printf("Invalid input %s\n", menuin);
2404 }
2405 break;
2406
2407 case 'H':
2408 /*
2409 * Hold call.
2410 */
2411 if (current_call != -1) {
2412
2413 pjsua_call_set_hold(current_call, NULL);
2414
2415 } else {
2416 PJ_LOG(3,(THIS_FILE, "No current call"));
2417 }
2418 break;
2419
2420 case 'v':
2421 /*
2422 * Send re-INVITE (to release hold, etc).
2423 */
2424 if (current_call != -1) {
2425
2426 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
2427
2428 } else {
2429 PJ_LOG(3,(THIS_FILE, "No current call"));
2430 }
2431 break;
2432
2433 case 'x':
2434 /*
2435 * Transfer call.
2436 */
2437 if (current_call == -1) {
2438
2439 PJ_LOG(3,(THIS_FILE, "No current call"));
2440
2441 } else {
2442 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00002443 pjsua_msg_data msg_data;
2444 pjsip_generic_string_hdr refer_sub;
2445 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2446 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00002447 pjsua_call_info ci;
2448
2449 pjsua_call_get_info(current_call, &ci);
2450 printf("Transfering current call [%d] %.*s\n",
2451 current_call,
2452 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002453
2454 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
2455
2456 /* Check if call is still there. */
2457
2458 if (call != current_call) {
2459 puts("Call has been disconnected");
2460 continue;
2461 }
2462
Benny Prijonod524e822006-09-22 12:48:18 +00002463 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002464 if (app_config.no_refersub) {
2465 /* Add Refer-Sub: false in outgoing REFER request */
2466 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
2467 &STR_FALSE);
2468 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
2469 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002470 if (result.nb_result != NO_NB) {
2471 if (result.nb_result == -1 || result.nb_result == 0)
2472 puts("You can't do that with transfer call!");
2473 else {
2474 pjsua_buddy_info binfo;
2475 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00002476 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002477 }
2478
2479 } else if (result.uri_result) {
2480 pj_str_t tmp;
2481 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00002482 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002483 }
2484 }
2485 break;
2486
Benny Prijonof7b1c392006-11-11 16:46:34 +00002487 case 'X':
2488 /*
2489 * Transfer call with replaces.
2490 */
2491 if (current_call == -1) {
2492
2493 PJ_LOG(3,(THIS_FILE, "No current call"));
2494
2495 } else {
2496 int call = current_call;
2497 int dst_call;
2498 pjsua_msg_data msg_data;
2499 pjsip_generic_string_hdr refer_sub;
2500 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2501 pj_str_t STR_FALSE = { "false", 5 };
2502 pjsua_call_id ids[PJSUA_MAX_CALLS];
2503 pjsua_call_info ci;
2504 unsigned i, count;
2505
2506 count = PJ_ARRAY_SIZE(ids);
2507 pjsua_enum_calls(ids, &count);
2508
2509 if (count <= 1) {
2510 puts("There are no other calls");
2511 continue;
2512 }
2513
2514 pjsua_call_get_info(current_call, &ci);
2515 printf("Transfer call [%d] %.*s to one of the following:\n",
2516 current_call,
2517 (int)ci.remote_info.slen, ci.remote_info.ptr);
2518
2519 for (i=0; i<count; ++i) {
2520 pjsua_call_info call_info;
2521
2522 if (ids[i] == call)
2523 continue;
2524
2525 pjsua_call_get_info(ids[i], &call_info);
2526 printf("%d %.*s [%.*s]\n",
2527 ids[i],
2528 (int)call_info.remote_info.slen,
2529 call_info.remote_info.ptr,
2530 (int)call_info.state_text.slen,
2531 call_info.state_text.ptr);
2532 }
2533
2534 if (!simple_input("Enter call number to be replaced",
2535 buf, sizeof(buf)))
2536 continue;
2537
2538 dst_call = my_atoi(buf);
2539
2540 /* Check if call is still there. */
2541
2542 if (call != current_call) {
2543 puts("Call has been disconnected");
2544 continue;
2545 }
2546
2547 /* Check that destination call is valid. */
2548 if (dst_call == call) {
2549 puts("Destination call number must not be the same "
2550 "as the call being transfered");
2551 continue;
2552 }
2553 if (dst_call >= PJSUA_MAX_CALLS) {
2554 puts("Invalid destination call number");
2555 continue;
2556 }
2557 if (!pjsua_call_is_active(dst_call)) {
2558 puts("Invalid destination call number");
2559 continue;
2560 }
2561
2562 pjsua_msg_data_init(&msg_data);
2563 if (app_config.no_refersub) {
2564 /* Add Refer-Sub: false in outgoing REFER request */
2565 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
2566 &STR_FALSE);
2567 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
2568 }
2569
2570 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
2571 }
2572 break;
2573
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002574 case '#':
2575 /*
2576 * Send DTMF strings.
2577 */
2578 if (current_call == -1) {
2579
2580 PJ_LOG(3,(THIS_FILE, "No current call"));
2581
2582 } else if (!pjsua_call_has_media(current_call)) {
2583
2584 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
2585
2586 } else {
2587 pj_str_t digits;
2588 int call = current_call;
2589 pj_status_t status;
2590
2591 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
2592 sizeof(buf)))
2593 {
2594 break;
2595 }
2596
2597 if (call != current_call) {
2598 puts("Call has been disconnected");
2599 continue;
2600 }
2601
2602 digits = pj_str(buf);
2603 status = pjsua_call_dial_dtmf(current_call, &digits);
2604 if (status != PJ_SUCCESS) {
2605 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
2606 } else {
2607 puts("DTMF digits enqueued for transmission");
2608 }
2609 }
2610 break;
2611
Benny Prijono56315612006-07-18 14:39:40 +00002612 case 'S':
2613 /*
2614 * Send arbitrary request
2615 */
2616 if (pjsua_acc_get_count() == 0) {
2617 puts("Sorry, need at least one account configured");
2618 break;
2619 }
2620
2621 puts("Send arbitrary request to remote host");
2622
2623 /* Input METHOD */
2624 if (!simple_input("Request method:",text,sizeof(text)))
2625 break;
2626
2627 /* Input destination URI */
2628 uri = NULL;
2629 ui_input_url("Destination URI", buf, sizeof(buf), &result);
2630 if (result.nb_result != NO_NB) {
2631
2632 if (result.nb_result == -1 || result.nb_result == 0) {
2633 puts("Sorry you can't do that!");
2634 continue;
2635 } else {
2636 pjsua_buddy_info binfo;
2637 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2638 uri = binfo.uri.ptr;
2639 }
2640
2641 } else if (result.uri_result) {
2642 uri = result.uri_result;
2643 }
2644
2645 tmp = pj_str(uri);
2646
2647 send_request(text, &tmp);
2648 break;
2649
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002650 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00002651 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
2652 pj_str_t tmp;
2653 int delay;
2654
2655 tmp.ptr = menuin+6;
2656 tmp.slen = pj_ansi_strlen(menuin)-7;
2657
2658 if (tmp.slen < 1) {
2659 puts("Usage: sleep MSEC");
2660 break;
2661 }
2662
2663 delay = pj_strtoul(&tmp);
2664 if (delay < 0) delay = 0;
2665 pj_thread_sleep(delay);
2666 break;
2667 }
2668 /* Continue below */
2669
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002670 case 'u':
2671 /*
2672 * Subscribe/unsubscribe presence.
2673 */
2674 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
2675 if (result.nb_result != NO_NB) {
2676 if (result.nb_result == -1) {
2677 int i, count;
2678 count = pjsua_get_buddy_count();
2679 for (i=0; i<count; ++i)
2680 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
2681 } else if (result.nb_result == 0) {
2682 puts("Sorry, can only subscribe to buddy's presence, "
2683 "not from existing call");
2684 } else {
2685 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
2686 }
2687
2688 } else if (result.uri_result) {
2689 puts("Sorry, can only subscribe to buddy's presence, "
2690 "not arbitrary URL (for now)");
2691 }
2692
2693 break;
2694
2695 case 'r':
2696 switch (menuin[1]) {
2697 case 'r':
2698 /*
2699 * Re-Register.
2700 */
2701 pjsua_acc_set_registration(current_acc, PJ_TRUE);
2702 break;
2703 case 'u':
2704 /*
2705 * Unregister
2706 */
2707 pjsua_acc_set_registration(current_acc, PJ_FALSE);
2708 break;
2709 }
2710 break;
2711
2712 case 't':
2713 pjsua_acc_get_info(current_acc, &acc_info);
2714 acc_info.online_status = !acc_info.online_status;
2715 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
2716 printf("Setting %s online status to %s\n",
2717 acc_info.acc_uri.ptr,
2718 (acc_info.online_status?"online":"offline"));
2719 break;
2720
2721 case 'c':
2722 switch (menuin[1]) {
2723 case 'l':
2724 conf_list();
2725 break;
2726 case 'c':
2727 case 'd':
2728 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002729 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002730 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002731 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002732 const char *src_title, *dst_title;
2733
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002734 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002735
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002736 if (cnt != 3) {
2737 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002738
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002739 src_title = (menuin[1]=='c'?
2740 "Connect src port #":
2741 "Disconnect src port #");
2742 dst_title = (menuin[1]=='c'?
2743 "To dst port #":
2744 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002745
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002746 if (!simple_input(src_title, src_port, sizeof(src_port)))
2747 break;
2748
2749 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
2750 break;
2751 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002752
2753 if (menuin[1]=='c') {
2754 status = pjsua_conf_connect(my_atoi(src_port),
2755 my_atoi(dst_port));
2756 } else {
2757 status = pjsua_conf_disconnect(my_atoi(src_port),
2758 my_atoi(dst_port));
2759 }
2760 if (status == PJ_SUCCESS) {
2761 puts("Success");
2762 } else {
2763 puts("ERROR!!");
2764 }
2765 }
2766 break;
2767 }
2768 break;
2769
Benny Prijono6dd967c2006-12-26 02:27:14 +00002770 case 'V':
2771 /* Adjust audio volume */
2772 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
2773 if (simple_input(buf,text,sizeof(text))) {
2774 char *err;
2775 app_config.mic_level = (float)strtod(text, &err);
2776 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
2777 }
2778 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
2779 app_config.speaker_level);
2780 if (simple_input(buf,text,sizeof(text))) {
2781 char *err;
2782 app_config.speaker_level = (float)strtod(text, &err);
2783 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
2784 }
2785
2786 break;
2787
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002788 case 'd':
2789 if (menuin[1] == 'c') {
2790 char settings[2000];
2791 int len;
2792
2793 len = write_settings(&app_config, settings, sizeof(settings));
2794 if (len < 1)
2795 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
2796 else
2797 PJ_LOG(3,(THIS_FILE,
2798 "Dumping configuration (%d bytes):\n%s\n",
2799 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00002800
2801 } else if (menuin[1] == 'q') {
2802
2803 if (current_call != PJSUA_INVALID_ID) {
2804 char buf[1024];
2805 pjsua_call_dump(current_call, PJ_TRUE, buf,
2806 sizeof(buf), " ");
2807 PJ_LOG(3,(THIS_FILE, "\n%s", buf));
2808 } else {
2809 PJ_LOG(3,(THIS_FILE, "No current call"));
2810 }
2811
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002812 } else {
2813 app_dump(menuin[1]=='d');
2814 }
2815 break;
2816
2817
2818 case 'f':
2819 if (simple_input("Enter output filename", buf, sizeof(buf))) {
2820 char settings[2000];
2821 int len;
2822
2823 len = write_settings(&app_config, settings, sizeof(settings));
2824 if (len < 1)
2825 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
2826 else {
2827 pj_oshandle_t fd;
2828 pj_status_t status;
2829
2830 status = pj_file_open(app_config.pool, buf,
2831 PJ_O_WRONLY, &fd);
2832 if (status != PJ_SUCCESS) {
2833 pjsua_perror(THIS_FILE, "Unable to open file", status);
2834 } else {
2835 pj_ssize_t size = len;
2836 pj_file_write(fd, settings, &size);
2837 pj_file_close(fd);
2838
2839 printf("Settings successfully written to '%s'\n", buf);
2840 }
2841 }
2842
2843 }
2844 break;
2845
2846
2847 case 'q':
2848 goto on_exit;
2849
2850
2851 default:
2852 if (menuin[0] != '\n' && menuin[0] != '\r') {
2853 printf("Invalid input %s", menuin);
2854 }
2855 keystroke_help();
2856 break;
2857 }
2858 }
2859
2860on_exit:
2861 ;
2862}
2863
2864
2865/*****************************************************************************
2866 * Public API
2867 */
2868
2869pj_status_t app_init(int argc, char *argv[])
2870{
Benny Prijonoe93e2872006-06-28 16:46:49 +00002871 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00002872 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002873 unsigned i;
2874 pj_status_t status;
2875
2876 /* Create pjsua */
2877 status = pjsua_create();
2878 if (status != PJ_SUCCESS)
2879 return status;
2880
2881 /* Create pool for application */
2882 app_config.pool = pjsua_pool_create("pjsua", 4000, 4000);
2883
2884 /* Initialize default config */
2885 default_config(&app_config);
2886
2887 /* Parse the arguments */
2888 status = parse_args(argc, argv, &app_config, &uri_arg);
2889 if (status != PJ_SUCCESS)
2890 return status;
2891
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002892 /* Initialize application callbacks */
2893 app_config.cfg.cb.on_call_state = &on_call_state;
2894 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
2895 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijono0875ae82006-12-26 00:11:48 +00002896 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002897 app_config.cfg.cb.on_reg_state = &on_reg_state;
2898 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
2899 app_config.cfg.cb.on_pager = &on_pager;
2900 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002901 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00002902 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002903
2904 /* Initialize pjsua */
2905 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
2906 &app_config.media_cfg);
2907 if (status != PJ_SUCCESS)
2908 return status;
2909
Benny Prijonoe909eac2006-07-27 22:04:56 +00002910#ifdef STEREO_DEMO
2911 stereo_demo();
2912#endif
2913
Benny Prijono804ff0a2006-09-14 11:17:48 +00002914 /* Initialize calls data */
2915 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
2916 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
2917 app_config.call_data[i].timer.cb = &call_timeout_callback;
2918 }
2919
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002920 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00002921 for (i=0; i<app_config.wav_count; ++i) {
2922 pjsua_player_id wav_id;
2923
2924 status = pjsua_player_create(&app_config.wav_files[i], 0,
2925 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002926 if (status != PJ_SUCCESS)
2927 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00002928
Benny Prijono72c04d22007-02-17 22:20:58 +00002929 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00002930 app_config.wav_id = wav_id;
2931 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
2932 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002933 }
2934
Benny Prijono4af234b2007-01-24 02:02:09 +00002935 /* Optionally registers tone players */
2936 for (i=0; i<app_config.tone_count; ++i) {
2937 pjmedia_port *tport;
2938 char name[80];
2939 pj_str_t label;
2940 pj_status_t status;
2941
2942 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
2943 app_config.tones[i].freq1,
2944 app_config.tones[i].freq2);
2945 label = pj_str(name);
2946 status = pjmedia_tonegen_create2(app_config.pool, &label,
2947 8000, 1, 160, 16,
2948 PJMEDIA_TONEGEN_LOOP, &tport);
2949 if (status != PJ_SUCCESS) {
2950 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
2951 goto on_error;
2952 }
2953
2954 status = pjsua_conf_add_port(app_config.pool, tport,
2955 &app_config.tone_slots[i]);
2956 pj_assert(status == PJ_SUCCESS);
2957
2958 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
2959 pj_assert(status == PJ_SUCCESS);
2960 }
2961
Benny Prijono1ebd6142006-10-19 15:48:02 +00002962 /* Optionally create recorder file, if any. */
2963 if (app_config.rec_file.slen) {
2964 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
2965 &app_config.rec_id);
2966 if (status != PJ_SUCCESS)
2967 goto on_error;
2968
2969 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
2970 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002971
Benny Prijonoe347cb02007-02-14 14:36:13 +00002972 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
2973
Benny Prijono87ef89a2007-01-14 00:39:45 +00002974 /* Add UDP transport unless it's disabled. */
2975 if (!app_config.no_udp) {
2976 pjsua_acc_id aid;
2977
2978 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
2979 &app_config.udp_cfg,
2980 &transport_id);
2981 if (status != PJ_SUCCESS)
2982 goto on_error;
2983
2984 /* Add local account */
2985 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
2986 //pjsua_acc_set_transport(aid, transport_id);
2987 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00002988
2989 if (app_config.udp_cfg.port == 0) {
2990 pjsua_transport_info ti;
2991 pj_sockaddr_in *a;
2992
2993 pjsua_transport_get_info(transport_id, &ti);
2994 a = (pj_sockaddr_in*)&ti.local_addr;
2995
2996 tcp_cfg.port = pj_ntohs(a->sin_port);
2997 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00002998 }
2999
Benny Prijonoe93e2872006-06-28 16:46:49 +00003000 /* Add TCP transport unless it's disabled */
3001 if (!app_config.no_tcp) {
3002 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00003003 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00003004 &transport_id);
3005 if (status != PJ_SUCCESS)
3006 goto on_error;
3007
3008 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00003009 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00003010 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3011
3012 }
3013
Benny Prijonoe93e2872006-06-28 16:46:49 +00003014
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003015#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
3016 /* Add TLS transport when application wants one */
3017 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00003018
3019 pjsua_acc_id acc_id;
3020
3021 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00003022 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003023 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00003024 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003025 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00003026 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003027 if (status != PJ_SUCCESS)
3028 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00003029
3030 /* Add local account */
3031 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
3032 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003033 }
3034#endif
3035
Benny Prijonoe93e2872006-06-28 16:46:49 +00003036 if (transport_id == -1) {
3037 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
3038 status = -1;
3039 goto on_error;
3040 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003041
3042
3043 /* Add accounts */
3044 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003045 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003046 if (status != PJ_SUCCESS)
3047 goto on_error;
3048 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3049 }
3050
3051 /* Add buddies */
3052 for (i=0; i<app_config.buddy_cnt; ++i) {
3053 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
3054 if (status != PJ_SUCCESS)
3055 goto on_error;
3056 }
3057
3058 /* Optionally set codec orders */
3059 for (i=0; i<app_config.codec_cnt; ++i) {
3060 pjsua_codec_set_priority(&app_config.codec_arg[i],
3061 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
3062 }
3063
3064 /* Add RTP transports */
3065 status = pjsua_media_transports_create(&app_config.rtp_cfg);
3066 if (status != PJ_SUCCESS)
3067 goto on_error;
3068
Benny Prijono22a300a2006-06-14 20:04:55 +00003069 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00003070#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00003071 if (app_config.null_audio) {
3072 status = pjsua_set_null_snd_dev();
3073 if (status != PJ_SUCCESS)
3074 return status;
3075 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00003076#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00003077
Benny Prijono4e5d5512007-03-06 18:11:30 +00003078 if (app_config.capture_dev != PJSUA_INVALID_ID
3079 || app_config.playback_dev != PJSUA_INVALID_ID) {
3080 status
3081 = pjsua_set_snd_dev(app_config.capture_dev, app_config.playback_dev);
3082 if (status != PJ_SUCCESS)
3083 goto on_error;
3084 }
3085
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003086 return PJ_SUCCESS;
3087
3088on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00003089 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003090 return status;
3091}
3092
3093
3094pj_status_t app_main(void)
3095{
3096 pj_status_t status;
3097
3098 /* Start pjsua */
3099 status = pjsua_start();
3100 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00003101 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003102 return status;
3103 }
3104
3105 console_app_main(&uri_arg);
3106
3107 return PJ_SUCCESS;
3108}
3109
3110pj_status_t app_destroy(void)
3111{
Benny Prijonof762ee72006-12-01 11:14:37 +00003112 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00003113 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00003114
Benny Prijonoe909eac2006-07-27 22:04:56 +00003115#ifdef STEREO_DEMO
3116 if (app_config.snd) {
3117 pjmedia_snd_port_destroy(app_config.snd);
3118 app_config.snd = NULL;
3119 }
3120#endif
3121
Benny Prijono4af234b2007-01-24 02:02:09 +00003122 /* Close tone generators */
3123 for (i=0; i<app_config.tone_count; ++i) {
3124 pjsua_conf_remove_port(app_config.tone_slots[i]);
3125 }
3126
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003127 if (app_config.pool) {
3128 pj_pool_release(app_config.pool);
3129 app_config.pool = NULL;
3130 }
3131
Benny Prijonof762ee72006-12-01 11:14:37 +00003132 status = pjsua_destroy();
3133
3134 pj_bzero(&app_config, sizeof(app_config));
3135
3136 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003137}
Benny Prijonoe909eac2006-07-27 22:04:56 +00003138
3139
3140#ifdef STEREO_DEMO
3141static void stereo_demo()
3142{
3143 pjmedia_port *conf, *splitter, *ch1;
3144 unsigned clock;
3145 pj_status_t status;
3146
3147 /* Disable existing sound device */
3148 conf = pjsua_set_no_snd_dev();
3149
3150 clock = app_config.media_cfg.clock_rate;
3151
3152 /* Create stereo-mono splitter/combiner */
3153 status = pjmedia_splitcomb_create(app_config.pool,
3154 clock /* clock rate */,
3155 2 /* stereo */,
3156 clock*2*10/1000/* 10ms samples * 2ch */,
3157 16 /* bits */,
3158 0 /* options */,
3159 &splitter);
3160 pj_assert(status == PJ_SUCCESS);
3161
3162 /* Connect channel0 (left channel?) to conference port slot0 */
3163 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
3164 0 /*options*/,
3165 conf);
3166 pj_assert(status == PJ_SUCCESS);
3167
3168 /* Create reverse channel for channel1 (right channel?)... */
3169 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
3170 splitter,
3171 1 /* ch1 */,
3172 0 /* options */,
3173 &ch1);
3174 pj_assert(status == PJ_SUCCESS);
3175
3176 /* .. and register it to conference bridge (it would be slot1
3177 * if there's no other devices connected to the bridge)
3178 */
3179 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
3180 pj_assert(status == PJ_SUCCESS);
3181
3182 /* Create sound device */
3183 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
3184 clock /* clock rate */,
3185 2 /* stereo */,
3186 clock*2*10/1000 /* 10 ms samples * 2ch */,
3187 16 /* bits */,
3188 0, &app_config.snd);
3189 pj_assert(status == PJ_SUCCESS);
3190
3191
3192 /* Connect the splitter to the sound device */
3193 status = pjmedia_snd_port_connect(app_config.snd, splitter);
3194 pj_assert(status == PJ_SUCCESS);
3195
3196}
3197#endif
3198