blob: 51438ee56f0bdc9d2c11c52a2f562cc5278d7aba [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
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000022#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000023#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000024
Benny Prijonoe909eac2006-07-27 22:04:56 +000025//#define STEREO_DEMO
Benny 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 Prijonodcfc0ba2007-09-30 16:50:27 +0000130 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijono2a67ea42007-10-25 02:51:33 +0000131 puts (" --service-route Enable Service-Route processing");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000132 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000133 puts ("");
134 puts ("SIP Account Control:");
135 puts (" --next-account Add more account");
136 puts ("");
137 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000138 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
139 puts (" TCP and UDP transports on the specified port, unless");
140 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000141 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
142 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000143 puts (" --no-tcp Disable TCP transport.");
144 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000145 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
146 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000147 puts (" --outbound=url Set the URL of global outbound proxy server");
148 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000149 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000150 puts ("");
151 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000152 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000153 puts (" --tls-ca-file Specify TLS CA file (default=none)");
154 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
155 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
156 puts (" --tls-password Specify TLS password to private key file (default=none)");
157 puts (" --tls-verify-server Verify server's certificate (default=no)");
158 puts (" --tls-verify-client Verify client's certificate (default=no)");
159 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
160
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000161 puts ("");
162 puts ("Media Options:");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000163 puts (" --use-ice Enable ICE (default:no)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000164 puts (" --add-codec=name Manually add codec (default is to enable all)");
165 puts (" --clock-rate=N Override sound device clock rate");
166 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000167 puts (" --play-file=file Register WAV file in conference bridge.");
168 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000169 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
170 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
171 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000172 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173 puts (" --auto-play Automatically play the file (to incoming calls only)");
174 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000175 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000176 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
177 puts (" --auto-rec Automatically record conversation");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
Benny Prijono00cae612006-07-31 15:19:36 +0000179 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000180 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000181 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000182 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono00cae612006-07-31 15:19:36 +0000183 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
184 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
185 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000186 puts (" --capture-dev=id Audio capture device ID (default=-1)");
187 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000188
Benny Prijono0a12f002006-07-26 17:05:39 +0000189
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000190 puts ("");
191 puts ("Buddy List (can be more than one):");
192 puts (" --add-buddy url Add the specified URL to the buddy list.");
193 puts ("");
194 puts ("User Agent options:");
195 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
196 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000197 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000198 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000199 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000200
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000201 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000202 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
203 puts ("");
204
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000205 fflush(stdout);
206}
207
208
209/* Set default config. */
210static void default_config(struct app_config *cfg)
211{
Benny Prijono56315612006-07-18 14:39:40 +0000212 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000213 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000214
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000215 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000216 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000217 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
218
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000219 pjsua_logging_config_default(&cfg->log_cfg);
220 pjsua_media_config_default(&cfg->media_cfg);
221 pjsua_transport_config_default(&cfg->udp_cfg);
222 cfg->udp_cfg.port = 5060;
223 pjsua_transport_config_default(&cfg->rtp_cfg);
224 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000225 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000226 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000227 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000229 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000230 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000231 cfg->capture_dev = PJSUA_INVALID_ID;
232 cfg->playback_dev = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000233
234 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
235 pjsua_acc_config_default(&cfg->acc_cfg[i]);
236
237 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
238 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000239}
240
241
242/*
243 * Read command arguments from config file.
244 */
245static int read_config_file(pj_pool_t *pool, const char *filename,
246 int *app_argc, char ***app_argv)
247{
248 int i;
249 FILE *fhnd;
250 char line[200];
251 int argc = 0;
252 char **argv;
253 enum { MAX_ARGS = 64 };
254
255 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
256 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
257 argv[argc++] = *app_argv[0];
258
259 /* Open config file. */
260 fhnd = fopen(filename, "rt");
261 if (!fhnd) {
262 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
263 fflush(stdout);
264 return -1;
265 }
266
267 /* Scan tokens in the file. */
268 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000269 char *token;
270 char *p;
271 const char *whitespace = " \t\r\n";
272 char cDelimiter;
273 int len, token_len;
274
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000275 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000276
277 // Trim ending newlines
278 len = strlen(line);
279 if (line[len-1]=='\n')
280 line[--len] = '\0';
281 if (line[len-1]=='\r')
282 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000283
Benny Prijonobf5b4692007-06-28 03:20:17 +0000284 if (len==0) continue;
285
286 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
287 // first, scan whitespaces
288 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
289
290 if (*p == '\0') // are we done yet?
291 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000292
Benny Prijonobf5b4692007-06-28 03:20:17 +0000293 if (*p == '"' || *p == '\'') { // is token a quoted string
294 cDelimiter = *p++; // save quote delimiter
295 token = p;
296
297 while (*p != '\0' && *p != cDelimiter) p++;
298
299 if (*p == '\0') // found end of the line, but,
300 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000301
Benny Prijonobf5b4692007-06-28 03:20:17 +0000302 } else { // token's not a quoted string
303 token = p;
304
305 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
306
307 cDelimiter = *p;
308 }
309
310 *p = '\0';
311 token_len = p-token;
312
313 if (token_len > 0) {
314 if (*token == '#')
315 break; // ignore remainder of line
316
317 argv[argc] = pj_pool_alloc(pool, token_len + 1);
318 pj_memcpy(argv[argc], token, token_len + 1);
319 ++argc;
320 }
321
322 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000323 }
324 }
325
326 /* Copy arguments from command line */
327 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
328 argv[argc++] = (*app_argv)[i];
329
330 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
331 PJ_LOG(1,(THIS_FILE,
332 "Too many arguments specified in cmd line/config file"));
333 fflush(stdout);
334 fclose(fhnd);
335 return -1;
336 }
337
338 fclose(fhnd);
339
340 /* Assign the new command line back to the original command line. */
341 *app_argc = argc;
342 *app_argv = argv;
343 return 0;
344
345}
346
347static int my_atoi(const char *cs)
348{
349 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000350
351 pj_cstr(&s, cs);
352 if (cs[0] == '-') {
353 s.ptr++, s.slen--;
354 return 0 - (int)pj_strtoul(&s);
355 } else if (cs[0] == '+') {
356 s.ptr++, s.slen--;
357 return pj_strtoul(&s);
358 } else {
359 return pj_strtoul(&s);
360 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000361}
362
363
364/* Parse arguments. */
365static pj_status_t parse_args(int argc, char *argv[],
366 struct app_config *cfg,
367 pj_str_t *uri_to_call)
368{
369 int c;
370 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000371 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000373 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
374 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono2a67ea42007-10-25 02:51:33 +0000375 OPT_100REL, OPT_SERVICE_ROUTE, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000376 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
378 OPT_AUTO_ANSWER, OPT_AUTO_HANGUP, OPT_AUTO_PLAY, OPT_AUTO_LOOP,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000379 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_USE_ICE,
Benny Prijono4af234b2007-01-24 02:02:09 +0000380 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
381 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000382 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijonod79f25c2006-08-02 19:41:37 +0000383 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000384 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000385 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000386 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000387 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
388 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
389 OPT_TLS_NEG_TIMEOUT,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000390 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000391 };
392 struct pj_getopt_option long_options[] = {
393 { "config-file",1, 0, OPT_CONFIG_FILE},
394 { "log-file", 1, 0, OPT_LOG_FILE},
395 { "log-level", 1, 0, OPT_LOG_LEVEL},
396 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
397 { "help", 0, 0, OPT_HELP},
398 { "version", 0, 0, OPT_VERSION},
399 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
400 { "null-audio", 0, 0, OPT_NULL_AUDIO},
401 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000402 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000403 { "no-tcp", 0, 0, OPT_NO_TCP},
404 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000405 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000406 { "proxy", 1, 0, OPT_PROXY},
407 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
408 { "registrar", 1, 0, OPT_REGISTRAR},
409 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000410 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000411 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono2a67ea42007-10-25 02:51:33 +0000412 { "service-route", 0, 0, OPT_SERVICE_ROUTE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 { "id", 1, 0, OPT_ID},
414 { "contact", 1, 0, OPT_CONTACT},
415 { "realm", 1, 0, OPT_REALM},
416 { "username", 1, 0, OPT_USERNAME},
417 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000418 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000419 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000420 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000421 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
422 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
423 { "no-presence", 0, 0, OPT_NO_PRESENCE},
424 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
425 { "auto-hangup",1, 0, OPT_AUTO_HANGUP},
426 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000427 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000428 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
429 { "auto-conf", 0, 0, OPT_AUTO_CONF},
430 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000431 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000432 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000433 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000434 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000435 { "add-codec", 1, 0, OPT_ADD_CODEC},
436 { "complexity", 1, 0, OPT_COMPLEXITY},
437 { "quality", 1, 0, OPT_QUALITY},
438 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000439 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000440 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono00cae612006-07-31 15:19:36 +0000441 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
442 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
443 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000445 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000446 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000447 { "duration", 1, 0, OPT_DURATION},
448 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000449 { "use-tls", 0, 0, OPT_USE_TLS},
450 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000451 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
452 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000453 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000454 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
455 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
456 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000457 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
458 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000459 { NULL, 0, 0, 0}
460 };
461 pj_status_t status;
462 pjsua_acc_config *cur_acc;
463 char *config_file = NULL;
464 unsigned i;
465
466 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000467 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000468 while ((c=pj_getopt_long(argc, argv, "", long_options,
469 &option_index)) != -1)
470 {
471 switch (c) {
472 case OPT_CONFIG_FILE:
473 config_file = pj_optarg;
474 break;
475 }
476 if (config_file)
477 break;
478 }
479
480 if (config_file) {
481 status = read_config_file(app_config.pool, config_file, &argc, &argv);
482 if (status != 0)
483 return status;
484 }
485
486 cfg->acc_cnt = 0;
487 cur_acc = &cfg->acc_cfg[0];
488
489
490 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
491 * read from config file.
492 */
493 pj_optind = 0;
494 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000495 pj_str_t tmp;
496 long lval;
497
498 switch (c) {
499
Benny Prijono6f137482006-06-15 11:31:36 +0000500 case OPT_CONFIG_FILE:
501 /* Ignore as this has been processed before */
502 break;
503
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504 case OPT_LOG_FILE:
505 cfg->log_cfg.log_filename = pj_str(pj_optarg);
506 break;
507
508 case OPT_LOG_LEVEL:
509 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
510 if (c < 0 || c > 6) {
511 PJ_LOG(1,(THIS_FILE,
512 "Error: expecting integer value 0-6 "
513 "for --log-level"));
514 return PJ_EINVAL;
515 }
516 cfg->log_cfg.level = c;
517 pj_log_set_level( c );
518 break;
519
520 case OPT_APP_LOG_LEVEL:
521 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
522 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
523 PJ_LOG(1,(THIS_FILE,
524 "Error: expecting integer value 0-6 "
525 "for --app-log-level"));
526 return PJ_EINVAL;
527 }
528 break;
529
530 case OPT_HELP:
531 usage();
532 return PJ_EINVAL;
533
534 case OPT_VERSION: /* version */
535 pj_dump_config();
536 return PJ_EINVAL;
537
538 case OPT_NULL_AUDIO:
539 cfg->null_audio = PJ_TRUE;
540 break;
541
542 case OPT_CLOCK_RATE:
543 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
544 if (lval < 8000 || lval > 48000) {
545 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
546 "8000-48000 for clock rate"));
547 return PJ_EINVAL;
548 }
549 cfg->media_cfg.clock_rate = lval;
550 break;
551
552 case OPT_LOCAL_PORT: /* local-port */
553 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000554 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 PJ_LOG(1,(THIS_FILE,
556 "Error: expecting integer value for "
557 "--local-port"));
558 return PJ_EINVAL;
559 }
560 cfg->udp_cfg.port = (pj_uint16_t)lval;
561 break;
562
Benny Prijono0a5cad82006-09-26 13:21:02 +0000563 case OPT_IP_ADDR: /* ip-addr */
564 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
565 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
566 break;
567
Benny Prijonoe93e2872006-06-28 16:46:49 +0000568 case OPT_NO_UDP: /* no-udp */
569 if (cfg->no_tcp) {
570 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
571 return PJ_EINVAL;
572 }
573
574 cfg->no_udp = PJ_TRUE;
575 break;
576
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000577 case OPT_NOREFERSUB: /* norefersub */
578 cfg->no_refersub = PJ_TRUE;
579 break;
580
Benny Prijonoe93e2872006-06-28 16:46:49 +0000581 case OPT_NO_TCP: /* no-tcp */
582 if (cfg->no_udp) {
583 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
584 return PJ_EINVAL;
585 }
586
587 cfg->no_tcp = PJ_TRUE;
588 break;
589
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000590 case OPT_PROXY: /* proxy */
591 if (pjsua_verify_sip_url(pj_optarg) != 0) {
592 PJ_LOG(1,(THIS_FILE,
593 "Error: invalid SIP URL '%s' "
594 "in proxy argument", pj_optarg));
595 return PJ_EINVAL;
596 }
597 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
598 break;
599
600 case OPT_OUTBOUND_PROXY: /* outbound proxy */
601 if (pjsua_verify_sip_url(pj_optarg) != 0) {
602 PJ_LOG(1,(THIS_FILE,
603 "Error: invalid SIP URL '%s' "
604 "in outbound proxy argument", pj_optarg));
605 return PJ_EINVAL;
606 }
607 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
608 break;
609
610 case OPT_REGISTRAR: /* registrar */
611 if (pjsua_verify_sip_url(pj_optarg) != 0) {
612 PJ_LOG(1,(THIS_FILE,
613 "Error: invalid SIP URL '%s' in "
614 "registrar argument", pj_optarg));
615 return PJ_EINVAL;
616 }
617 cur_acc->reg_uri = pj_str(pj_optarg);
618 break;
619
620 case OPT_REG_TIMEOUT: /* reg-timeout */
621 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
622 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
623 PJ_LOG(1,(THIS_FILE,
624 "Error: invalid value for --reg-timeout "
625 "(expecting 1-3600)"));
626 return PJ_EINVAL;
627 }
628 break;
629
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000630 case OPT_PUBLISH: /* publish */
631 cur_acc->publish_enabled = PJ_TRUE;
632 break;
633
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000634 case OPT_100REL: /** 100rel */
635 cur_acc->require_100rel = PJ_TRUE;
636 cfg->cfg.require_100rel = PJ_TRUE;
637 break;
638
Benny Prijono2a67ea42007-10-25 02:51:33 +0000639 case OPT_SERVICE_ROUTE: /* Service-Route processing */
640 cur_acc->enable_service_route = PJ_TRUE;
641 break;
642
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000643 case OPT_ID: /* id */
644 if (pjsua_verify_sip_url(pj_optarg) != 0) {
645 PJ_LOG(1,(THIS_FILE,
646 "Error: invalid SIP URL '%s' "
647 "in local id argument", pj_optarg));
648 return PJ_EINVAL;
649 }
650 cur_acc->id = pj_str(pj_optarg);
651 break;
652
653 case OPT_CONTACT: /* contact */
654 if (pjsua_verify_sip_url(pj_optarg) != 0) {
655 PJ_LOG(1,(THIS_FILE,
656 "Error: invalid SIP URL '%s' "
657 "in contact argument", pj_optarg));
658 return PJ_EINVAL;
659 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000660 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000661 break;
662
663 case OPT_NEXT_ACCOUNT: /* Add more account. */
664 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000665 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000666 break;
667
668 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000669 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
670 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000671 break;
672
673 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000674 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000675 break;
676
677 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000678 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
679 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000680#if PJSIP_HAS_DIGEST_AKA_AUTH
681 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
682 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
683 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
684#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000685 break;
686
687 case OPT_NEXT_CRED: /* next credential */
688 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000689 break;
690
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000691 case OPT_NAMESERVER: /* nameserver */
692 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
693 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
694 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
695 return PJ_ETOOMANY;
696 }
697 break;
698
Benny Prijonoebbf6892007-03-24 17:37:25 +0000699 case OPT_STUN_DOMAIN: /* STUN domain */
700 cfg->cfg.stun_domain = pj_str(pj_optarg);
701 break;
702
Benny Prijonoc97608e2007-03-23 16:34:20 +0000703 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000704 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000705 break;
706
707 case OPT_ADD_BUDDY: /* Add to buddy list. */
708 if (pjsua_verify_sip_url(pj_optarg) != 0) {
709 PJ_LOG(1,(THIS_FILE,
710 "Error: invalid URL '%s' in "
711 "--add-buddy option", pj_optarg));
712 return -1;
713 }
714 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
715 PJ_LOG(1,(THIS_FILE,
716 "Error: too many buddies in buddy list."));
717 return -1;
718 }
719 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
720 cfg->buddy_cnt++;
721 break;
722
723 case OPT_AUTO_PLAY:
724 cfg->auto_play = 1;
725 break;
726
Benny Prijono1ebd6142006-10-19 15:48:02 +0000727 case OPT_AUTO_REC:
728 cfg->auto_rec = 1;
729 break;
730
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000731 case OPT_AUTO_LOOP:
732 cfg->auto_loop = 1;
733 break;
734
Benny Prijono7ca96da2006-08-07 12:11:40 +0000735 case OPT_AUTO_CONF:
736 cfg->auto_conf = 1;
737 break;
738
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000739 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000740 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000741 break;
742
Benny Prijono4af234b2007-01-24 02:02:09 +0000743 case OPT_PLAY_TONE:
744 {
745 int f1, f2, on, off;
746 int n;
747
748 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
749 if (n != 4) {
750 puts("Expecting f1,f2,on,off in --play-tone");
751 return -1;
752 }
753
754 cfg->tones[cfg->tone_count].freq1 = (short)f1;
755 cfg->tones[cfg->tone_count].freq2 = (short)f2;
756 cfg->tones[cfg->tone_count].on_msec = (short)on;
757 cfg->tones[cfg->tone_count].off_msec = (short)off;
758 ++cfg->tone_count;
759 }
760 break;
761
Benny Prijono1ebd6142006-10-19 15:48:02 +0000762 case OPT_REC_FILE:
763 cfg->rec_file = pj_str(pj_optarg);
764 break;
765
Benny Prijonoc97608e2007-03-23 16:34:20 +0000766 case OPT_USE_ICE:
767 cfg->media_cfg.enable_ice = PJ_TRUE;
768 break;
769
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000770 case OPT_RTP_PORT:
771 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +0000772 if (cfg->rtp_cfg.port == 0) {
773 enum { START_PORT=4000 };
774 unsigned range;
775
776 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
777 cfg->rtp_cfg.port = START_PORT +
778 ((pj_rand() % range) & 0xFFFE);
779 }
780
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000781 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
782 PJ_LOG(1,(THIS_FILE,
783 "Error: rtp-port argument value "
784 "(expecting 1-65535"));
785 return -1;
786 }
787 break;
788
789 case OPT_ADD_CODEC:
790 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
791 break;
792
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000793 /* These options were no longer valid after new pjsua */
794 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000795 case OPT_COMPLEXITY:
796 cfg->complexity = my_atoi(pj_optarg);
797 if (cfg->complexity < 0 || cfg->complexity > 10) {
798 PJ_LOG(1,(THIS_FILE,
799 "Error: invalid --complexity (expecting 0-10"));
800 return -1;
801 }
802 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000803 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000804
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000805 case OPT_DURATION:
806 cfg->duration = my_atoi(pj_optarg);
807 break;
808
Benny Prijonof521eb02006-08-06 23:07:25 +0000809 case OPT_THREAD_CNT:
810 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
811 if (cfg->cfg.thread_cnt > 128) {
812 PJ_LOG(1,(THIS_FILE,
813 "Error: invalid --thread-cnt option"));
814 return -1;
815 }
816 break;
817
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000818 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +0000819 cfg->media_cfg.ptime = my_atoi(pj_optarg);
820 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000821 PJ_LOG(1,(THIS_FILE,
822 "Error: invalid --ptime option"));
823 return -1;
824 }
825 break;
826
Benny Prijono0a12f002006-07-26 17:05:39 +0000827 case OPT_NO_VAD:
828 cfg->media_cfg.no_vad = PJ_TRUE;
829 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000830
Benny Prijonod79f25c2006-08-02 19:41:37 +0000831 case OPT_EC_TAIL:
832 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
833 if (cfg->media_cfg.ec_tail_len > 1000) {
834 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
835 "is too big"));
836 return -1;
837 }
838 break;
839
Benny Prijono0498d902006-06-19 14:49:14 +0000840 case OPT_QUALITY:
841 cfg->media_cfg.quality = my_atoi(pj_optarg);
842 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
843 PJ_LOG(1,(THIS_FILE,
844 "Error: invalid --quality (expecting 0-10"));
845 return -1;
846 }
847 break;
848
Benny Prijono00cae612006-07-31 15:19:36 +0000849 case OPT_ILBC_MODE:
850 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
851 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
852 PJ_LOG(1,(THIS_FILE,
853 "Error: invalid --ilbc-mode (expecting 20 or 30"));
854 return -1;
855 }
856 break;
857
858 case OPT_RX_DROP_PCT:
859 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
860 if (cfg->media_cfg.rx_drop_pct > 100) {
861 PJ_LOG(1,(THIS_FILE,
862 "Error: invalid --rx-drop-pct (expecting <= 100"));
863 return -1;
864 }
865 break;
866
867 case OPT_TX_DROP_PCT:
868 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
869 if (cfg->media_cfg.tx_drop_pct > 100) {
870 PJ_LOG(1,(THIS_FILE,
871 "Error: invalid --tx-drop-pct (expecting <= 100"));
872 return -1;
873 }
874 break;
875
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000876 case OPT_AUTO_ANSWER:
877 cfg->auto_answer = my_atoi(pj_optarg);
878 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
879 PJ_LOG(1,(THIS_FILE,
880 "Error: invalid code in --auto-answer "
881 "(expecting 100-699"));
882 return -1;
883 }
884 break;
885
886 case OPT_MAX_CALLS:
887 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +0000888 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +0000889 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
890 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +0000891 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000892 return -1;
893 }
894 break;
895
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000896 case OPT_USE_TLS:
897 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +0000898#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
899 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
900 return -1;
901#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000902 break;
903
904 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +0000905 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
906#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
907 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
908 return -1;
909#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000910 break;
911
Benny Prijonof3bbc132006-12-25 06:43:59 +0000912 case OPT_TLS_CERT_FILE:
913 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
914#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
915 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
916 return -1;
917#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000918 break;
919
Benny Prijonof3bbc132006-12-25 06:43:59 +0000920 case OPT_TLS_PRIV_FILE:
921 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
922 break;
923
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000924 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +0000925 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
926#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
927 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
928 return -1;
929#endif
930 break;
931
932 case OPT_TLS_VERIFY_SERVER:
933 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
934 break;
935
936 case OPT_TLS_VERIFY_CLIENT:
937 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
938 break;
939
940 case OPT_TLS_NEG_TIMEOUT:
941 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000942 break;
943
Benny Prijono4e5d5512007-03-06 18:11:30 +0000944 case OPT_CAPTURE_DEV:
945 cfg->capture_dev = atoi(pj_optarg);
946 break;
947
948 case OPT_PLAYBACK_DEV:
949 cfg->playback_dev = atoi(pj_optarg);
950 break;
951
Benny Prijono22a300a2006-06-14 20:04:55 +0000952 default:
Benny Prijono787b8692006-06-19 12:40:03 +0000953 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +0000954 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000955 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +0000956 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000957 }
958 }
959
960 if (pj_optind != argc) {
961 pj_str_t uri_arg;
962
963 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
964 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
965 return -1;
966 }
967 uri_arg = pj_str(argv[pj_optind]);
968 if (uri_to_call)
969 *uri_to_call = uri_arg;
970 pj_optind++;
971
972 /* Add URI to call to buddy list if it's not already there */
973 for (i=0; i<cfg->buddy_cnt; ++i) {
974 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
975 break;
976 }
977 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
978 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
979 }
980
981 } else {
982 if (uri_to_call)
983 uri_to_call->slen = 0;
984 }
985
986 if (pj_optind != argc) {
987 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
988 return PJ_EINVAL;
989 }
990
Benny Prijono56315612006-07-18 14:39:40 +0000991 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
992 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000993
994 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000995 if (cfg->acc_cfg[i].cred_info[cfg->acc_cfg[i].cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000996 {
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000997 cfg->acc_cfg[i].cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000998 }
999 }
1000
1001
1002 return PJ_SUCCESS;
1003}
1004
1005
1006/*
1007 * Save account settings
1008 */
1009static void write_account_settings(int acc_index, pj_str_t *result)
1010{
1011 unsigned i;
1012 char line[128];
1013 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1014
1015
1016 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1017 pj_strcat2(result, line);
1018
1019
1020 /* Identity */
1021 if (acc_cfg->id.slen) {
1022 pj_ansi_sprintf(line, "--id %.*s\n",
1023 (int)acc_cfg->id.slen,
1024 acc_cfg->id.ptr);
1025 pj_strcat2(result, line);
1026 }
1027
1028 /* Registrar server */
1029 if (acc_cfg->reg_uri.slen) {
1030 pj_ansi_sprintf(line, "--registrar %.*s\n",
1031 (int)acc_cfg->reg_uri.slen,
1032 acc_cfg->reg_uri.ptr);
1033 pj_strcat2(result, line);
1034
1035 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1036 acc_cfg->reg_timeout);
1037 pj_strcat2(result, line);
1038 }
1039
1040 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001041 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001042 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001043 (int)acc_cfg->force_contact.slen,
1044 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 pj_strcat2(result, line);
1046 }
1047
1048 /* Proxy */
1049 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1050 pj_ansi_sprintf(line, "--proxy %.*s\n",
1051 (int)acc_cfg->proxy[i].slen,
1052 acc_cfg->proxy[i].ptr);
1053 pj_strcat2(result, line);
1054 }
1055
1056 /* Credentials */
1057 for (i=0; i<acc_cfg->cred_count; ++i) {
1058 if (acc_cfg->cred_info[i].realm.slen) {
1059 pj_ansi_sprintf(line, "--realm %.*s\n",
1060 (int)acc_cfg->cred_info[i].realm.slen,
1061 acc_cfg->cred_info[i].realm.ptr);
1062 pj_strcat2(result, line);
1063 }
1064
1065 if (acc_cfg->cred_info[i].username.slen) {
1066 pj_ansi_sprintf(line, "--username %.*s\n",
1067 (int)acc_cfg->cred_info[i].username.slen,
1068 acc_cfg->cred_info[i].username.ptr);
1069 pj_strcat2(result, line);
1070 }
1071
1072 if (acc_cfg->cred_info[i].data.slen) {
1073 pj_ansi_sprintf(line, "--password %.*s\n",
1074 (int)acc_cfg->cred_info[i].data.slen,
1075 acc_cfg->cred_info[i].data.ptr);
1076 pj_strcat2(result, line);
1077 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001078
1079 if (i != acc_cfg->cred_count - 1)
1080 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001081 }
1082
1083}
1084
1085
1086/*
1087 * Write settings.
1088 */
1089static int write_settings(const struct app_config *config,
1090 char *buf, pj_size_t max)
1091{
1092 unsigned acc_index;
1093 unsigned i;
1094 pj_str_t cfg;
1095 char line[128];
1096
1097 PJ_UNUSED_ARG(max);
1098
1099 cfg.ptr = buf;
1100 cfg.slen = 0;
1101
1102 /* Logging. */
1103 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1104 pj_ansi_sprintf(line, "--log-level %d\n",
1105 config->log_cfg.level);
1106 pj_strcat2(&cfg, line);
1107
1108 pj_ansi_sprintf(line, "--app-log-level %d\n",
1109 config->log_cfg.console_level);
1110 pj_strcat2(&cfg, line);
1111
1112 if (config->log_cfg.log_filename.slen) {
1113 pj_ansi_sprintf(line, "--log-file %.*s\n",
1114 (int)config->log_cfg.log_filename.slen,
1115 config->log_cfg.log_filename.ptr);
1116 pj_strcat2(&cfg, line);
1117 }
1118
1119
1120 /* Save account settings. */
1121 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1122
1123 write_account_settings(acc_index, &cfg);
1124
1125 if (acc_index < config->acc_cnt-1)
1126 pj_strcat2(&cfg, "--next-account\n");
1127 }
1128
1129
1130 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1131
1132 /* Outbound proxy */
1133 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1134 pj_ansi_sprintf(line, "--outbound %.*s\n",
1135 (int)config->cfg.outbound_proxy[i].slen,
1136 config->cfg.outbound_proxy[i].ptr);
1137 pj_strcat2(&cfg, line);
1138 }
1139
1140
1141 /* UDP Transport. */
1142 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1143 pj_strcat2(&cfg, line);
1144
Benny Prijono0a5cad82006-09-26 13:21:02 +00001145 /* IP address, if any. */
1146 if (config->udp_cfg.public_addr.slen) {
1147 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1148 (int)config->udp_cfg.public_addr.slen,
1149 config->udp_cfg.public_addr.ptr);
1150 pj_strcat2(&cfg, line);
1151 }
1152
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001153 /* No TCP ? */
1154 if (config->no_tcp) {
1155 pj_strcat2(&cfg, "--no-tcp\n");
1156 }
1157
1158 /* No UDP ? */
1159 if (config->no_udp) {
1160 pj_strcat2(&cfg, "--no-udp\n");
1161 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001162
1163 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001164 if (config->cfg.stun_domain.slen) {
1165 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1166 (int)config->cfg.stun_domain.slen,
1167 config->cfg.stun_domain.ptr);
1168 pj_strcat2(&cfg, line);
1169 }
1170 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001171 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001172 (int)config->cfg.stun_host.slen,
1173 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001174 pj_strcat2(&cfg, line);
1175 }
1176
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001177
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001178 /* TLS */
1179 if (config->use_tls)
1180 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001181 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001182 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001183 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1184 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001185 pj_strcat2(&cfg, line);
1186 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001187 if (config->udp_cfg.tls_setting.cert_file.slen) {
1188 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1189 (int)config->udp_cfg.tls_setting.cert_file.slen,
1190 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001191 pj_strcat2(&cfg, line);
1192 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001193 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1194 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1195 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1196 config->udp_cfg.tls_setting.privkey_file.ptr);
1197 pj_strcat2(&cfg, line);
1198 }
1199
1200 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001201 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001202 (int)config->udp_cfg.tls_setting.password.slen,
1203 config->udp_cfg.tls_setting.password.ptr);
1204 pj_strcat2(&cfg, line);
1205 }
1206
1207 if (config->udp_cfg.tls_setting.verify_server)
1208 pj_strcat2(&cfg, "--tls-verify-server\n");
1209
1210 if (config->udp_cfg.tls_setting.verify_client)
1211 pj_strcat2(&cfg, "--tls-verify-client\n");
1212
1213 if (config->udp_cfg.tls_setting.timeout.sec) {
1214 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001215 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001216 pj_strcat2(&cfg, line);
1217 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001218
1219 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1220
1221
1222 /* Media */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001223 if (config->media_cfg.enable_ice)
1224 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001225 if (config->null_audio)
1226 pj_strcat2(&cfg, "--null-audio\n");
1227 if (config->auto_play)
1228 pj_strcat2(&cfg, "--auto-play\n");
1229 if (config->auto_loop)
1230 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001231 if (config->auto_conf)
1232 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001233 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001234 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001235 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001236 pj_strcat2(&cfg, line);
1237 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001238 for (i=0; i<config->tone_count; ++i) {
1239 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1240 config->tones[i].freq1, config->tones[i].freq2,
1241 config->tones[i].on_msec, config->tones[i].off_msec);
1242 pj_strcat2(&cfg, line);
1243 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001244 if (config->rec_file.slen) {
1245 pj_ansi_sprintf(line, "--rec-file %s\n",
1246 config->rec_file.ptr);
1247 pj_strcat2(&cfg, line);
1248 }
1249 if (config->auto_rec)
1250 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001251 if (config->capture_dev != PJSUA_INVALID_ID) {
1252 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1253 pj_strcat2(&cfg, line);
1254 }
1255 if (config->playback_dev != PJSUA_INVALID_ID) {
1256 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1257 pj_strcat2(&cfg, line);
1258 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001259
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001260 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001261 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001262 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001263 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001264 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001265 } else {
1266 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1267 config->media_cfg.clock_rate);
1268 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001269 }
Benny Prijono70972992006-08-05 11:13:58 +00001270
1271 /* quality */
1272 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001274 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001275 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001276 } else {
1277 pj_ansi_sprintf(line, "#using default --quality %d\n",
1278 config->media_cfg.quality);
1279 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001280 }
Benny Prijono0498d902006-06-19 14:49:14 +00001281
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001282
1283 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001284 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001285 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001286 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001287 pj_strcat2(&cfg, line);
1288 }
1289
Benny Prijono70972992006-08-05 11:13:58 +00001290 /* no-vad */
1291 if (config->media_cfg.no_vad) {
1292 pj_strcat2(&cfg, "--no-vad\n");
1293 }
1294
1295 /* ec-tail */
1296 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1297 pj_ansi_sprintf(line, "--ec-tail %d\n",
1298 config->media_cfg.ec_tail_len);
1299 pj_strcat2(&cfg, line);
1300 } else {
1301 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1302 config->media_cfg.ec_tail_len);
1303 pj_strcat2(&cfg, line);
1304 }
1305
1306
1307 /* ilbc-mode */
1308 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1309 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1310 config->media_cfg.ilbc_mode);
1311 pj_strcat2(&cfg, line);
1312 } else {
1313 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1314 config->media_cfg.ilbc_mode);
1315 pj_strcat2(&cfg, line);
1316 }
1317
1318 /* RTP drop */
1319 if (config->media_cfg.tx_drop_pct) {
1320 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1321 config->media_cfg.tx_drop_pct);
1322 pj_strcat2(&cfg, line);
1323
1324 }
1325 if (config->media_cfg.rx_drop_pct) {
1326 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1327 config->media_cfg.rx_drop_pct);
1328 pj_strcat2(&cfg, line);
1329
1330 }
1331
1332
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001333 /* Start RTP port. */
1334 pj_ansi_sprintf(line, "--rtp-port %d\n",
1335 config->rtp_cfg.port);
1336 pj_strcat2(&cfg, line);
1337
1338 /* Add codec. */
1339 for (i=0; i<config->codec_cnt; ++i) {
1340 pj_ansi_sprintf(line, "--add-codec %s\n",
1341 config->codec_arg[i].ptr);
1342 pj_strcat2(&cfg, line);
1343 }
1344
1345 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1346
1347 /* Auto-answer. */
1348 if (config->auto_answer != 0) {
1349 pj_ansi_sprintf(line, "--auto-answer %d\n",
1350 config->auto_answer);
1351 pj_strcat2(&cfg, line);
1352 }
1353
1354 /* Max calls. */
1355 pj_ansi_sprintf(line, "--max-calls %d\n",
1356 config->cfg.max_calls);
1357 pj_strcat2(&cfg, line);
1358
1359 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001360 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001361 pj_ansi_sprintf(line, "--duration %d\n",
1362 config->duration);
1363 pj_strcat2(&cfg, line);
1364 }
1365
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001366 /* norefersub ? */
1367 if (config->no_refersub) {
1368 pj_strcat2(&cfg, "--norefersub\n");
1369 }
1370
1371
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001372 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1373
1374 /* Add buddies. */
1375 for (i=0; i<config->buddy_cnt; ++i) {
1376 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1377 (int)config->buddy_cfg[i].uri.slen,
1378 config->buddy_cfg[i].uri.ptr);
1379 pj_strcat2(&cfg, line);
1380 }
1381
1382
1383 *(cfg.ptr + cfg.slen) = '\0';
1384 return cfg.slen;
1385}
1386
1387
1388/*
1389 * Dump application states.
1390 */
1391static void app_dump(pj_bool_t detail)
1392{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001393 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001394}
1395
1396
1397/*****************************************************************************
1398 * Console application
1399 */
1400
1401/*
1402 * Find next call when current call is disconnected or when user
1403 * press ']'
1404 */
1405static pj_bool_t find_next_call(void)
1406{
1407 int i, max;
1408
1409 max = pjsua_call_get_max_count();
1410 for (i=current_call+1; i<max; ++i) {
1411 if (pjsua_call_is_active(i)) {
1412 current_call = i;
1413 return PJ_TRUE;
1414 }
1415 }
1416
1417 for (i=0; i<current_call; ++i) {
1418 if (pjsua_call_is_active(i)) {
1419 current_call = i;
1420 return PJ_TRUE;
1421 }
1422 }
1423
1424 current_call = PJSUA_INVALID_ID;
1425 return PJ_FALSE;
1426}
1427
1428
1429/*
1430 * Find previous call when user press '['
1431 */
1432static pj_bool_t find_prev_call(void)
1433{
1434 int i, max;
1435
1436 max = pjsua_call_get_max_count();
1437 for (i=current_call-1; i>=0; --i) {
1438 if (pjsua_call_is_active(i)) {
1439 current_call = i;
1440 return PJ_TRUE;
1441 }
1442 }
1443
1444 for (i=max-1; i>current_call; --i) {
1445 if (pjsua_call_is_active(i)) {
1446 current_call = i;
1447 return PJ_TRUE;
1448 }
1449 }
1450
1451 current_call = PJSUA_INVALID_ID;
1452 return PJ_FALSE;
1453}
1454
1455
Benny Prijono804ff0a2006-09-14 11:17:48 +00001456/* Callback from timer when the maximum call duration has been
1457 * exceeded.
1458 */
1459static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1460 struct pj_timer_entry *entry)
1461{
1462 pjsua_call_id call_id = entry->id;
1463 pjsua_msg_data msg_data;
1464 pjsip_generic_string_hdr warn;
1465 pj_str_t hname = pj_str("Warning");
1466 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1467
1468 PJ_UNUSED_ARG(timer_heap);
1469
Benny Prijono148c9dd2006-09-19 13:37:53 +00001470 if (call_id == PJSUA_INVALID_ID) {
1471 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1472 return;
1473 }
1474
Benny Prijono804ff0a2006-09-14 11:17:48 +00001475 /* Add warning header */
1476 pjsua_msg_data_init(&msg_data);
1477 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1478 pj_list_push_back(&msg_data.hdr_list, &warn);
1479
1480 /* Call duration has been exceeded; disconnect the call */
1481 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1482 "for call %d, disconnecting the call",
1483 app_config.duration, call_id));
1484 entry->id = PJSUA_INVALID_ID;
1485 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1486}
1487
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001488
1489/*
1490 * Handler when invite state has changed.
1491 */
1492static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1493{
1494 pjsua_call_info call_info;
1495
1496 PJ_UNUSED_ARG(e);
1497
1498 pjsua_call_get_info(call_id, &call_info);
1499
1500 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1501
Benny Prijono804ff0a2006-09-14 11:17:48 +00001502 /* Cancel duration timer, if any */
1503 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1504 struct call_data *cd = &app_config.call_data[call_id];
1505 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1506
1507 cd->timer.id = PJSUA_INVALID_ID;
1508 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1509 }
1510
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001511 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1512 call_id,
1513 call_info.last_status,
1514 call_info.last_status_text.ptr));
1515
1516 if (call_id == current_call) {
1517 find_next_call();
1518 }
1519
Benny Prijono4be63b52006-11-25 14:50:25 +00001520 /* Dump media state upon disconnected */
1521 if (1) {
1522 char buf[1024];
1523 pjsua_call_dump(call_id, PJ_TRUE, buf,
1524 sizeof(buf), " ");
1525 PJ_LOG(5,(THIS_FILE,
1526 "Call %d disconnected, dumping media stats\n%s",
1527 call_id, buf));
1528 }
1529
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001530 } else {
1531
Benny Prijono804ff0a2006-09-14 11:17:48 +00001532 if (app_config.duration!=NO_LIMIT &&
1533 call_info.state == PJSIP_INV_STATE_CONFIRMED)
1534 {
1535 /* Schedule timer to hangup call after the specified duration */
1536 struct call_data *cd = &app_config.call_data[call_id];
1537 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1538 pj_time_val delay;
1539
1540 cd->timer.id = call_id;
1541 delay.sec = app_config.duration;
1542 delay.msec = 0;
1543 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
1544 }
1545
Benny Prijono4be63b52006-11-25 14:50:25 +00001546 if (call_info.state == PJSIP_INV_STATE_EARLY) {
1547 int code;
1548 pj_str_t reason;
1549 pjsip_msg *msg;
1550
1551 /* This can only occur because of TX or RX message */
1552 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
1553
1554 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1555 msg = e->body.tsx_state.src.rdata->msg_info.msg;
1556 } else {
1557 msg = e->body.tsx_state.src.tdata->msg;
1558 }
1559
1560 code = msg->line.status.code;
1561 reason = msg->line.status.reason;
1562
1563 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
1564 call_id, call_info.state_text.ptr,
1565 code, (int)reason.slen, reason.ptr));
1566 } else {
1567 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
1568 call_id,
1569 call_info.state_text.ptr));
1570 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001571
1572 if (current_call==PJSUA_INVALID_ID)
1573 current_call = call_id;
1574
1575 }
1576}
1577
1578
1579/**
1580 * Handler when there is incoming call.
1581 */
1582static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
1583 pjsip_rx_data *rdata)
1584{
1585 pjsua_call_info call_info;
1586
1587 PJ_UNUSED_ARG(acc_id);
1588 PJ_UNUSED_ARG(rdata);
1589
1590 pjsua_call_get_info(call_id, &call_info);
1591
1592 if (app_config.auto_answer > 0) {
1593 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
1594 }
1595
1596 if (app_config.auto_answer < 200) {
1597 PJ_LOG(3,(THIS_FILE,
1598 "Incoming call for account %d!\n"
1599 "From: %s\n"
1600 "To: %s\n"
1601 "Press a to answer or h to reject call",
1602 acc_id,
1603 call_info.remote_info.ptr,
1604 call_info.local_info.ptr));
1605 }
1606}
1607
1608
1609/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00001610 * Handler when a transaction within a call has changed state.
1611 */
1612static void on_call_tsx_state(pjsua_call_id call_id,
1613 pjsip_transaction *tsx,
1614 pjsip_event *e)
1615{
1616 const pjsip_method info_method =
1617 {
1618 PJSIP_OTHER_METHOD,
1619 { "INFO", 4 }
1620 };
1621
1622 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
1623 /*
1624 * Handle INFO method.
1625 */
1626 if (tsx->role == PJSIP_ROLE_UAC &&
1627 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00001628 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
1629 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00001630 {
1631 /* Status of outgoing INFO request */
1632 if (tsx->status_code >= 200 && tsx->status_code < 300) {
1633 PJ_LOG(4,(THIS_FILE,
1634 "Call %d: DTMF sent successfully with INFO",
1635 call_id));
1636 } else if (tsx->status_code >= 300) {
1637 PJ_LOG(4,(THIS_FILE,
1638 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
1639 call_id,
1640 tsx->status_code,
1641 (int)tsx->status_text.slen,
1642 tsx->status_text.ptr));
1643 }
1644 } else if (tsx->role == PJSIP_ROLE_UAS &&
1645 tsx->state == PJSIP_TSX_STATE_TRYING)
1646 {
1647 /* Answer incoming INFO with 200/OK */
1648 pjsip_rx_data *rdata;
1649 pjsip_tx_data *tdata;
1650 pj_status_t status;
1651
1652 rdata = e->body.tsx_state.src.rdata;
1653
1654 if (rdata->msg_info.msg->body) {
1655 status = pjsip_endpt_create_response(tsx->endpt, rdata,
1656 200, NULL, &tdata);
1657 if (status == PJ_SUCCESS)
1658 status = pjsip_tsx_send_msg(tsx, tdata);
1659
1660 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
1661 call_id,
1662 (int)rdata->msg_info.msg->body->len,
1663 rdata->msg_info.msg->body->data));
1664 } else {
1665 status = pjsip_endpt_create_response(tsx->endpt, rdata,
1666 400, NULL, &tdata);
1667 if (status == PJ_SUCCESS)
1668 status = pjsip_tsx_send_msg(tsx, tdata);
1669 }
1670 }
1671 }
1672}
1673
1674
1675/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001676 * Callback on media state changed event.
1677 * The action may connect the call to sound device, to file, or
1678 * to loop the call.
1679 */
1680static void on_call_media_state(pjsua_call_id call_id)
1681{
1682 pjsua_call_info call_info;
1683
1684 pjsua_call_get_info(call_id, &call_info);
1685
1686 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
1687 pj_bool_t connect_sound = PJ_TRUE;
1688
1689 /* Loopback sound, if desired */
1690 if (app_config.auto_loop) {
1691 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
1692 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00001693
1694 /* Automatically record conversation, if desired */
1695 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1696 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1697 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001698 }
1699
1700 /* Stream a file, if desired */
1701 if (app_config.auto_play && app_config.wav_port != PJSUA_INVALID_ID) {
1702 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
1703 connect_sound = PJ_FALSE;
1704 }
1705
Benny Prijono7ca96da2006-08-07 12:11:40 +00001706 /* Put call in conference with other calls, if desired */
1707 if (app_config.auto_conf) {
1708 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00001709 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00001710 unsigned i;
1711
1712 /* Get all calls, and establish media connection between
1713 * this call and other calls.
1714 */
1715 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00001716
Benny Prijono7ca96da2006-08-07 12:11:40 +00001717 for (i=0; i<call_cnt; ++i) {
1718 if (call_ids[i] == call_id)
1719 continue;
1720
1721 if (!pjsua_call_has_media(call_ids[i]))
1722 continue;
1723
1724 pjsua_conf_connect(call_info.conf_slot,
1725 pjsua_call_get_conf_port(call_ids[i]));
1726 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1727 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001728
1729 /* Automatically record conversation, if desired */
1730 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1731 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1732 app_config.rec_port);
1733 }
1734
Benny Prijono7ca96da2006-08-07 12:11:40 +00001735 }
1736
1737 /* Also connect call to local sound device */
1738 connect_sound = PJ_TRUE;
1739 }
1740
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001741 /* Otherwise connect to sound device */
1742 if (connect_sound) {
1743 pjsua_conf_connect(call_info.conf_slot, 0);
1744 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001745
1746 /* Automatically record conversation, if desired */
1747 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1748 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1749 pjsua_conf_connect(0, app_config.rec_port);
1750 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001751 }
1752
1753 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
1754
1755 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
1756 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
1757 call_id));
1758 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
1759 PJ_LOG(3,(THIS_FILE,
1760 "Media for call %d is suspended (hold) by remote",
1761 call_id));
Benny Prijono096c56c2007-09-15 08:30:16 +00001762 } else if (call_info.media_status == PJSUA_CALL_MEDIA_ERROR) {
1763 pj_str_t reason = pj_str("ICE negotiation failed");
1764
1765 PJ_LOG(1,(THIS_FILE,
1766 "Media has reported error, disconnecting call"));
1767
1768 pjsua_call_hangup(call_id, 500, &reason, NULL);
1769
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001770 } else {
1771 PJ_LOG(3,(THIS_FILE,
1772 "Media for call %d is inactive",
1773 call_id));
1774 }
1775}
1776
Benny Prijono0875ae82006-12-26 00:11:48 +00001777/*
1778 * DTMF callback.
1779 */
1780static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
1781{
1782 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
1783}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001784
1785/*
1786 * Handler registration status has changed.
1787 */
1788static void on_reg_state(pjsua_acc_id acc_id)
1789{
1790 PJ_UNUSED_ARG(acc_id);
1791
1792 // Log already written.
1793}
1794
1795
1796/*
1797 * Handler on buddy state changed.
1798 */
1799static void on_buddy_state(pjsua_buddy_id buddy_id)
1800{
1801 pjsua_buddy_info info;
1802 pjsua_buddy_get_info(buddy_id, &info);
1803
1804 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
1805 (int)info.uri.slen,
1806 info.uri.ptr,
1807 (int)info.status_text.slen,
1808 info.status_text.ptr));
1809}
1810
1811
1812/**
1813 * Incoming IM message (i.e. MESSAGE request)!
1814 */
1815static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
1816 const pj_str_t *to, const pj_str_t *contact,
1817 const pj_str_t *mime_type, const pj_str_t *text)
1818{
1819 /* Note: call index may be -1 */
1820 PJ_UNUSED_ARG(call_id);
1821 PJ_UNUSED_ARG(to);
1822 PJ_UNUSED_ARG(contact);
1823 PJ_UNUSED_ARG(mime_type);
1824
Benny Prijonof4b538d2007-05-14 16:45:20 +00001825 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001826 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00001827 (int)text->slen, text->ptr,
1828 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001829}
1830
1831
1832/**
1833 * Received typing indication
1834 */
1835static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
1836 const pj_str_t *to, const pj_str_t *contact,
1837 pj_bool_t is_typing)
1838{
1839 PJ_UNUSED_ARG(call_id);
1840 PJ_UNUSED_ARG(to);
1841 PJ_UNUSED_ARG(contact);
1842
1843 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
1844 (int)from->slen, from->ptr,
1845 (is_typing?"is typing..":"has stopped typing")));
1846}
1847
1848
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001849/**
1850 * Call transfer request status.
1851 */
1852static void on_call_transfer_status(pjsua_call_id call_id,
1853 int status_code,
1854 const pj_str_t *status_text,
1855 pj_bool_t final,
1856 pj_bool_t *p_cont)
1857{
1858 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
1859 call_id, status_code,
1860 (int)status_text->slen, status_text->ptr,
1861 (final ? "[final]" : "")));
1862
1863 if (status_code/100 == 2) {
1864 PJ_LOG(3,(THIS_FILE,
1865 "Call %d: call transfered successfully, disconnecting call",
1866 call_id));
1867 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
1868 *p_cont = PJ_FALSE;
1869 }
1870}
1871
1872
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001873/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00001874 * Notification that call is being replaced.
1875 */
1876static void on_call_replaced(pjsua_call_id old_call_id,
1877 pjsua_call_id new_call_id)
1878{
1879 pjsua_call_info old_ci, new_ci;
1880
1881 pjsua_call_get_info(old_call_id, &old_ci);
1882 pjsua_call_get_info(new_call_id, &new_ci);
1883
1884 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
1885 "call %d with %.*s",
1886 old_call_id,
1887 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
1888 new_call_id,
1889 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
1890}
1891
1892
1893/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00001894 * NAT type detection callback.
1895 */
1896static void on_nat_detect(const pj_stun_nat_detect_result *res)
1897{
1898 if (res->status != PJ_SUCCESS) {
1899 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
1900 } else {
1901 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
1902 }
1903}
1904
1905
1906/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001907 * Print buddy list.
1908 */
1909static void print_buddy_list(void)
1910{
1911 pjsua_buddy_id ids[64];
1912 int i;
1913 unsigned count = PJ_ARRAY_SIZE(ids);
1914
1915 puts("Buddy list:");
1916
1917 pjsua_enum_buddies(ids, &count);
1918
1919 if (count == 0)
1920 puts(" -none-");
1921 else {
1922 for (i=0; i<(int)count; ++i) {
1923 pjsua_buddy_info info;
1924
1925 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
1926 continue;
1927
Benny Prijono4461c7d2007-08-25 13:36:15 +00001928 printf(" [%2d] <%.*s> %.*s\n",
1929 ids[i]+1,
1930 (int)info.status_text.slen,
1931 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001932 (int)info.uri.slen,
1933 info.uri.ptr);
1934 }
1935 }
1936 puts("");
1937}
1938
1939
1940/*
1941 * Print account status.
1942 */
1943static void print_acc_status(int acc_id)
1944{
1945 char buf[80];
1946 pjsua_acc_info info;
1947
1948 pjsua_acc_get_info(acc_id, &info);
1949
1950 if (!info.has_registration) {
1951 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
1952 (int)info.status_text.slen,
1953 info.status_text.ptr);
1954
1955 } else {
1956 pj_ansi_snprintf(buf, sizeof(buf),
1957 "%d/%.*s (expires=%d)",
1958 info.status,
1959 (int)info.status_text.slen,
1960 info.status_text.ptr,
1961 info.expires);
1962
1963 }
1964
1965 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
1966 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00001967 printf(" Online status: %.*s\n",
1968 (int)info.online_status_text.slen,
1969 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001970}
1971
1972
1973/*
1974 * Show a bit of help.
1975 */
1976static void keystroke_help(void)
1977{
1978 pjsua_acc_id acc_ids[16];
1979 unsigned count = PJ_ARRAY_SIZE(acc_ids);
1980 int i;
1981
1982 printf(">>>>\n");
1983
1984 pjsua_enum_accs(acc_ids, &count);
1985
1986 printf("Account list:\n");
1987 for (i=0; i<(int)count; ++i)
1988 print_acc_status(acc_ids[i]);
1989
1990 print_buddy_list();
1991
1992 //puts("Commands:");
1993 puts("+=============================================================================+");
1994 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
1995 puts("| | | |");
1996 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
1997 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00001998 puts("| a Answer call | i Send IM | !a Modify accnt. |");
1999 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2000 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2001 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002002 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2003 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002004 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002005 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002006 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2007 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2008 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2009 puts("| | V Adjust audio Volume | f Save config |");
2010 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002011 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijono990042e2007-01-21 19:36:00 +00002012 puts("| q QUIT sleep N: console sleep for N ms |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002013 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002014
2015 i = pjsua_call_get_count();
2016 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002017
2018 if (current_call != PJSUA_INVALID_ID) {
2019 pjsua_call_info ci;
2020 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2021 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2022 (int)ci.remote_info.slen, ci.remote_info.ptr,
2023 (int)ci.state_text.slen, ci.state_text.ptr);
2024 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002025}
2026
2027
2028/*
2029 * Input simple string
2030 */
2031static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2032{
2033 char *p;
2034
2035 printf("%s (empty to cancel): ", title); fflush(stdout);
2036 fgets(buf, len, stdin);
2037
2038 /* Remove trailing newlines. */
2039 for (p=buf; ; ++p) {
2040 if (*p=='\r' || *p=='\n') *p='\0';
2041 else if (!*p) break;
2042 }
2043
2044 if (!*buf)
2045 return PJ_FALSE;
2046
2047 return PJ_TRUE;
2048}
2049
2050
2051#define NO_NB -2
2052struct input_result
2053{
2054 int nb_result;
2055 char *uri_result;
2056};
2057
2058
2059/*
2060 * Input URL.
2061 */
2062static void ui_input_url(const char *title, char *buf, int len,
2063 struct input_result *result)
2064{
2065 result->nb_result = NO_NB;
2066 result->uri_result = NULL;
2067
2068 print_buddy_list();
2069
2070 printf("Choices:\n"
2071 " 0 For current dialog.\n"
2072 " -1 All %d buddies in buddy list\n"
2073 " [1 -%2d] Select from buddy list\n"
2074 " URL An URL\n"
2075 " <Enter> Empty input (or 'q') to cancel\n"
2076 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2077 printf("%s: ", title);
2078
2079 fflush(stdout);
2080 fgets(buf, len, stdin);
2081 len = strlen(buf);
2082
2083 /* Left trim */
2084 while (pj_isspace(*buf)) {
2085 ++buf;
2086 --len;
2087 }
2088
2089 /* Remove trailing newlines */
2090 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2091 buf[--len] = '\0';
2092
2093 if (len == 0 || buf[0]=='q')
2094 return;
2095
2096 if (pj_isdigit(*buf) || *buf=='-') {
2097
2098 int i;
2099
2100 if (*buf=='-')
2101 i = 1;
2102 else
2103 i = 0;
2104
2105 for (; i<len; ++i) {
2106 if (!pj_isdigit(buf[i])) {
2107 puts("Invalid input");
2108 return;
2109 }
2110 }
2111
2112 result->nb_result = my_atoi(buf);
2113
2114 if (result->nb_result >= 0 &&
2115 result->nb_result <= (int)pjsua_get_buddy_count())
2116 {
2117 return;
2118 }
2119 if (result->nb_result == -1)
2120 return;
2121
2122 puts("Invalid input");
2123 result->nb_result = NO_NB;
2124 return;
2125
2126 } else {
2127 pj_status_t status;
2128
2129 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2130 pjsua_perror(THIS_FILE, "Invalid URL", status);
2131 return;
2132 }
2133
2134 result->uri_result = buf;
2135 }
2136}
2137
2138/*
2139 * List the ports in conference bridge
2140 */
2141static void conf_list(void)
2142{
2143 unsigned i, count;
2144 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2145
2146 printf("Conference ports:\n");
2147
2148 count = PJ_ARRAY_SIZE(id);
2149 pjsua_enum_conf_ports(id, &count);
2150
2151 for (i=0; i<count; ++i) {
2152 char txlist[PJSUA_MAX_CALLS*4+10];
2153 unsigned j;
2154 pjsua_conf_port_info info;
2155
2156 pjsua_conf_get_port_info(id[i], &info);
2157
2158 txlist[0] = '\0';
2159 for (j=0; j<info.listener_cnt; ++j) {
2160 char s[10];
2161 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2162 pj_ansi_strcat(txlist, s);
2163 }
2164 printf("Port #%02d[%2dKHz/%dms] %20.*s transmitting to: %s\n",
2165 info.slot_id,
2166 info.clock_rate/1000,
2167 info.samples_per_frame * 1000 / info.clock_rate,
2168 (int)info.name.slen,
2169 info.name.ptr,
2170 txlist);
2171
2172 }
2173 puts("");
2174}
2175
2176
2177/*
Benny Prijono56315612006-07-18 14:39:40 +00002178 * Send arbitrary request to remote host
2179 */
2180static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2181{
2182 pj_str_t str_method;
2183 pjsip_method method;
2184 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002185 pjsip_endpoint *endpt;
2186 pj_status_t status;
2187
2188 endpt = pjsua_get_pjsip_endpt();
2189
2190 str_method = pj_str(cstr_method);
2191 pjsip_method_init_np(&method, &str_method);
2192
Benny Prijonofff245c2007-04-02 11:44:47 +00002193 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002194
2195 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
2196 if (status != PJ_SUCCESS) {
2197 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002198 return;
2199 }
2200}
2201
2202
2203/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002204 * Change extended online status.
2205 */
2206static void change_online_status(void)
2207{
2208 char menuin[32];
2209 pj_bool_t online_status;
2210 pjrpid_element elem;
2211 int i, choice;
2212
2213 enum {
2214 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2215 };
2216
2217 struct opt {
2218 int id;
2219 char *name;
2220 } opts[] = {
2221 { AVAILABLE, "Available" },
2222 { BUSY, "Busy"},
2223 { OTP, "On the phone"},
2224 { IDLE, "Idle"},
2225 { AWAY, "Away"},
2226 { BRB, "Be right back"},
2227 { OFFLINE, "Offline"}
2228 };
2229
2230 printf("\n"
2231 "Choices:\n");
2232 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2233 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2234 }
2235
2236 if (!simple_input("Select status", menuin, sizeof(menuin)))
2237 return;
2238
2239 choice = atoi(menuin) - 1;
2240 if (choice < 0 || choice >= OPT_MAX) {
2241 puts("Invalid selection");
2242 return;
2243 }
2244
2245 pj_bzero(&elem, sizeof(elem));
2246 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2247
2248 online_status = PJ_TRUE;
2249
2250 switch (choice) {
2251 case AVAILABLE:
2252 break;
2253 case BUSY:
2254 elem.activity = PJRPID_ACTIVITY_BUSY;
2255 elem.note = pj_str("Busy");
2256 break;
2257 case OTP:
2258 elem.activity = PJRPID_ACTIVITY_BUSY;
2259 elem.note = pj_str("On the phone");
2260 break;
2261 case IDLE:
2262 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2263 elem.note = pj_str("Idle");
2264 break;
2265 case AWAY:
2266 elem.activity = PJRPID_ACTIVITY_AWAY;
2267 elem.note = pj_str("Away");
2268 break;
2269 case BRB:
2270 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2271 elem.note = pj_str("Be right back");
2272 break;
2273 case OFFLINE:
2274 online_status = PJ_FALSE;
2275 break;
2276 }
2277
2278 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2279}
2280
2281
2282/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002283 * Change codec priorities.
2284 */
2285static void manage_codec_prio(void)
2286{
2287 pjsua_codec_info c[32];
2288 unsigned i, count = PJ_ARRAY_SIZE(c);
2289 char input[32];
2290 char *codec, *prio;
2291 pj_str_t id;
2292 int new_prio;
2293 pj_status_t status;
2294
2295 printf("List of codecs:\n");
2296
2297 pjsua_enum_codecs(c, &count);
2298 for (i=0; i<count; ++i) {
2299 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2300 c[i].codec_id.ptr);
2301 }
2302
2303 puts("");
2304 puts("Enter codec name and its new priority (e.g. \"speex/16000 200\"), empty to cancel:");
2305
2306 printf("Codec name and priority: ");
2307 fgets(input, sizeof(input), stdin);
2308 if (input[0]=='\r' || input[0]=='\n') {
2309 puts("Done");
2310 return;
2311 }
2312
2313 codec = strtok(input, " \t\r\n");
2314 prio = strtok(NULL, " \r\n");
2315
2316 if (!codec || !prio) {
2317 puts("Invalid input");
2318 return;
2319 }
2320
2321 new_prio = atoi(prio);
2322 if (new_prio < 0)
2323 new_prio = 0;
2324 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2325 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2326
2327 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2328 (pj_uint8_t)new_prio);
2329 if (status != PJ_SUCCESS)
2330 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2331}
2332
2333
2334/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002335 * Main "user interface" loop.
2336 */
2337void console_app_main(const pj_str_t *uri_to_call)
2338{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002339 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002340 char buf[128];
2341 char text[128];
2342 int i, count;
2343 char *uri;
2344 pj_str_t tmp;
2345 struct input_result result;
2346 pjsua_call_info call_info;
2347 pjsua_acc_info acc_info;
2348
2349
2350 /* If user specifies URI to call, then call the URI */
2351 if (uri_to_call->slen) {
2352 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2353 }
2354
2355 keystroke_help();
2356
2357 for (;;) {
2358
2359 printf(">>> ");
2360 fflush(stdout);
2361
Benny Prijono990042e2007-01-21 19:36:00 +00002362 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2363 /*
2364 * Be friendly to users who redirect commands into
2365 * program, when file ends, resume with kbd.
2366 * If exit is desired end script with q for quit
2367 */
2368 /* Reopen stdin/stdout/stderr to /dev/console */
2369#if defined(PJ_WIN32) && PJ_WIN32!=0
2370 if (freopen ("CONIN$", "r", stdin) == NULL) {
2371#else
2372 if (1) {
2373#endif
2374 puts("Cannot switch back to console from file redirection");
2375 menuin[0] = 'q';
2376 menuin[1] = '\0';
2377 } else {
2378 puts("Switched back to console from file redirection");
2379 continue;
2380 }
2381 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002382
2383 switch (menuin[0]) {
2384
2385 case 'm':
2386 /* Make call! : */
2387 printf("(You currently have %d calls)\n",
2388 pjsua_call_get_count());
2389
2390 uri = NULL;
2391 ui_input_url("Make call", buf, sizeof(buf), &result);
2392 if (result.nb_result != NO_NB) {
2393
2394 if (result.nb_result == -1 || result.nb_result == 0) {
2395 puts("You can't do that with make call!");
2396 continue;
2397 } else {
2398 pjsua_buddy_info binfo;
2399 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2400 uri = binfo.uri.ptr;
2401 }
2402
2403 } else if (result.uri_result) {
2404 uri = result.uri_result;
2405 }
2406
2407 tmp = pj_str(uri);
2408 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2409 break;
2410
2411 case 'M':
2412 /* Make multiple calls! : */
2413 printf("(You currently have %d calls)\n",
2414 pjsua_call_get_count());
2415
2416 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2417 continue;
2418
2419 count = my_atoi(menuin);
2420 if (count < 1)
2421 continue;
2422
2423 ui_input_url("Make call", buf, sizeof(buf), &result);
2424 if (result.nb_result != NO_NB) {
2425 pjsua_buddy_info binfo;
2426 if (result.nb_result == -1 || result.nb_result == 0) {
2427 puts("You can't do that with make call!");
2428 continue;
2429 }
2430 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2431 uri = binfo.uri.ptr;
2432 } else {
2433 uri = result.uri_result;
2434 }
2435
2436 for (i=0; i<my_atoi(menuin); ++i) {
2437 pj_status_t status;
2438
2439 tmp = pj_str(uri);
2440 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2441 NULL, NULL);
2442 if (status != PJ_SUCCESS)
2443 break;
2444 }
2445 break;
2446
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002447 case 'n':
Benny Prijono6ba8c542007-10-16 01:34:14 +00002448 pjsua_detect_nat_type();
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002449 break;
2450
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002451 case 'i':
2452 /* Send instant messaeg */
2453
2454 /* i is for call index to send message, if any */
2455 i = -1;
2456
2457 /* Make compiler happy. */
2458 uri = NULL;
2459
2460 /* Input destination. */
2461 ui_input_url("Send IM to", buf, sizeof(buf), &result);
2462 if (result.nb_result != NO_NB) {
2463
2464 if (result.nb_result == -1) {
2465 puts("You can't send broadcast IM like that!");
2466 continue;
2467
2468 } else if (result.nb_result == 0) {
2469
2470 i = current_call;
2471
2472 } else {
2473 pjsua_buddy_info binfo;
2474 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2475 uri = binfo.uri.ptr;
2476 }
2477
2478 } else if (result.uri_result) {
2479 uri = result.uri_result;
2480 }
2481
2482
2483 /* Send typing indication. */
2484 if (i != -1)
2485 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
2486 else {
2487 pj_str_t tmp_uri = pj_str(uri);
2488 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
2489 }
2490
2491 /* Input the IM . */
2492 if (!simple_input("Message", text, sizeof(text))) {
2493 /*
2494 * Cancelled.
2495 * Send typing notification too, saying we're not typing.
2496 */
2497 if (i != -1)
2498 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
2499 else {
2500 pj_str_t tmp_uri = pj_str(uri);
2501 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
2502 }
2503 continue;
2504 }
2505
2506 tmp = pj_str(text);
2507
2508 /* Send the IM */
2509 if (i != -1)
2510 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
2511 else {
2512 pj_str_t tmp_uri = pj_str(uri);
2513 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
2514 }
2515
2516 break;
2517
2518 case 'a':
2519
2520 if (current_call != -1) {
2521 pjsua_call_get_info(current_call, &call_info);
2522 } else {
2523 /* Make compiler happy */
2524 call_info.role = PJSIP_ROLE_UAC;
2525 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
2526 }
2527
2528 if (current_call == -1 ||
2529 call_info.role != PJSIP_ROLE_UAS ||
2530 call_info.state >= PJSIP_INV_STATE_CONNECTING)
2531 {
2532 puts("No pending incoming call");
2533 fflush(stdout);
2534 continue;
2535
2536 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00002537 int st_code;
2538 char contact[120];
2539 pj_str_t hname = { "Contact", 7 };
2540 pj_str_t hvalue;
2541 pjsip_generic_string_hdr hcontact;
2542 pjsua_msg_data msg_data;
2543
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002544 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
2545 continue;
2546
Benny Prijono20d36722007-02-22 14:52:24 +00002547 st_code = my_atoi(buf);
2548 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002549 continue;
2550
Benny Prijono20d36722007-02-22 14:52:24 +00002551 pjsua_msg_data_init(&msg_data);
2552
2553 if (st_code/100 == 3) {
2554 if (!simple_input("Enter URL to be put in Contact",
2555 contact, sizeof(contact)))
2556 continue;
2557 hvalue = pj_str(contact);
2558 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
2559
2560 pj_list_push_back(&msg_data.hdr_list, &hcontact);
2561 }
2562
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002563 /*
2564 * Must check again!
2565 * Call may have been disconnected while we're waiting for
2566 * keyboard input.
2567 */
2568 if (current_call == -1) {
2569 puts("Call has been disconnected");
2570 fflush(stdout);
2571 continue;
2572 }
2573
Benny Prijono20d36722007-02-22 14:52:24 +00002574 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002575 }
2576
2577 break;
2578
2579
2580 case 'h':
2581
2582 if (current_call == -1) {
2583 puts("No current call");
2584 fflush(stdout);
2585 continue;
2586
2587 } else if (menuin[1] == 'a') {
2588
2589 /* Hangup all calls */
2590 pjsua_call_hangup_all();
2591
2592 } else {
2593
2594 /* Hangup current calls */
2595 pjsua_call_hangup(current_call, 0, NULL, NULL);
2596 }
2597 break;
2598
2599 case ']':
2600 case '[':
2601 /*
2602 * Cycle next/prev dialog.
2603 */
2604 if (menuin[0] == ']') {
2605 find_next_call();
2606
2607 } else {
2608 find_prev_call();
2609 }
2610
2611 if (current_call != -1) {
2612
2613 pjsua_call_get_info(current_call, &call_info);
2614 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
2615 (int)call_info.remote_info.slen,
2616 call_info.remote_info.ptr));
2617
2618 } else {
2619 PJ_LOG(3,(THIS_FILE,"No current dialog"));
2620 }
2621 break;
2622
2623
2624 case '>':
2625 case '<':
2626 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
2627 break;
2628
2629 i = my_atoi(buf);
2630 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00002631 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002632 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
2633 } else {
2634 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
2635 }
2636 break;
2637
2638
2639 case '+':
2640 if (menuin[1] == 'b') {
2641
2642 pjsua_buddy_config buddy_cfg;
2643 pjsua_buddy_id buddy_id;
2644 pj_status_t status;
2645
2646 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
2647 break;
2648
2649 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
2650 printf("Invalid SIP URI '%s'\n", buf);
2651 break;
2652 }
2653
Benny Prijonoac623b32006-07-03 15:19:31 +00002654 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002655
2656 buddy_cfg.uri = pj_str(buf);
2657 buddy_cfg.subscribe = PJ_TRUE;
2658
2659 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
2660 if (status == PJ_SUCCESS) {
2661 printf("New buddy '%s' added at index %d\n",
2662 buf, buddy_id+1);
2663 }
2664
2665 } else if (menuin[1] == 'a') {
2666
Benny Prijonofb2b3652007-06-28 07:15:03 +00002667 char id[80], registrar[80], realm[80], uname[80], passwd[30];
2668 pjsua_acc_config acc_cfg;
2669 pj_status_t status;
2670
2671 if (!simple_input("Your SIP URL:", id, sizeof(id)))
2672 break;
2673 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
2674 break;
2675 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
2676 break;
2677 if (!simple_input("Auth Username:", uname, sizeof(uname)))
2678 break;
2679 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
2680 break;
2681
2682 pjsua_acc_config_default(&acc_cfg);
2683 acc_cfg.id = pj_str(id);
2684 acc_cfg.reg_uri = pj_str(registrar);
2685 acc_cfg.cred_count = 1;
2686 acc_cfg.cred_info[0].scheme = pj_str("digest");
2687 acc_cfg.cred_info[0].realm = pj_str(realm);
2688 acc_cfg.cred_info[0].username = pj_str(uname);
2689 acc_cfg.cred_info[0].data_type = 0;
2690 acc_cfg.cred_info[0].data = pj_str(passwd);
2691
2692 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
2693 if (status != PJ_SUCCESS) {
2694 pjsua_perror(THIS_FILE, "Error adding new account", status);
2695 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002696
2697 } else {
2698 printf("Invalid input %s\n", menuin);
2699 }
2700 break;
2701
2702 case '-':
2703 if (menuin[1] == 'b') {
2704 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
2705 break;
2706
2707 i = my_atoi(buf) - 1;
2708
2709 if (!pjsua_buddy_is_valid(i)) {
2710 printf("Invalid buddy id %d\n", i);
2711 } else {
2712 pjsua_buddy_del(i);
2713 printf("Buddy %d deleted\n", i);
2714 }
2715
2716 } else if (menuin[1] == 'a') {
2717
2718 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
2719 break;
2720
2721 i = my_atoi(buf);
2722
2723 if (!pjsua_acc_is_valid(i)) {
2724 printf("Invalid account id %d\n", i);
2725 } else {
2726 pjsua_acc_del(i);
2727 printf("Account %d deleted\n", i);
2728 }
2729
2730 } else {
2731 printf("Invalid input %s\n", menuin);
2732 }
2733 break;
2734
2735 case 'H':
2736 /*
2737 * Hold call.
2738 */
2739 if (current_call != -1) {
2740
2741 pjsua_call_set_hold(current_call, NULL);
2742
2743 } else {
2744 PJ_LOG(3,(THIS_FILE, "No current call"));
2745 }
2746 break;
2747
2748 case 'v':
2749 /*
2750 * Send re-INVITE (to release hold, etc).
2751 */
2752 if (current_call != -1) {
2753
2754 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
2755
2756 } else {
2757 PJ_LOG(3,(THIS_FILE, "No current call"));
2758 }
2759 break;
2760
Benny Prijonoc08682e2007-10-04 06:17:58 +00002761 case 'U':
2762 /*
2763 * Send UPDATE
2764 */
2765 if (current_call != -1) {
2766
2767 pjsua_call_update(current_call, 0, NULL);
2768
2769 } else {
2770 PJ_LOG(3,(THIS_FILE, "No current call"));
2771 }
2772 break;
2773
2774 case 'C':
2775 if (menuin[1] == 'p') {
2776 manage_codec_prio();
2777 }
2778 break;
2779
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002780 case 'x':
2781 /*
2782 * Transfer call.
2783 */
2784 if (current_call == -1) {
2785
2786 PJ_LOG(3,(THIS_FILE, "No current call"));
2787
2788 } else {
2789 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00002790 pjsua_msg_data msg_data;
2791 pjsip_generic_string_hdr refer_sub;
2792 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2793 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00002794 pjsua_call_info ci;
2795
2796 pjsua_call_get_info(current_call, &ci);
2797 printf("Transfering current call [%d] %.*s\n",
2798 current_call,
2799 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002800
2801 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
2802
2803 /* Check if call is still there. */
2804
2805 if (call != current_call) {
2806 puts("Call has been disconnected");
2807 continue;
2808 }
2809
Benny Prijonod524e822006-09-22 12:48:18 +00002810 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002811 if (app_config.no_refersub) {
2812 /* Add Refer-Sub: false in outgoing REFER request */
2813 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
2814 &STR_FALSE);
2815 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
2816 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002817 if (result.nb_result != NO_NB) {
2818 if (result.nb_result == -1 || result.nb_result == 0)
2819 puts("You can't do that with transfer call!");
2820 else {
2821 pjsua_buddy_info binfo;
2822 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00002823 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002824 }
2825
2826 } else if (result.uri_result) {
2827 pj_str_t tmp;
2828 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00002829 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002830 }
2831 }
2832 break;
2833
Benny Prijonof7b1c392006-11-11 16:46:34 +00002834 case 'X':
2835 /*
2836 * Transfer call with replaces.
2837 */
2838 if (current_call == -1) {
2839
2840 PJ_LOG(3,(THIS_FILE, "No current call"));
2841
2842 } else {
2843 int call = current_call;
2844 int dst_call;
2845 pjsua_msg_data msg_data;
2846 pjsip_generic_string_hdr refer_sub;
2847 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2848 pj_str_t STR_FALSE = { "false", 5 };
2849 pjsua_call_id ids[PJSUA_MAX_CALLS];
2850 pjsua_call_info ci;
2851 unsigned i, count;
2852
2853 count = PJ_ARRAY_SIZE(ids);
2854 pjsua_enum_calls(ids, &count);
2855
2856 if (count <= 1) {
2857 puts("There are no other calls");
2858 continue;
2859 }
2860
2861 pjsua_call_get_info(current_call, &ci);
2862 printf("Transfer call [%d] %.*s to one of the following:\n",
2863 current_call,
2864 (int)ci.remote_info.slen, ci.remote_info.ptr);
2865
2866 for (i=0; i<count; ++i) {
2867 pjsua_call_info call_info;
2868
2869 if (ids[i] == call)
2870 continue;
2871
2872 pjsua_call_get_info(ids[i], &call_info);
2873 printf("%d %.*s [%.*s]\n",
2874 ids[i],
2875 (int)call_info.remote_info.slen,
2876 call_info.remote_info.ptr,
2877 (int)call_info.state_text.slen,
2878 call_info.state_text.ptr);
2879 }
2880
2881 if (!simple_input("Enter call number to be replaced",
2882 buf, sizeof(buf)))
2883 continue;
2884
2885 dst_call = my_atoi(buf);
2886
2887 /* Check if call is still there. */
2888
2889 if (call != current_call) {
2890 puts("Call has been disconnected");
2891 continue;
2892 }
2893
2894 /* Check that destination call is valid. */
2895 if (dst_call == call) {
2896 puts("Destination call number must not be the same "
2897 "as the call being transfered");
2898 continue;
2899 }
2900 if (dst_call >= PJSUA_MAX_CALLS) {
2901 puts("Invalid destination call number");
2902 continue;
2903 }
2904 if (!pjsua_call_is_active(dst_call)) {
2905 puts("Invalid destination call number");
2906 continue;
2907 }
2908
2909 pjsua_msg_data_init(&msg_data);
2910 if (app_config.no_refersub) {
2911 /* Add Refer-Sub: false in outgoing REFER request */
2912 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
2913 &STR_FALSE);
2914 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
2915 }
2916
2917 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
2918 }
2919 break;
2920
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002921 case '#':
2922 /*
2923 * Send DTMF strings.
2924 */
2925 if (current_call == -1) {
2926
2927 PJ_LOG(3,(THIS_FILE, "No current call"));
2928
2929 } else if (!pjsua_call_has_media(current_call)) {
2930
2931 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
2932
2933 } else {
2934 pj_str_t digits;
2935 int call = current_call;
2936 pj_status_t status;
2937
2938 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
2939 sizeof(buf)))
2940 {
2941 break;
2942 }
2943
2944 if (call != current_call) {
2945 puts("Call has been disconnected");
2946 continue;
2947 }
2948
2949 digits = pj_str(buf);
2950 status = pjsua_call_dial_dtmf(current_call, &digits);
2951 if (status != PJ_SUCCESS) {
2952 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
2953 } else {
2954 puts("DTMF digits enqueued for transmission");
2955 }
2956 }
2957 break;
2958
Benny Prijonofeb69f42007-10-05 09:12:26 +00002959 case '*':
2960 /* Send DTMF with INFO */
2961 if (current_call == -1) {
2962
2963 PJ_LOG(3,(THIS_FILE, "No current call"));
2964
2965 } else {
2966 const pj_str_t SIP_INFO = pj_str("INFO");
2967 pj_str_t digits;
2968 int call = current_call;
2969 int i;
2970 pj_status_t status;
2971
2972 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
2973 sizeof(buf)))
2974 {
2975 break;
2976 }
2977
2978 if (call != current_call) {
2979 puts("Call has been disconnected");
2980 continue;
2981 }
2982
2983 digits = pj_str(buf);
2984 for (i=0; i<digits.slen; ++i) {
2985 pjsua_msg_data msg_data;
2986 char body[80];
2987
2988 pjsua_msg_data_init(&msg_data);
2989 msg_data.content_type = pj_str("application/dtmf-relay");
2990
2991 pj_ansi_snprintf(body, sizeof(body),
2992 "Signal=%c\r\n"
2993 "Duration=160",
2994 buf[i]);
2995 msg_data.msg_body = pj_str(body);
2996
2997 status = pjsua_call_send_request(current_call, &SIP_INFO,
2998 &msg_data);
2999 if (status != PJ_SUCCESS) {
3000 break;
3001 }
3002 }
3003 }
3004 break;
3005
Benny Prijono56315612006-07-18 14:39:40 +00003006 case 'S':
3007 /*
3008 * Send arbitrary request
3009 */
3010 if (pjsua_acc_get_count() == 0) {
3011 puts("Sorry, need at least one account configured");
3012 break;
3013 }
3014
3015 puts("Send arbitrary request to remote host");
3016
3017 /* Input METHOD */
3018 if (!simple_input("Request method:",text,sizeof(text)))
3019 break;
3020
3021 /* Input destination URI */
3022 uri = NULL;
3023 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3024 if (result.nb_result != NO_NB) {
3025
3026 if (result.nb_result == -1 || result.nb_result == 0) {
3027 puts("Sorry you can't do that!");
3028 continue;
3029 } else {
3030 pjsua_buddy_info binfo;
3031 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3032 uri = binfo.uri.ptr;
3033 }
3034
3035 } else if (result.uri_result) {
3036 uri = result.uri_result;
3037 }
3038
3039 tmp = pj_str(uri);
3040
3041 send_request(text, &tmp);
3042 break;
3043
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003044 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003045 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3046 pj_str_t tmp;
3047 int delay;
3048
3049 tmp.ptr = menuin+6;
3050 tmp.slen = pj_ansi_strlen(menuin)-7;
3051
3052 if (tmp.slen < 1) {
3053 puts("Usage: sleep MSEC");
3054 break;
3055 }
3056
3057 delay = pj_strtoul(&tmp);
3058 if (delay < 0) delay = 0;
3059 pj_thread_sleep(delay);
3060 break;
3061 }
3062 /* Continue below */
3063
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003064 case 'u':
3065 /*
3066 * Subscribe/unsubscribe presence.
3067 */
3068 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3069 if (result.nb_result != NO_NB) {
3070 if (result.nb_result == -1) {
3071 int i, count;
3072 count = pjsua_get_buddy_count();
3073 for (i=0; i<count; ++i)
3074 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3075 } else if (result.nb_result == 0) {
3076 puts("Sorry, can only subscribe to buddy's presence, "
3077 "not from existing call");
3078 } else {
3079 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3080 }
3081
3082 } else if (result.uri_result) {
3083 puts("Sorry, can only subscribe to buddy's presence, "
3084 "not arbitrary URL (for now)");
3085 }
3086
3087 break;
3088
3089 case 'r':
3090 switch (menuin[1]) {
3091 case 'r':
3092 /*
3093 * Re-Register.
3094 */
3095 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3096 break;
3097 case 'u':
3098 /*
3099 * Unregister
3100 */
3101 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3102 break;
3103 }
3104 break;
3105
3106 case 't':
3107 pjsua_acc_get_info(current_acc, &acc_info);
3108 acc_info.online_status = !acc_info.online_status;
3109 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3110 printf("Setting %s online status to %s\n",
3111 acc_info.acc_uri.ptr,
3112 (acc_info.online_status?"online":"offline"));
3113 break;
3114
Benny Prijono4461c7d2007-08-25 13:36:15 +00003115 case 'T':
3116 change_online_status();
3117 break;
3118
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003119 case 'c':
3120 switch (menuin[1]) {
3121 case 'l':
3122 conf_list();
3123 break;
3124 case 'c':
3125 case 'd':
3126 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003127 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003128 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003129 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003130 const char *src_title, *dst_title;
3131
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003132 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003133
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003134 if (cnt != 3) {
3135 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003136
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003137 src_title = (menuin[1]=='c'?
3138 "Connect src port #":
3139 "Disconnect src port #");
3140 dst_title = (menuin[1]=='c'?
3141 "To dst port #":
3142 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003143
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003144 if (!simple_input(src_title, src_port, sizeof(src_port)))
3145 break;
3146
3147 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3148 break;
3149 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003150
3151 if (menuin[1]=='c') {
3152 status = pjsua_conf_connect(my_atoi(src_port),
3153 my_atoi(dst_port));
3154 } else {
3155 status = pjsua_conf_disconnect(my_atoi(src_port),
3156 my_atoi(dst_port));
3157 }
3158 if (status == PJ_SUCCESS) {
3159 puts("Success");
3160 } else {
3161 puts("ERROR!!");
3162 }
3163 }
3164 break;
3165 }
3166 break;
3167
Benny Prijono6dd967c2006-12-26 02:27:14 +00003168 case 'V':
3169 /* Adjust audio volume */
3170 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3171 if (simple_input(buf,text,sizeof(text))) {
3172 char *err;
3173 app_config.mic_level = (float)strtod(text, &err);
3174 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3175 }
3176 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3177 app_config.speaker_level);
3178 if (simple_input(buf,text,sizeof(text))) {
3179 char *err;
3180 app_config.speaker_level = (float)strtod(text, &err);
3181 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3182 }
3183
3184 break;
3185
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003186 case 'd':
3187 if (menuin[1] == 'c') {
3188 char settings[2000];
3189 int len;
3190
3191 len = write_settings(&app_config, settings, sizeof(settings));
3192 if (len < 1)
3193 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3194 else
3195 PJ_LOG(3,(THIS_FILE,
3196 "Dumping configuration (%d bytes):\n%s\n",
3197 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003198
3199 } else if (menuin[1] == 'q') {
3200
3201 if (current_call != PJSUA_INVALID_ID) {
3202 char buf[1024];
3203 pjsua_call_dump(current_call, PJ_TRUE, buf,
3204 sizeof(buf), " ");
3205 PJ_LOG(3,(THIS_FILE, "\n%s", buf));
3206 } else {
3207 PJ_LOG(3,(THIS_FILE, "No current call"));
3208 }
3209
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003210 } else {
3211 app_dump(menuin[1]=='d');
3212 }
3213 break;
3214
3215
3216 case 'f':
3217 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3218 char settings[2000];
3219 int len;
3220
3221 len = write_settings(&app_config, settings, sizeof(settings));
3222 if (len < 1)
3223 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3224 else {
3225 pj_oshandle_t fd;
3226 pj_status_t status;
3227
3228 status = pj_file_open(app_config.pool, buf,
3229 PJ_O_WRONLY, &fd);
3230 if (status != PJ_SUCCESS) {
3231 pjsua_perror(THIS_FILE, "Unable to open file", status);
3232 } else {
3233 pj_ssize_t size = len;
3234 pj_file_write(fd, settings, &size);
3235 pj_file_close(fd);
3236
3237 printf("Settings successfully written to '%s'\n", buf);
3238 }
3239 }
3240
3241 }
3242 break;
3243
3244
3245 case 'q':
3246 goto on_exit;
3247
3248
3249 default:
3250 if (menuin[0] != '\n' && menuin[0] != '\r') {
3251 printf("Invalid input %s", menuin);
3252 }
3253 keystroke_help();
3254 break;
3255 }
3256 }
3257
3258on_exit:
3259 ;
3260}
3261
3262
3263/*****************************************************************************
3264 * Public API
3265 */
3266
3267pj_status_t app_init(int argc, char *argv[])
3268{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003269 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003270 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003271 unsigned i;
3272 pj_status_t status;
3273
3274 /* Create pjsua */
3275 status = pjsua_create();
3276 if (status != PJ_SUCCESS)
3277 return status;
3278
3279 /* Create pool for application */
Benny Prijonod5696da2007-07-17 16:25:45 +00003280 app_config.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003281
3282 /* Initialize default config */
3283 default_config(&app_config);
3284
3285 /* Parse the arguments */
3286 status = parse_args(argc, argv, &app_config, &uri_arg);
3287 if (status != PJ_SUCCESS)
3288 return status;
3289
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003290 /* Initialize application callbacks */
3291 app_config.cfg.cb.on_call_state = &on_call_state;
3292 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3293 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003294 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003295 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003296 app_config.cfg.cb.on_reg_state = &on_reg_state;
3297 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3298 app_config.cfg.cb.on_pager = &on_pager;
3299 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003300 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003301 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003302 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003303
3304 /* Initialize pjsua */
3305 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3306 &app_config.media_cfg);
3307 if (status != PJ_SUCCESS)
3308 return status;
3309
Benny Prijonoe909eac2006-07-27 22:04:56 +00003310#ifdef STEREO_DEMO
3311 stereo_demo();
3312#endif
3313
Benny Prijono804ff0a2006-09-14 11:17:48 +00003314 /* Initialize calls data */
3315 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3316 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3317 app_config.call_data[i].timer.cb = &call_timeout_callback;
3318 }
3319
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003320 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003321 for (i=0; i<app_config.wav_count; ++i) {
3322 pjsua_player_id wav_id;
3323
3324 status = pjsua_player_create(&app_config.wav_files[i], 0,
3325 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003326 if (status != PJ_SUCCESS)
3327 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003328
Benny Prijono72c04d22007-02-17 22:20:58 +00003329 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003330 app_config.wav_id = wav_id;
3331 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
3332 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003333 }
3334
Benny Prijono4af234b2007-01-24 02:02:09 +00003335 /* Optionally registers tone players */
3336 for (i=0; i<app_config.tone_count; ++i) {
3337 pjmedia_port *tport;
3338 char name[80];
3339 pj_str_t label;
3340 pj_status_t status;
3341
3342 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3343 app_config.tones[i].freq1,
3344 app_config.tones[i].freq2);
3345 label = pj_str(name);
3346 status = pjmedia_tonegen_create2(app_config.pool, &label,
3347 8000, 1, 160, 16,
3348 PJMEDIA_TONEGEN_LOOP, &tport);
3349 if (status != PJ_SUCCESS) {
3350 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3351 goto on_error;
3352 }
3353
3354 status = pjsua_conf_add_port(app_config.pool, tport,
3355 &app_config.tone_slots[i]);
3356 pj_assert(status == PJ_SUCCESS);
3357
3358 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3359 pj_assert(status == PJ_SUCCESS);
3360 }
3361
Benny Prijono1ebd6142006-10-19 15:48:02 +00003362 /* Optionally create recorder file, if any. */
3363 if (app_config.rec_file.slen) {
3364 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3365 &app_config.rec_id);
3366 if (status != PJ_SUCCESS)
3367 goto on_error;
3368
3369 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3370 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003371
Benny Prijonoe347cb02007-02-14 14:36:13 +00003372 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3373
Benny Prijono87ef89a2007-01-14 00:39:45 +00003374 /* Add UDP transport unless it's disabled. */
3375 if (!app_config.no_udp) {
3376 pjsua_acc_id aid;
3377
3378 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
3379 &app_config.udp_cfg,
3380 &transport_id);
3381 if (status != PJ_SUCCESS)
3382 goto on_error;
3383
3384 /* Add local account */
3385 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
3386 //pjsua_acc_set_transport(aid, transport_id);
3387 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00003388
3389 if (app_config.udp_cfg.port == 0) {
3390 pjsua_transport_info ti;
3391 pj_sockaddr_in *a;
3392
3393 pjsua_transport_get_info(transport_id, &ti);
3394 a = (pj_sockaddr_in*)&ti.local_addr;
3395
3396 tcp_cfg.port = pj_ntohs(a->sin_port);
3397 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00003398 }
3399
Benny Prijonoe93e2872006-06-28 16:46:49 +00003400 /* Add TCP transport unless it's disabled */
3401 if (!app_config.no_tcp) {
3402 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00003403 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00003404 &transport_id);
3405 if (status != PJ_SUCCESS)
3406 goto on_error;
3407
3408 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00003409 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00003410 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3411
3412 }
3413
Benny Prijonoe93e2872006-06-28 16:46:49 +00003414
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003415#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
3416 /* Add TLS transport when application wants one */
3417 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00003418
3419 pjsua_acc_id acc_id;
3420
3421 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00003422 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003423 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00003424 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003425 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00003426 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003427 if (status != PJ_SUCCESS)
3428 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00003429
3430 /* Add local account */
3431 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
3432 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003433 }
3434#endif
3435
Benny Prijonoe93e2872006-06-28 16:46:49 +00003436 if (transport_id == -1) {
3437 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
3438 status = -1;
3439 goto on_error;
3440 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003441
3442
3443 /* Add accounts */
3444 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003445 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003446 if (status != PJ_SUCCESS)
3447 goto on_error;
3448 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3449 }
3450
3451 /* Add buddies */
3452 for (i=0; i<app_config.buddy_cnt; ++i) {
3453 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
3454 if (status != PJ_SUCCESS)
3455 goto on_error;
3456 }
3457
3458 /* Optionally set codec orders */
3459 for (i=0; i<app_config.codec_cnt; ++i) {
3460 pjsua_codec_set_priority(&app_config.codec_arg[i],
3461 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
3462 }
3463
3464 /* Add RTP transports */
3465 status = pjsua_media_transports_create(&app_config.rtp_cfg);
3466 if (status != PJ_SUCCESS)
3467 goto on_error;
3468
Benny Prijono22a300a2006-06-14 20:04:55 +00003469 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00003470#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00003471 if (app_config.null_audio) {
3472 status = pjsua_set_null_snd_dev();
3473 if (status != PJ_SUCCESS)
3474 return status;
3475 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00003476#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00003477
Benny Prijono4e5d5512007-03-06 18:11:30 +00003478 if (app_config.capture_dev != PJSUA_INVALID_ID
3479 || app_config.playback_dev != PJSUA_INVALID_ID) {
3480 status
3481 = pjsua_set_snd_dev(app_config.capture_dev, app_config.playback_dev);
3482 if (status != PJ_SUCCESS)
3483 goto on_error;
3484 }
3485
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003486 return PJ_SUCCESS;
3487
3488on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00003489 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003490 return status;
3491}
3492
3493
3494pj_status_t app_main(void)
3495{
3496 pj_status_t status;
3497
3498 /* Start pjsua */
3499 status = pjsua_start();
3500 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00003501 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003502 return status;
3503 }
3504
3505 console_app_main(&uri_arg);
3506
3507 return PJ_SUCCESS;
3508}
3509
3510pj_status_t app_destroy(void)
3511{
Benny Prijonof762ee72006-12-01 11:14:37 +00003512 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00003513 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00003514
Benny Prijonoe909eac2006-07-27 22:04:56 +00003515#ifdef STEREO_DEMO
3516 if (app_config.snd) {
3517 pjmedia_snd_port_destroy(app_config.snd);
3518 app_config.snd = NULL;
3519 }
3520#endif
3521
Benny Prijono4af234b2007-01-24 02:02:09 +00003522 /* Close tone generators */
3523 for (i=0; i<app_config.tone_count; ++i) {
3524 pjsua_conf_remove_port(app_config.tone_slots[i]);
3525 }
3526
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003527 if (app_config.pool) {
3528 pj_pool_release(app_config.pool);
3529 app_config.pool = NULL;
3530 }
3531
Benny Prijonof762ee72006-12-01 11:14:37 +00003532 status = pjsua_destroy();
3533
3534 pj_bzero(&app_config, sizeof(app_config));
3535
3536 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003537}
Benny Prijonoe909eac2006-07-27 22:04:56 +00003538
3539
3540#ifdef STEREO_DEMO
3541static void stereo_demo()
3542{
3543 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00003544 pj_status_t status;
3545
3546 /* Disable existing sound device */
3547 conf = pjsua_set_no_snd_dev();
3548
Benny Prijonoe909eac2006-07-27 22:04:56 +00003549 /* Create stereo-mono splitter/combiner */
3550 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003551 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003552 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003553 2 * conf->info.samples_per_frame,
3554 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003555 0 /* options */,
3556 &splitter);
3557 pj_assert(status == PJ_SUCCESS);
3558
3559 /* Connect channel0 (left channel?) to conference port slot0 */
3560 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
3561 0 /*options*/,
3562 conf);
3563 pj_assert(status == PJ_SUCCESS);
3564
3565 /* Create reverse channel for channel1 (right channel?)... */
3566 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
3567 splitter,
3568 1 /* ch1 */,
3569 0 /* options */,
3570 &ch1);
3571 pj_assert(status == PJ_SUCCESS);
3572
3573 /* .. and register it to conference bridge (it would be slot1
3574 * if there's no other devices connected to the bridge)
3575 */
3576 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
3577 pj_assert(status == PJ_SUCCESS);
3578
3579 /* Create sound device */
3580 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003581 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003582 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003583 2 * conf->info.samples_per_frame,
3584 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003585 0, &app_config.snd);
3586 pj_assert(status == PJ_SUCCESS);
3587
3588
3589 /* Connect the splitter to the sound device */
3590 status = pjmedia_snd_port_connect(app_config.snd, splitter);
3591 pj_assert(status == PJ_SUCCESS);
3592
3593}
3594#endif
3595