blob: 264036ec9a9409d7fa68d81f0bbf6bdb4a656a23 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20
21
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000022#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000023#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000024
Benny Prijonoe909eac2006-07-27 22:04:56 +000025//#define STEREO_DEMO
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026
Benny Prijono4f966892008-06-14 22:43:56 +000027/* Ringtones US UK */
28#define RINGBACK_FREQ1 440 /* 400 */
29#define RINGBACK_FREQ2 480 /* 450 */
30#define RINGBACK_ON 2000 /* 400 */
31#define RINGBACK_OFF 4000 /* 200 */
32#define RINGBACK_CNT 1 /* 2 */
33#define RINGBACK_INTERVAL 4000 /* 2000 */
Benny Prijono91d20f42008-06-14 19:42:37 +000034
Benny Prijono4f966892008-06-14 22:43:56 +000035#define RING_FREQ1 800
36#define RING_FREQ2 640
37#define RING_ON 200
38#define RING_OFF 100
39#define RING_CNT 3
40#define RING_INTERVAL 3000
Benny Prijono91d20f42008-06-14 19:42:37 +000041
42
Benny Prijono804ff0a2006-09-14 11:17:48 +000043/* Call specific data */
44struct call_data
45{
46 pj_timer_entry timer;
Benny Prijono91d20f42008-06-14 19:42:37 +000047 pj_bool_t ringback_on;
48 pj_bool_t ring_on;
Benny Prijono804ff0a2006-09-14 11:17:48 +000049};
50
Benny Prijonoeebe9af2006-06-13 22:57:13 +000051
52/* Pjsua application data */
53static struct app_config
54{
55 pjsua_config cfg;
56 pjsua_logging_config log_cfg;
57 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000058 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000059 pj_bool_t no_tcp;
60 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000061 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000062 pjsua_transport_config udp_cfg;
63 pjsua_transport_config rtp_cfg;
64
65 unsigned acc_cnt;
66 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
67
68 unsigned buddy_cnt;
69 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
70
Benny Prijono804ff0a2006-09-14 11:17:48 +000071 struct call_data call_data[PJSUA_MAX_CALLS];
72
Benny Prijonoeebe9af2006-06-13 22:57:13 +000073 pj_pool_t *pool;
74 /* Compatibility with older pjsua */
75
76 unsigned codec_cnt;
77 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000078 unsigned codec_dis_cnt;
79 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000080 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000081 unsigned wav_count;
82 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000083 unsigned tone_count;
84 pjmedia_tone_desc tones[32];
85 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000086 pjsua_player_id wav_id;
87 pjsua_conf_port_id wav_port;
88 pj_bool_t auto_play;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +000089 pj_bool_t auto_play_hangup;
90 pj_timer_entry auto_hangup_timer;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000091 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000092 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000093 pj_str_t rec_file;
94 pj_bool_t auto_rec;
95 pjsua_recorder_id rec_id;
96 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000097 unsigned auto_answer;
98 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +000099
100#ifdef STEREO_DEMO
101 pjmedia_snd_port *snd;
102#endif
103
Benny Prijono6dd967c2006-12-26 02:27:14 +0000104 float mic_level,
105 speaker_level;
106
Benny Prijono4e5d5512007-03-06 18:11:30 +0000107 int capture_dev, playback_dev;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000108 unsigned capture_lat, playback_lat;
Benny Prijono91d20f42008-06-14 19:42:37 +0000109
110 pj_bool_t no_tones;
111 int ringback_slot;
112 int ringback_cnt;
113 pjmedia_port *ringback_port;
114 int ring_slot;
115 int ring_cnt;
116 pjmedia_port *ring_port;
117
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000118} app_config;
119
120
Benny Prijono21b9ad92006-08-15 13:11:22 +0000121//static pjsua_acc_id current_acc;
122#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +0000123static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoebc32c32008-06-12 13:30:39 +0000124static pj_bool_t cmd_echo;
125static int stdout_refresh = -1;
126static const char *stdout_refresh_text = "STDOUT_REFRESH";
127static pj_bool_t stdout_refresh_quit = PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000128static pj_str_t uri_arg;
129
Benny Prijono6eddd872008-03-21 13:46:08 +0000130static char some_buf[2048];
131
Benny Prijono594e4c52006-09-14 18:51:01 +0000132#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +0000133static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +0000134#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000135pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000136
Benny Prijono91d20f42008-06-14 19:42:37 +0000137static void ringback_start(pjsua_call_id call_id);
138static void ring_start(pjsua_call_id call_id);
139static void ring_stop(pjsua_call_id call_id);
140
141
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000142/*****************************************************************************
143 * Configuration manipulation
144 */
145
146/* Show usage */
147static void usage(void)
148{
149 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000150 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000151 puts ("");
152 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000153 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000154 puts (" --help Display this help screen");
155 puts (" --version Display version info");
156 puts ("");
157 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000158 puts (" --log-file=fname Log to filename (default stderr)");
159 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
160 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
161 puts ("");
162 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000163 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000164#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000165 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
166 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000167#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000168 puts (" --registrar=url Set the URL of registrar server");
169 puts (" --id=url Set the URL of local ID (used in From header)");
170 puts (" --contact=url Optionally override the Contact information");
171 puts (" --proxy=url Optional URL of proxy server to visit");
172 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000173 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174 puts (" --realm=string Set realm");
175 puts (" --username=string Set authentication username");
176 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000177 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000178 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000179 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
180 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000181 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 puts ("");
183 puts ("SIP Account Control:");
184 puts (" --next-account Add more account");
185 puts ("");
186 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000187 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
188 puts (" TCP and UDP transports on the specified port, unless");
189 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000190 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
191 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000192 puts (" --no-tcp Disable TCP transport.");
193 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000194 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
195 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000196 puts (" --outbound=url Set the URL of global outbound proxy server");
197 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000198 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000199 puts ("");
200 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000201 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000202 puts (" --tls-ca-file Specify TLS CA file (default=none)");
203 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
204 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
205 puts (" --tls-password Specify TLS password to private key file (default=none)");
206 puts (" --tls-verify-server Verify server's certificate (default=no)");
207 puts (" --tls-verify-client Verify client's certificate (default=no)");
208 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
209
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000210 puts ("");
211 puts ("Media Options:");
212 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000213 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000214 puts (" --clock-rate=N Override conference bridge clock rate");
215 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000216 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000217 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000218 puts (" --play-file=file Register WAV file in conference bridge.");
219 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000220 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
221 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
222 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000223 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000224 puts (" --auto-play Automatically play the file (to incoming calls only)");
225 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000226 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000227 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
228 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000229 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000230 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000231 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000232 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono00cae612006-07-31 15:19:36 +0000233 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000234 puts (" --capture-dev=id Audio capture device ID (default=-1)");
235 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000236 puts (" --capture-lat=N Audio capture latency, in ms (default=10)");
237 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000238 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
239 puts (" Specify N=-1 (default) to disable this feature.");
240 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000241 puts (" --no-tones Disable audible tones");
Benny Prijono00cae612006-07-31 15:19:36 +0000242
Benny Prijonof76e1392008-06-06 14:51:48 +0000243 puts ("");
244 puts ("Media Transport Options:");
245 puts (" --use-ice Enable ICE (default:no)");
246 puts (" --ice-no-host Disable ICE host candidates");
247 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
248 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
249 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
250 puts (" --use-turn Enable TURN relay with ICE (default:no)");
251 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
252 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
253 puts (" --turn-user TURN username");
254 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000255
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000256 puts ("");
257 puts ("Buddy List (can be more than one):");
258 puts (" --add-buddy url Add the specified URL to the buddy list.");
259 puts ("");
260 puts ("User Agent options:");
261 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
262 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000263 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000264 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000265 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000266 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000267
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000268 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000269 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
270 puts ("");
271
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000272 fflush(stdout);
273}
274
275
276/* Set default config. */
277static void default_config(struct app_config *cfg)
278{
Benny Prijono56315612006-07-18 14:39:40 +0000279 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000280 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000281
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000282 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000283 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000284 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
285
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000286 pjsua_logging_config_default(&cfg->log_cfg);
287 pjsua_media_config_default(&cfg->media_cfg);
288 pjsua_transport_config_default(&cfg->udp_cfg);
289 cfg->udp_cfg.port = 5060;
290 pjsua_transport_config_default(&cfg->rtp_cfg);
291 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000292 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000293 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000294 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000295 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000296 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000297 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000298 cfg->capture_dev = PJSUA_INVALID_ID;
299 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000300 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
301 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000302 cfg->ringback_slot = PJSUA_INVALID_ID;
303 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000304
305 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
306 pjsua_acc_config_default(&cfg->acc_cfg[i]);
307
308 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
309 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000310}
311
312
313/*
314 * Read command arguments from config file.
315 */
316static int read_config_file(pj_pool_t *pool, const char *filename,
317 int *app_argc, char ***app_argv)
318{
319 int i;
320 FILE *fhnd;
321 char line[200];
322 int argc = 0;
323 char **argv;
324 enum { MAX_ARGS = 64 };
325
326 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
327 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
328 argv[argc++] = *app_argv[0];
329
330 /* Open config file. */
331 fhnd = fopen(filename, "rt");
332 if (!fhnd) {
333 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
334 fflush(stdout);
335 return -1;
336 }
337
338 /* Scan tokens in the file. */
339 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000340 char *token;
341 char *p;
342 const char *whitespace = " \t\r\n";
343 char cDelimiter;
344 int len, token_len;
345
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000346 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000347
348 // Trim ending newlines
349 len = strlen(line);
350 if (line[len-1]=='\n')
351 line[--len] = '\0';
352 if (line[len-1]=='\r')
353 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000354
Benny Prijonobf5b4692007-06-28 03:20:17 +0000355 if (len==0) continue;
356
357 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
358 // first, scan whitespaces
359 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
360
361 if (*p == '\0') // are we done yet?
362 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363
Benny Prijonobf5b4692007-06-28 03:20:17 +0000364 if (*p == '"' || *p == '\'') { // is token a quoted string
365 cDelimiter = *p++; // save quote delimiter
366 token = p;
367
368 while (*p != '\0' && *p != cDelimiter) p++;
369
370 if (*p == '\0') // found end of the line, but,
371 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372
Benny Prijonobf5b4692007-06-28 03:20:17 +0000373 } else { // token's not a quoted string
374 token = p;
375
376 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
377
378 cDelimiter = *p;
379 }
380
381 *p = '\0';
382 token_len = p-token;
383
384 if (token_len > 0) {
385 if (*token == '#')
386 break; // ignore remainder of line
387
388 argv[argc] = pj_pool_alloc(pool, token_len + 1);
389 pj_memcpy(argv[argc], token, token_len + 1);
390 ++argc;
391 }
392
393 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000394 }
395 }
396
397 /* Copy arguments from command line */
398 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
399 argv[argc++] = (*app_argv)[i];
400
401 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
402 PJ_LOG(1,(THIS_FILE,
403 "Too many arguments specified in cmd line/config file"));
404 fflush(stdout);
405 fclose(fhnd);
406 return -1;
407 }
408
409 fclose(fhnd);
410
411 /* Assign the new command line back to the original command line. */
412 *app_argc = argc;
413 *app_argv = argv;
414 return 0;
415
416}
417
418static int my_atoi(const char *cs)
419{
420 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000421
422 pj_cstr(&s, cs);
423 if (cs[0] == '-') {
424 s.ptr++, s.slen--;
425 return 0 - (int)pj_strtoul(&s);
426 } else if (cs[0] == '+') {
427 s.ptr++, s.slen--;
428 return pj_strtoul(&s);
429 } else {
430 return pj_strtoul(&s);
431 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000432}
433
434
435/* Parse arguments. */
436static pj_status_t parse_args(int argc, char *argv[],
437 struct app_config *cfg,
438 pj_str_t *uri_to_call)
439{
440 int c;
441 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000442 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000443 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000444 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
445 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000446 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000447 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000448 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000449 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000450 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
451 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijonof76e1392008-06-06 14:51:48 +0000452 OPT_USE_TURN,OPT_ICE_NO_HOST, OPT_TURN_SRV, OPT_TURN_TCP,
453 OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000454 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
455 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000456 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijonod79f25c2006-08-02 19:41:37 +0000457 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000458 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000459 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000460 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000461 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
462 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
463 OPT_TLS_NEG_TIMEOUT,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000464 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Benny Prijono91d20f42008-06-14 19:42:37 +0000465 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000466 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000467#ifdef _IONBF
468 OPT_STDOUT_NO_BUF,
469#endif
Benny Prijonofce28542007-12-09 15:41:10 +0000470 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471 };
472 struct pj_getopt_option long_options[] = {
473 { "config-file",1, 0, OPT_CONFIG_FILE},
474 { "log-file", 1, 0, OPT_LOG_FILE},
475 { "log-level", 1, 0, OPT_LOG_LEVEL},
476 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
477 { "help", 0, 0, OPT_HELP},
478 { "version", 0, 0, OPT_VERSION},
479 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000480 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000481 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000482 { "null-audio", 0, 0, OPT_NULL_AUDIO},
483 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000484 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000485 { "no-tcp", 0, 0, OPT_NO_TCP},
486 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000487 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000488 { "proxy", 1, 0, OPT_PROXY},
489 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
490 { "registrar", 1, 0, OPT_REGISTRAR},
491 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000492 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000493 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000494 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000495 { "id", 1, 0, OPT_ID},
496 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000497 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
498 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000499 { "realm", 1, 0, OPT_REALM},
500 { "username", 1, 0, OPT_USERNAME},
501 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000502 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000503 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000504 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
506 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
507 { "no-presence", 0, 0, OPT_NO_PRESENCE},
508 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000510 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000511 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000512 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
513 { "auto-conf", 0, 0, OPT_AUTO_CONF},
514 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000515 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000516 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000517 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000518
Benny Prijonoc97608e2007-03-23 16:34:20 +0000519 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000520 { "use-turn", 0, 0, OPT_USE_TURN},
521 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
522 { "turn-srv", 1, 0, OPT_TURN_SRV},
523 { "turn-tcp", 0, 0, OPT_TURN_TCP},
524 { "turn-user", 1, 0, OPT_TURN_USER},
525 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
526
Benny Prijonod8179652008-01-23 20:39:07 +0000527#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
528 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000529 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000530#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000531 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000532 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000533 { "complexity", 1, 0, OPT_COMPLEXITY},
534 { "quality", 1, 0, OPT_QUALITY},
535 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000536 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000537 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono00cae612006-07-31 15:19:36 +0000538 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
539 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
540 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000541 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000542 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000543 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000544 { "duration", 1, 0, OPT_DURATION},
545 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000546 { "use-tls", 0, 0, OPT_USE_TLS},
547 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000548 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
549 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000550 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000551 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
552 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
553 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000554 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
555 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000556 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
557 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000558 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
559 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000560#ifdef _IONBF
561 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
562#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000563 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000564 { "no-tones", 0, 0, OPT_NO_TONES},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000565 { NULL, 0, 0, 0}
566 };
567 pj_status_t status;
568 pjsua_acc_config *cur_acc;
569 char *config_file = NULL;
570 unsigned i;
571
572 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000573 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000574 while ((c=pj_getopt_long(argc, argv, "", long_options,
575 &option_index)) != -1)
576 {
577 switch (c) {
578 case OPT_CONFIG_FILE:
579 config_file = pj_optarg;
580 break;
581 }
582 if (config_file)
583 break;
584 }
585
586 if (config_file) {
587 status = read_config_file(app_config.pool, config_file, &argc, &argv);
588 if (status != 0)
589 return status;
590 }
591
592 cfg->acc_cnt = 0;
593 cur_acc = &cfg->acc_cfg[0];
594
595
596 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
597 * read from config file.
598 */
599 pj_optind = 0;
600 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000601 pj_str_t tmp;
602 long lval;
603
604 switch (c) {
605
Benny Prijono6f137482006-06-15 11:31:36 +0000606 case OPT_CONFIG_FILE:
607 /* Ignore as this has been processed before */
608 break;
609
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000610 case OPT_LOG_FILE:
611 cfg->log_cfg.log_filename = pj_str(pj_optarg);
612 break;
613
614 case OPT_LOG_LEVEL:
615 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
616 if (c < 0 || c > 6) {
617 PJ_LOG(1,(THIS_FILE,
618 "Error: expecting integer value 0-6 "
619 "for --log-level"));
620 return PJ_EINVAL;
621 }
622 cfg->log_cfg.level = c;
623 pj_log_set_level( c );
624 break;
625
626 case OPT_APP_LOG_LEVEL:
627 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
628 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
629 PJ_LOG(1,(THIS_FILE,
630 "Error: expecting integer value 0-6 "
631 "for --app-log-level"));
632 return PJ_EINVAL;
633 }
634 break;
635
636 case OPT_HELP:
637 usage();
638 return PJ_EINVAL;
639
640 case OPT_VERSION: /* version */
641 pj_dump_config();
642 return PJ_EINVAL;
643
644 case OPT_NULL_AUDIO:
645 cfg->null_audio = PJ_TRUE;
646 break;
647
648 case OPT_CLOCK_RATE:
649 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000650 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000651 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000652 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000653 return PJ_EINVAL;
654 }
655 cfg->media_cfg.clock_rate = lval;
656 break;
657
Benny Prijonof3758ee2008-02-26 15:32:16 +0000658 case OPT_SND_CLOCK_RATE:
659 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000660 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000661 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000662 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000663 return PJ_EINVAL;
664 }
665 cfg->media_cfg.snd_clock_rate = lval;
666 break;
667
Benny Prijono7d60d052008-03-29 12:24:20 +0000668 case OPT_STEREO:
669 cfg->media_cfg.channel_count = 2;
670 break;
671
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000672 case OPT_LOCAL_PORT: /* local-port */
673 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000674 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000675 PJ_LOG(1,(THIS_FILE,
676 "Error: expecting integer value for "
677 "--local-port"));
678 return PJ_EINVAL;
679 }
680 cfg->udp_cfg.port = (pj_uint16_t)lval;
681 break;
682
Benny Prijono0a5cad82006-09-26 13:21:02 +0000683 case OPT_IP_ADDR: /* ip-addr */
684 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
685 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
686 break;
687
Benny Prijonoe93e2872006-06-28 16:46:49 +0000688 case OPT_NO_UDP: /* no-udp */
689 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000690 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
691 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000692 }
693
694 cfg->no_udp = PJ_TRUE;
695 break;
696
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000697 case OPT_NOREFERSUB: /* norefersub */
698 cfg->no_refersub = PJ_TRUE;
699 break;
700
Benny Prijonoe93e2872006-06-28 16:46:49 +0000701 case OPT_NO_TCP: /* no-tcp */
702 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000703 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
704 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000705 }
706
707 cfg->no_tcp = PJ_TRUE;
708 break;
709
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000710 case OPT_PROXY: /* proxy */
711 if (pjsua_verify_sip_url(pj_optarg) != 0) {
712 PJ_LOG(1,(THIS_FILE,
713 "Error: invalid SIP URL '%s' "
714 "in proxy argument", pj_optarg));
715 return PJ_EINVAL;
716 }
717 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
718 break;
719
720 case OPT_OUTBOUND_PROXY: /* outbound proxy */
721 if (pjsua_verify_sip_url(pj_optarg) != 0) {
722 PJ_LOG(1,(THIS_FILE,
723 "Error: invalid SIP URL '%s' "
724 "in outbound proxy argument", pj_optarg));
725 return PJ_EINVAL;
726 }
727 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
728 break;
729
730 case OPT_REGISTRAR: /* registrar */
731 if (pjsua_verify_sip_url(pj_optarg) != 0) {
732 PJ_LOG(1,(THIS_FILE,
733 "Error: invalid SIP URL '%s' in "
734 "registrar argument", pj_optarg));
735 return PJ_EINVAL;
736 }
737 cur_acc->reg_uri = pj_str(pj_optarg);
738 break;
739
740 case OPT_REG_TIMEOUT: /* reg-timeout */
741 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
742 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
743 PJ_LOG(1,(THIS_FILE,
744 "Error: invalid value for --reg-timeout "
745 "(expecting 1-3600)"));
746 return PJ_EINVAL;
747 }
748 break;
749
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000750 case OPT_PUBLISH: /* publish */
751 cur_acc->publish_enabled = PJ_TRUE;
752 break;
753
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000754 case OPT_100REL: /** 100rel */
755 cur_acc->require_100rel = PJ_TRUE;
756 cfg->cfg.require_100rel = PJ_TRUE;
757 break;
758
Benny Prijono48ab2b72007-11-08 09:24:30 +0000759 case OPT_USE_IMS: /* Activate IMS settings */
760 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000761 break;
762
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000763 case OPT_ID: /* id */
764 if (pjsua_verify_sip_url(pj_optarg) != 0) {
765 PJ_LOG(1,(THIS_FILE,
766 "Error: invalid SIP URL '%s' "
767 "in local id argument", pj_optarg));
768 return PJ_EINVAL;
769 }
770 cur_acc->id = pj_str(pj_optarg);
771 break;
772
773 case OPT_CONTACT: /* contact */
774 if (pjsua_verify_sip_url(pj_optarg) != 0) {
775 PJ_LOG(1,(THIS_FILE,
776 "Error: invalid SIP URL '%s' "
777 "in contact argument", pj_optarg));
778 return PJ_EINVAL;
779 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000780 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000781 break;
782
Benny Prijonofce28542007-12-09 15:41:10 +0000783 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000784 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000785 break;
786
787 case OPT_USE_COMPACT_FORM:
788 /* enable compact form - from Ticket #342 */
789 {
790 extern pj_bool_t pjsip_use_compact_form;
791 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
792 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
793
794 pjsip_use_compact_form = PJ_TRUE;
795 /* do not transmit Allow header */
796 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
797 /* Do not include rtpmap for static payload types (<96) */
798 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
799 }
800 break;
801
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000802 case OPT_NEXT_ACCOUNT: /* Add more account. */
803 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000804 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000805 break;
806
807 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000808 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000809 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000810 break;
811
812 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000813 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000814 break;
815
816 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000817 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
818 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000819#if PJSIP_HAS_DIGEST_AKA_AUTH
820 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
821 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
822 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
823#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000824 break;
825
826 case OPT_NEXT_CRED: /* next credential */
827 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000828 break;
829
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000830 case OPT_NAMESERVER: /* nameserver */
831 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
832 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
833 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
834 return PJ_ETOOMANY;
835 }
836 break;
837
Benny Prijonoebbf6892007-03-24 17:37:25 +0000838 case OPT_STUN_DOMAIN: /* STUN domain */
839 cfg->cfg.stun_domain = pj_str(pj_optarg);
840 break;
841
Benny Prijonoc97608e2007-03-23 16:34:20 +0000842 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000843 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000844 break;
845
846 case OPT_ADD_BUDDY: /* Add to buddy list. */
847 if (pjsua_verify_sip_url(pj_optarg) != 0) {
848 PJ_LOG(1,(THIS_FILE,
849 "Error: invalid URL '%s' in "
850 "--add-buddy option", pj_optarg));
851 return -1;
852 }
853 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
854 PJ_LOG(1,(THIS_FILE,
855 "Error: too many buddies in buddy list."));
856 return -1;
857 }
858 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
859 cfg->buddy_cnt++;
860 break;
861
862 case OPT_AUTO_PLAY:
863 cfg->auto_play = 1;
864 break;
865
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000866 case OPT_AUTO_PLAY_HANGUP:
867 cfg->auto_play_hangup = 1;
868 break;
869
Benny Prijono1ebd6142006-10-19 15:48:02 +0000870 case OPT_AUTO_REC:
871 cfg->auto_rec = 1;
872 break;
873
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000874 case OPT_AUTO_LOOP:
875 cfg->auto_loop = 1;
876 break;
877
Benny Prijono7ca96da2006-08-07 12:11:40 +0000878 case OPT_AUTO_CONF:
879 cfg->auto_conf = 1;
880 break;
881
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000882 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000883 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000884 break;
885
Benny Prijono4af234b2007-01-24 02:02:09 +0000886 case OPT_PLAY_TONE:
887 {
888 int f1, f2, on, off;
889 int n;
890
891 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
892 if (n != 4) {
893 puts("Expecting f1,f2,on,off in --play-tone");
894 return -1;
895 }
896
897 cfg->tones[cfg->tone_count].freq1 = (short)f1;
898 cfg->tones[cfg->tone_count].freq2 = (short)f2;
899 cfg->tones[cfg->tone_count].on_msec = (short)on;
900 cfg->tones[cfg->tone_count].off_msec = (short)off;
901 ++cfg->tone_count;
902 }
903 break;
904
Benny Prijono1ebd6142006-10-19 15:48:02 +0000905 case OPT_REC_FILE:
906 cfg->rec_file = pj_str(pj_optarg);
907 break;
908
Benny Prijonoc97608e2007-03-23 16:34:20 +0000909 case OPT_USE_ICE:
910 cfg->media_cfg.enable_ice = PJ_TRUE;
911 break;
912
Benny Prijonof76e1392008-06-06 14:51:48 +0000913 case OPT_USE_TURN:
914 cfg->media_cfg.enable_turn = PJ_TRUE;
915 break;
916
917 case OPT_ICE_NO_HOST:
918 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
919 break;
920
921 case OPT_TURN_SRV:
922 cfg->media_cfg.turn_server = pj_str(pj_optarg);
923 break;
924
925 case OPT_TURN_TCP:
926 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
927 break;
928
929 case OPT_TURN_USER:
930 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
931 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
932 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
933 break;
934
935 case OPT_TURN_PASSWD:
936 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
937 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
938 break;
939
Benny Prijonod8179652008-01-23 20:39:07 +0000940#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
941 case OPT_USE_SRTP:
942 app_config.cfg.use_srtp = my_atoi(pj_optarg);
943 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
944 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
945 return -1;
946 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000947 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +0000948 break;
Benny Prijonof6508982008-01-25 09:02:33 +0000949 case OPT_SRTP_SECURE:
950 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
951 if (!pj_isdigit(*pj_optarg) ||
952 app_config.cfg.srtp_secure_signaling > 2)
953 {
954 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
955 return -1;
956 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000957 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +0000958 break;
Benny Prijonod8179652008-01-23 20:39:07 +0000959#endif
960
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000961 case OPT_RTP_PORT:
962 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +0000963 if (cfg->rtp_cfg.port == 0) {
964 enum { START_PORT=4000 };
965 unsigned range;
966
967 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
968 cfg->rtp_cfg.port = START_PORT +
969 ((pj_rand() % range) & 0xFFFE);
970 }
971
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000972 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
973 PJ_LOG(1,(THIS_FILE,
974 "Error: rtp-port argument value "
975 "(expecting 1-65535"));
976 return -1;
977 }
978 break;
979
Benny Prijonofce28542007-12-09 15:41:10 +0000980 case OPT_DIS_CODEC:
981 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
982 break;
983
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000984 case OPT_ADD_CODEC:
985 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
986 break;
987
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000988 /* These options were no longer valid after new pjsua */
989 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000990 case OPT_COMPLEXITY:
991 cfg->complexity = my_atoi(pj_optarg);
992 if (cfg->complexity < 0 || cfg->complexity > 10) {
993 PJ_LOG(1,(THIS_FILE,
994 "Error: invalid --complexity (expecting 0-10"));
995 return -1;
996 }
997 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000998 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000999
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001000 case OPT_DURATION:
1001 cfg->duration = my_atoi(pj_optarg);
1002 break;
1003
Benny Prijonof521eb02006-08-06 23:07:25 +00001004 case OPT_THREAD_CNT:
1005 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1006 if (cfg->cfg.thread_cnt > 128) {
1007 PJ_LOG(1,(THIS_FILE,
1008 "Error: invalid --thread-cnt option"));
1009 return -1;
1010 }
1011 break;
1012
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001013 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001014 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1015 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001016 PJ_LOG(1,(THIS_FILE,
1017 "Error: invalid --ptime option"));
1018 return -1;
1019 }
1020 break;
1021
Benny Prijono0a12f002006-07-26 17:05:39 +00001022 case OPT_NO_VAD:
1023 cfg->media_cfg.no_vad = PJ_TRUE;
1024 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001025
Benny Prijonod79f25c2006-08-02 19:41:37 +00001026 case OPT_EC_TAIL:
1027 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1028 if (cfg->media_cfg.ec_tail_len > 1000) {
1029 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1030 "is too big"));
1031 return -1;
1032 }
1033 break;
1034
Benny Prijono0498d902006-06-19 14:49:14 +00001035 case OPT_QUALITY:
1036 cfg->media_cfg.quality = my_atoi(pj_optarg);
1037 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1038 PJ_LOG(1,(THIS_FILE,
1039 "Error: invalid --quality (expecting 0-10"));
1040 return -1;
1041 }
1042 break;
1043
Benny Prijono00cae612006-07-31 15:19:36 +00001044 case OPT_ILBC_MODE:
1045 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1046 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1047 PJ_LOG(1,(THIS_FILE,
1048 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1049 return -1;
1050 }
1051 break;
1052
1053 case OPT_RX_DROP_PCT:
1054 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1055 if (cfg->media_cfg.rx_drop_pct > 100) {
1056 PJ_LOG(1,(THIS_FILE,
1057 "Error: invalid --rx-drop-pct (expecting <= 100"));
1058 return -1;
1059 }
1060 break;
1061
1062 case OPT_TX_DROP_PCT:
1063 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1064 if (cfg->media_cfg.tx_drop_pct > 100) {
1065 PJ_LOG(1,(THIS_FILE,
1066 "Error: invalid --tx-drop-pct (expecting <= 100"));
1067 return -1;
1068 }
1069 break;
1070
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001071 case OPT_AUTO_ANSWER:
1072 cfg->auto_answer = my_atoi(pj_optarg);
1073 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1074 PJ_LOG(1,(THIS_FILE,
1075 "Error: invalid code in --auto-answer "
1076 "(expecting 100-699"));
1077 return -1;
1078 }
1079 break;
1080
1081 case OPT_MAX_CALLS:
1082 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001083 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001084 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1085 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001086 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001087 return -1;
1088 }
1089 break;
1090
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001091 case OPT_USE_TLS:
1092 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001093#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1094 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1095 return -1;
1096#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001097 break;
1098
1099 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001100 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1101#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1102 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1103 return -1;
1104#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001105 break;
1106
Benny Prijonof3bbc132006-12-25 06:43:59 +00001107 case OPT_TLS_CERT_FILE:
1108 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1109#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1110 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1111 return -1;
1112#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001113 break;
1114
Benny Prijonof3bbc132006-12-25 06:43:59 +00001115 case OPT_TLS_PRIV_FILE:
1116 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1117 break;
1118
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001119 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001120 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1121#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1122 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1123 return -1;
1124#endif
1125 break;
1126
1127 case OPT_TLS_VERIFY_SERVER:
1128 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1129 break;
1130
1131 case OPT_TLS_VERIFY_CLIENT:
1132 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1133 break;
1134
1135 case OPT_TLS_NEG_TIMEOUT:
1136 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001137 break;
1138
Benny Prijono4e5d5512007-03-06 18:11:30 +00001139 case OPT_CAPTURE_DEV:
1140 cfg->capture_dev = atoi(pj_optarg);
1141 break;
1142
1143 case OPT_PLAYBACK_DEV:
1144 cfg->playback_dev = atoi(pj_optarg);
1145 break;
1146
Benny Prijonoebc32c32008-06-12 13:30:39 +00001147 case OPT_STDOUT_REFRESH:
1148 stdout_refresh = atoi(pj_optarg);
1149 break;
1150
1151 case OPT_STDOUT_REFRESH_TEXT:
1152 stdout_refresh_text = pj_optarg;
1153 break;
1154
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001155#ifdef _IONBF
1156 case OPT_STDOUT_NO_BUF:
1157 setvbuf(stdout, NULL, _IONBF, 0);
1158 break;
1159#endif
1160
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001161 case OPT_CAPTURE_LAT:
1162 cfg->capture_lat = atoi(pj_optarg);
1163 break;
1164
1165 case OPT_PLAYBACK_LAT:
1166 cfg->playback_lat = atoi(pj_optarg);
1167 break;
1168
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001169 case OPT_SND_AUTO_CLOSE:
1170 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1171 break;
1172
Benny Prijono91d20f42008-06-14 19:42:37 +00001173 case OPT_NO_TONES:
1174 cfg->no_tones = PJ_TRUE;
1175 break;
1176
Benny Prijono22a300a2006-06-14 20:04:55 +00001177 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001178 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001179 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001180 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001181 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001182 }
1183 }
1184
1185 if (pj_optind != argc) {
1186 pj_str_t uri_arg;
1187
1188 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1189 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1190 return -1;
1191 }
1192 uri_arg = pj_str(argv[pj_optind]);
1193 if (uri_to_call)
1194 *uri_to_call = uri_arg;
1195 pj_optind++;
1196
1197 /* Add URI to call to buddy list if it's not already there */
1198 for (i=0; i<cfg->buddy_cnt; ++i) {
1199 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1200 break;
1201 }
1202 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1203 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1204 }
1205
1206 } else {
1207 if (uri_to_call)
1208 uri_to_call->slen = 0;
1209 }
1210
1211 if (pj_optind != argc) {
1212 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1213 return PJ_EINVAL;
1214 }
1215
Benny Prijono56315612006-07-18 14:39:40 +00001216 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1217 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001218
1219 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001220 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1221
1222 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001223 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001224 acfg->cred_count++;
1225 }
1226
1227 /* When IMS mode is enabled for the account, verify that settings
1228 * are okay.
1229 */
1230 /* For now we check if IMS mode is activated by looking if
1231 * initial_auth is set.
1232 */
1233 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1234 /* Realm must point to the real domain */
1235 if (*acfg->cred_info[0].realm.ptr=='*') {
1236 PJ_LOG(1,(THIS_FILE,
1237 "Error: cannot use '*' as realm with IMS"));
1238 return PJ_EINVAL;
1239 }
1240
1241 /* Username for authentication must be in a@b format */
1242 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1243 PJ_LOG(1,(THIS_FILE,
1244 "Error: Username for authentication must "
1245 "be in user@domain format with IMS"));
1246 return PJ_EINVAL;
1247 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001248 }
1249 }
1250
1251
1252 return PJ_SUCCESS;
1253}
1254
1255
1256/*
1257 * Save account settings
1258 */
1259static void write_account_settings(int acc_index, pj_str_t *result)
1260{
1261 unsigned i;
1262 char line[128];
1263 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1264
1265
1266 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1267 pj_strcat2(result, line);
1268
1269
1270 /* Identity */
1271 if (acc_cfg->id.slen) {
1272 pj_ansi_sprintf(line, "--id %.*s\n",
1273 (int)acc_cfg->id.slen,
1274 acc_cfg->id.ptr);
1275 pj_strcat2(result, line);
1276 }
1277
1278 /* Registrar server */
1279 if (acc_cfg->reg_uri.slen) {
1280 pj_ansi_sprintf(line, "--registrar %.*s\n",
1281 (int)acc_cfg->reg_uri.slen,
1282 acc_cfg->reg_uri.ptr);
1283 pj_strcat2(result, line);
1284
1285 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1286 acc_cfg->reg_timeout);
1287 pj_strcat2(result, line);
1288 }
1289
1290 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001291 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001292 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001293 (int)acc_cfg->force_contact.slen,
1294 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001295 pj_strcat2(result, line);
1296 }
1297
Benny Prijonofce28542007-12-09 15:41:10 +00001298 /* */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001299 if (acc_cfg->allow_contact_rewrite==0)
Benny Prijonofce28542007-12-09 15:41:10 +00001300 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001301 pj_ansi_sprintf(line, "--contact-rewrite %i\n",
1302 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001303 pj_strcat2(result, line);
1304 }
1305
Benny Prijonod8179652008-01-23 20:39:07 +00001306#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1307 /* SRTP */
1308 if (acc_cfg->use_srtp) {
1309 pj_ansi_sprintf(line, "--use-srtp %i\n",
1310 (int)acc_cfg->use_srtp);
1311 pj_strcat2(result, line);
1312 }
1313#endif
1314
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001315 /* Proxy */
1316 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1317 pj_ansi_sprintf(line, "--proxy %.*s\n",
1318 (int)acc_cfg->proxy[i].slen,
1319 acc_cfg->proxy[i].ptr);
1320 pj_strcat2(result, line);
1321 }
1322
1323 /* Credentials */
1324 for (i=0; i<acc_cfg->cred_count; ++i) {
1325 if (acc_cfg->cred_info[i].realm.slen) {
1326 pj_ansi_sprintf(line, "--realm %.*s\n",
1327 (int)acc_cfg->cred_info[i].realm.slen,
1328 acc_cfg->cred_info[i].realm.ptr);
1329 pj_strcat2(result, line);
1330 }
1331
1332 if (acc_cfg->cred_info[i].username.slen) {
1333 pj_ansi_sprintf(line, "--username %.*s\n",
1334 (int)acc_cfg->cred_info[i].username.slen,
1335 acc_cfg->cred_info[i].username.ptr);
1336 pj_strcat2(result, line);
1337 }
1338
1339 if (acc_cfg->cred_info[i].data.slen) {
1340 pj_ansi_sprintf(line, "--password %.*s\n",
1341 (int)acc_cfg->cred_info[i].data.slen,
1342 acc_cfg->cred_info[i].data.ptr);
1343 pj_strcat2(result, line);
1344 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001345
1346 if (i != acc_cfg->cred_count - 1)
1347 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001348 }
1349
1350}
1351
1352
1353/*
1354 * Write settings.
1355 */
1356static int write_settings(const struct app_config *config,
1357 char *buf, pj_size_t max)
1358{
1359 unsigned acc_index;
1360 unsigned i;
1361 pj_str_t cfg;
1362 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001363 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001364
1365 PJ_UNUSED_ARG(max);
1366
1367 cfg.ptr = buf;
1368 cfg.slen = 0;
1369
1370 /* Logging. */
1371 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1372 pj_ansi_sprintf(line, "--log-level %d\n",
1373 config->log_cfg.level);
1374 pj_strcat2(&cfg, line);
1375
1376 pj_ansi_sprintf(line, "--app-log-level %d\n",
1377 config->log_cfg.console_level);
1378 pj_strcat2(&cfg, line);
1379
1380 if (config->log_cfg.log_filename.slen) {
1381 pj_ansi_sprintf(line, "--log-file %.*s\n",
1382 (int)config->log_cfg.log_filename.slen,
1383 config->log_cfg.log_filename.ptr);
1384 pj_strcat2(&cfg, line);
1385 }
1386
1387
1388 /* Save account settings. */
1389 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1390
1391 write_account_settings(acc_index, &cfg);
1392
1393 if (acc_index < config->acc_cnt-1)
1394 pj_strcat2(&cfg, "--next-account\n");
1395 }
1396
1397
1398 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1399
1400 /* Outbound proxy */
1401 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1402 pj_ansi_sprintf(line, "--outbound %.*s\n",
1403 (int)config->cfg.outbound_proxy[i].slen,
1404 config->cfg.outbound_proxy[i].ptr);
1405 pj_strcat2(&cfg, line);
1406 }
1407
1408
1409 /* UDP Transport. */
1410 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1411 pj_strcat2(&cfg, line);
1412
Benny Prijono0a5cad82006-09-26 13:21:02 +00001413 /* IP address, if any. */
1414 if (config->udp_cfg.public_addr.slen) {
1415 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1416 (int)config->udp_cfg.public_addr.slen,
1417 config->udp_cfg.public_addr.ptr);
1418 pj_strcat2(&cfg, line);
1419 }
1420
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001421 /* No TCP ? */
1422 if (config->no_tcp) {
1423 pj_strcat2(&cfg, "--no-tcp\n");
1424 }
1425
1426 /* No UDP ? */
1427 if (config->no_udp) {
1428 pj_strcat2(&cfg, "--no-udp\n");
1429 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001430
1431 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001432 if (config->cfg.stun_domain.slen) {
1433 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1434 (int)config->cfg.stun_domain.slen,
1435 config->cfg.stun_domain.ptr);
1436 pj_strcat2(&cfg, line);
1437 }
1438 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001439 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001440 (int)config->cfg.stun_host.slen,
1441 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001442 pj_strcat2(&cfg, line);
1443 }
1444
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001445 /* TLS */
1446 if (config->use_tls)
1447 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001448 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001449 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001450 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1451 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001452 pj_strcat2(&cfg, line);
1453 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001454 if (config->udp_cfg.tls_setting.cert_file.slen) {
1455 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1456 (int)config->udp_cfg.tls_setting.cert_file.slen,
1457 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001458 pj_strcat2(&cfg, line);
1459 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001460 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1461 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1462 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1463 config->udp_cfg.tls_setting.privkey_file.ptr);
1464 pj_strcat2(&cfg, line);
1465 }
1466
1467 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001468 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001469 (int)config->udp_cfg.tls_setting.password.slen,
1470 config->udp_cfg.tls_setting.password.ptr);
1471 pj_strcat2(&cfg, line);
1472 }
1473
1474 if (config->udp_cfg.tls_setting.verify_server)
1475 pj_strcat2(&cfg, "--tls-verify-server\n");
1476
1477 if (config->udp_cfg.tls_setting.verify_client)
1478 pj_strcat2(&cfg, "--tls-verify-client\n");
1479
1480 if (config->udp_cfg.tls_setting.timeout.sec) {
1481 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001482 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001483 pj_strcat2(&cfg, line);
1484 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001485
1486 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1487
Benny Prijonof6508982008-01-25 09:02:33 +00001488 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001489#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001490 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1491 pj_ansi_sprintf(line, "--use-srtp %d\n",
1492 app_config.cfg.use_srtp);
1493 pj_strcat2(&cfg, line);
1494 }
1495 if (app_config.cfg.srtp_secure_signaling !=
1496 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1497 {
1498 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1499 app_config.cfg.srtp_secure_signaling);
1500 pj_strcat2(&cfg, line);
1501 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001502#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001503
Benny Prijonof76e1392008-06-06 14:51:48 +00001504 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001505 if (config->media_cfg.enable_ice)
1506 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001507
1508 if (config->media_cfg.enable_turn)
1509 pj_strcat2(&cfg, "--use-turn\n");
1510
1511 if (config->media_cfg.ice_no_host_cands)
1512 pj_strcat2(&cfg, "--ice-no-host\n");
1513
1514 if (config->media_cfg.turn_server.slen) {
1515 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1516 (int)config->media_cfg.turn_server.slen,
1517 config->media_cfg.turn_server.ptr);
1518 pj_strcat2(&cfg, line);
1519 }
1520
1521 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1522 pj_strcat2(&cfg, "--turn-tcp\n");
1523
1524 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1525 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1526 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1527 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1528 pj_strcat2(&cfg, line);
1529 }
1530
1531 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1532 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1533 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1534 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1535 pj_strcat2(&cfg, line);
1536 }
1537
1538 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001539 if (config->null_audio)
1540 pj_strcat2(&cfg, "--null-audio\n");
1541 if (config->auto_play)
1542 pj_strcat2(&cfg, "--auto-play\n");
1543 if (config->auto_loop)
1544 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001545 if (config->auto_conf)
1546 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001547 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001548 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001549 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001550 pj_strcat2(&cfg, line);
1551 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001552 for (i=0; i<config->tone_count; ++i) {
1553 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1554 config->tones[i].freq1, config->tones[i].freq2,
1555 config->tones[i].on_msec, config->tones[i].off_msec);
1556 pj_strcat2(&cfg, line);
1557 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001558 if (config->rec_file.slen) {
1559 pj_ansi_sprintf(line, "--rec-file %s\n",
1560 config->rec_file.ptr);
1561 pj_strcat2(&cfg, line);
1562 }
1563 if (config->auto_rec)
1564 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001565 if (config->capture_dev != PJSUA_INVALID_ID) {
1566 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1567 pj_strcat2(&cfg, line);
1568 }
1569 if (config->playback_dev != PJSUA_INVALID_ID) {
1570 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1571 pj_strcat2(&cfg, line);
1572 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001573 if (config->media_cfg.snd_auto_close_time != -1) {
1574 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1575 config->media_cfg.snd_auto_close_time);
1576 pj_strcat2(&cfg, line);
1577 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001578 if (config->no_tones) {
1579 pj_strcat2(&cfg, "--no-tones\n");
1580 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001581
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001582 /* Sound device latency */
1583 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1584 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1585 pj_strcat2(&cfg, line);
1586 }
1587 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1588 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1589 pj_strcat2(&cfg, line);
1590 }
1591
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001592 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001593 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001594 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001595 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001596 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001597 } else {
1598 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1599 config->media_cfg.clock_rate);
1600 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001601 }
Benny Prijono70972992006-08-05 11:13:58 +00001602
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001603 if (config->media_cfg.snd_clock_rate &&
1604 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1605 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001606 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1607 config->media_cfg.snd_clock_rate);
1608 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001609 }
1610
Benny Prijono7d60d052008-03-29 12:24:20 +00001611 /* Stereo mode. */
1612 if (config->media_cfg.channel_count == 2) {
1613 pj_ansi_sprintf(line, "--stereo\n");
1614 pj_strcat2(&cfg, line);
1615 }
1616
Benny Prijono70972992006-08-05 11:13:58 +00001617 /* quality */
1618 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001619 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001620 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001621 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001622 } else {
1623 pj_ansi_sprintf(line, "#using default --quality %d\n",
1624 config->media_cfg.quality);
1625 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001626 }
Benny Prijono0498d902006-06-19 14:49:14 +00001627
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001628
1629 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001630 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001631 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001632 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001633 pj_strcat2(&cfg, line);
1634 }
1635
Benny Prijono70972992006-08-05 11:13:58 +00001636 /* no-vad */
1637 if (config->media_cfg.no_vad) {
1638 pj_strcat2(&cfg, "--no-vad\n");
1639 }
1640
1641 /* ec-tail */
1642 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1643 pj_ansi_sprintf(line, "--ec-tail %d\n",
1644 config->media_cfg.ec_tail_len);
1645 pj_strcat2(&cfg, line);
1646 } else {
1647 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1648 config->media_cfg.ec_tail_len);
1649 pj_strcat2(&cfg, line);
1650 }
1651
1652
1653 /* ilbc-mode */
1654 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1655 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1656 config->media_cfg.ilbc_mode);
1657 pj_strcat2(&cfg, line);
1658 } else {
1659 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1660 config->media_cfg.ilbc_mode);
1661 pj_strcat2(&cfg, line);
1662 }
1663
1664 /* RTP drop */
1665 if (config->media_cfg.tx_drop_pct) {
1666 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1667 config->media_cfg.tx_drop_pct);
1668 pj_strcat2(&cfg, line);
1669
1670 }
1671 if (config->media_cfg.rx_drop_pct) {
1672 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1673 config->media_cfg.rx_drop_pct);
1674 pj_strcat2(&cfg, line);
1675
1676 }
1677
1678
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001679 /* Start RTP port. */
1680 pj_ansi_sprintf(line, "--rtp-port %d\n",
1681 config->rtp_cfg.port);
1682 pj_strcat2(&cfg, line);
1683
1684 /* Add codec. */
1685 for (i=0; i<config->codec_cnt; ++i) {
1686 pj_ansi_sprintf(line, "--add-codec %s\n",
1687 config->codec_arg[i].ptr);
1688 pj_strcat2(&cfg, line);
1689 }
Benny Prijonofce28542007-12-09 15:41:10 +00001690 /* Disable codec */
1691 for (i=0; i<config->codec_dis_cnt; ++i) {
1692 pj_ansi_sprintf(line, "--dis-codec %s\n",
1693 config->codec_dis[i].ptr);
1694 pj_strcat2(&cfg, line);
1695 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001696
1697 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1698
1699 /* Auto-answer. */
1700 if (config->auto_answer != 0) {
1701 pj_ansi_sprintf(line, "--auto-answer %d\n",
1702 config->auto_answer);
1703 pj_strcat2(&cfg, line);
1704 }
1705
1706 /* Max calls. */
1707 pj_ansi_sprintf(line, "--max-calls %d\n",
1708 config->cfg.max_calls);
1709 pj_strcat2(&cfg, line);
1710
1711 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001712 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001713 pj_ansi_sprintf(line, "--duration %d\n",
1714 config->duration);
1715 pj_strcat2(&cfg, line);
1716 }
1717
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001718 /* norefersub ? */
1719 if (config->no_refersub) {
1720 pj_strcat2(&cfg, "--norefersub\n");
1721 }
1722
Benny Prijonofce28542007-12-09 15:41:10 +00001723 if (pjsip_use_compact_form)
1724 {
1725 pj_strcat2(&cfg, "--use-compact-form\n");
1726 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001727
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001728 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1729
1730 /* Add buddies. */
1731 for (i=0; i<config->buddy_cnt; ++i) {
1732 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1733 (int)config->buddy_cfg[i].uri.slen,
1734 config->buddy_cfg[i].uri.ptr);
1735 pj_strcat2(&cfg, line);
1736 }
1737
1738
1739 *(cfg.ptr + cfg.slen) = '\0';
1740 return cfg.slen;
1741}
1742
1743
1744/*
1745 * Dump application states.
1746 */
1747static void app_dump(pj_bool_t detail)
1748{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001749 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001750}
1751
1752
1753/*****************************************************************************
1754 * Console application
1755 */
1756
Benny Prijono91d20f42008-06-14 19:42:37 +00001757static void ringback_start(pjsua_call_id call_id)
1758{
1759 if (app_config.no_tones)
1760 return;
1761
1762 if (app_config.call_data[call_id].ringback_on)
1763 return;
1764
1765 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1766
1767 if (++app_config.ringback_cnt==1 &&
1768 app_config.ringback_slot!=PJSUA_INVALID_ID)
1769 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001770 pjsua_conf_connect(app_config.ringback_slot, 0);
1771 }
1772}
1773
1774static void ring_stop(pjsua_call_id call_id)
1775{
1776 if (app_config.no_tones)
1777 return;
1778
1779 if (app_config.call_data[call_id].ringback_on) {
1780 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1781
1782 pj_assert(app_config.ringback_cnt>0);
1783 if (--app_config.ringback_cnt == 0 &&
1784 app_config.ringback_slot!=PJSUA_INVALID_ID)
1785 {
1786 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001787 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001788 }
1789 }
1790
1791 if (app_config.call_data[call_id].ring_on) {
1792 app_config.call_data[call_id].ring_on = PJ_FALSE;
1793
1794 pj_assert(app_config.ring_cnt>0);
1795 if (--app_config.ring_cnt == 0 &&
1796 app_config.ring_slot!=PJSUA_INVALID_ID)
1797 {
1798 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001799 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001800 }
1801 }
1802}
1803
1804static void ring_start(pjsua_call_id call_id)
1805{
1806 if (app_config.no_tones)
1807 return;
1808
1809 if (app_config.call_data[call_id].ring_on)
1810 return;
1811
1812 app_config.call_data[call_id].ring_on = PJ_TRUE;
1813
1814 if (++app_config.ring_cnt==1 &&
1815 app_config.ring_slot!=PJSUA_INVALID_ID)
1816 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001817 pjsua_conf_connect(app_config.ring_slot, 0);
1818 }
1819}
1820
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001821/*
1822 * Find next call when current call is disconnected or when user
1823 * press ']'
1824 */
1825static pj_bool_t find_next_call(void)
1826{
1827 int i, max;
1828
1829 max = pjsua_call_get_max_count();
1830 for (i=current_call+1; i<max; ++i) {
1831 if (pjsua_call_is_active(i)) {
1832 current_call = i;
1833 return PJ_TRUE;
1834 }
1835 }
1836
1837 for (i=0; i<current_call; ++i) {
1838 if (pjsua_call_is_active(i)) {
1839 current_call = i;
1840 return PJ_TRUE;
1841 }
1842 }
1843
1844 current_call = PJSUA_INVALID_ID;
1845 return PJ_FALSE;
1846}
1847
1848
1849/*
1850 * Find previous call when user press '['
1851 */
1852static pj_bool_t find_prev_call(void)
1853{
1854 int i, max;
1855
1856 max = pjsua_call_get_max_count();
1857 for (i=current_call-1; i>=0; --i) {
1858 if (pjsua_call_is_active(i)) {
1859 current_call = i;
1860 return PJ_TRUE;
1861 }
1862 }
1863
1864 for (i=max-1; i>current_call; --i) {
1865 if (pjsua_call_is_active(i)) {
1866 current_call = i;
1867 return PJ_TRUE;
1868 }
1869 }
1870
1871 current_call = PJSUA_INVALID_ID;
1872 return PJ_FALSE;
1873}
1874
1875
Benny Prijono804ff0a2006-09-14 11:17:48 +00001876/* Callback from timer when the maximum call duration has been
1877 * exceeded.
1878 */
1879static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1880 struct pj_timer_entry *entry)
1881{
1882 pjsua_call_id call_id = entry->id;
1883 pjsua_msg_data msg_data;
1884 pjsip_generic_string_hdr warn;
1885 pj_str_t hname = pj_str("Warning");
1886 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1887
1888 PJ_UNUSED_ARG(timer_heap);
1889
Benny Prijono148c9dd2006-09-19 13:37:53 +00001890 if (call_id == PJSUA_INVALID_ID) {
1891 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1892 return;
1893 }
1894
Benny Prijono804ff0a2006-09-14 11:17:48 +00001895 /* Add warning header */
1896 pjsua_msg_data_init(&msg_data);
1897 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1898 pj_list_push_back(&msg_data.hdr_list, &warn);
1899
1900 /* Call duration has been exceeded; disconnect the call */
1901 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1902 "for call %d, disconnecting the call",
1903 app_config.duration, call_id));
1904 entry->id = PJSUA_INVALID_ID;
1905 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1906}
1907
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001908
1909/*
1910 * Handler when invite state has changed.
1911 */
1912static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1913{
1914 pjsua_call_info call_info;
1915
1916 PJ_UNUSED_ARG(e);
1917
1918 pjsua_call_get_info(call_id, &call_info);
1919
1920 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1921
Benny Prijono91d20f42008-06-14 19:42:37 +00001922 /* Stop all ringback for this call */
1923 ring_stop(call_id);
1924
Benny Prijono804ff0a2006-09-14 11:17:48 +00001925 /* Cancel duration timer, if any */
1926 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1927 struct call_data *cd = &app_config.call_data[call_id];
1928 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1929
1930 cd->timer.id = PJSUA_INVALID_ID;
1931 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1932 }
1933
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00001934 /* Rewind play file when hangup automatically,
1935 * since file is not looped
1936 */
1937 if (app_config.auto_play_hangup)
1938 pjsua_player_set_pos(app_config.wav_id, 0);
1939
1940
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001941 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1942 call_id,
1943 call_info.last_status,
1944 call_info.last_status_text.ptr));
1945
1946 if (call_id == current_call) {
1947 find_next_call();
1948 }
1949
Benny Prijono4be63b52006-11-25 14:50:25 +00001950 /* Dump media state upon disconnected */
1951 if (1) {
Benny Prijono6eddd872008-03-21 13:46:08 +00001952 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1953 sizeof(some_buf), " ");
Benny Prijono4be63b52006-11-25 14:50:25 +00001954 PJ_LOG(5,(THIS_FILE,
1955 "Call %d disconnected, dumping media stats\n%s",
Benny Prijono6eddd872008-03-21 13:46:08 +00001956 call_id, some_buf));
Benny Prijono4be63b52006-11-25 14:50:25 +00001957 }
1958
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001959 } else {
1960
Benny Prijono804ff0a2006-09-14 11:17:48 +00001961 if (app_config.duration!=NO_LIMIT &&
1962 call_info.state == PJSIP_INV_STATE_CONFIRMED)
1963 {
1964 /* Schedule timer to hangup call after the specified duration */
1965 struct call_data *cd = &app_config.call_data[call_id];
1966 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1967 pj_time_val delay;
1968
1969 cd->timer.id = call_id;
1970 delay.sec = app_config.duration;
1971 delay.msec = 0;
1972 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
1973 }
1974
Benny Prijono4be63b52006-11-25 14:50:25 +00001975 if (call_info.state == PJSIP_INV_STATE_EARLY) {
1976 int code;
1977 pj_str_t reason;
1978 pjsip_msg *msg;
1979
1980 /* This can only occur because of TX or RX message */
1981 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
1982
1983 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1984 msg = e->body.tsx_state.src.rdata->msg_info.msg;
1985 } else {
1986 msg = e->body.tsx_state.src.tdata->msg;
1987 }
1988
1989 code = msg->line.status.code;
1990 reason = msg->line.status.reason;
1991
Benny Prijono91d20f42008-06-14 19:42:37 +00001992 /* Start ringback for 180 for UAC unless there's SDP in 180 */
1993 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
1994 msg->body == NULL &&
1995 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
1996 {
1997 ringback_start(call_id);
1998 }
1999
Benny Prijono4be63b52006-11-25 14:50:25 +00002000 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2001 call_id, call_info.state_text.ptr,
2002 code, (int)reason.slen, reason.ptr));
2003 } else {
2004 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2005 call_id,
2006 call_info.state_text.ptr));
2007 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002008
2009 if (current_call==PJSUA_INVALID_ID)
2010 current_call = call_id;
2011
2012 }
2013}
2014
2015
2016/**
2017 * Handler when there is incoming call.
2018 */
2019static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2020 pjsip_rx_data *rdata)
2021{
2022 pjsua_call_info call_info;
2023
2024 PJ_UNUSED_ARG(acc_id);
2025 PJ_UNUSED_ARG(rdata);
2026
2027 pjsua_call_get_info(call_id, &call_info);
2028
Benny Prijono91d20f42008-06-14 19:42:37 +00002029 /* Start ringback */
2030 ring_start(call_id);
2031
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002032 if (app_config.auto_answer > 0) {
2033 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2034 }
2035
2036 if (app_config.auto_answer < 200) {
2037 PJ_LOG(3,(THIS_FILE,
2038 "Incoming call for account %d!\n"
2039 "From: %s\n"
2040 "To: %s\n"
2041 "Press a to answer or h to reject call",
2042 acc_id,
2043 call_info.remote_info.ptr,
2044 call_info.local_info.ptr));
2045 }
2046}
2047
2048
2049/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002050 * Handler when a transaction within a call has changed state.
2051 */
2052static void on_call_tsx_state(pjsua_call_id call_id,
2053 pjsip_transaction *tsx,
2054 pjsip_event *e)
2055{
2056 const pjsip_method info_method =
2057 {
2058 PJSIP_OTHER_METHOD,
2059 { "INFO", 4 }
2060 };
2061
2062 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2063 /*
2064 * Handle INFO method.
2065 */
2066 if (tsx->role == PJSIP_ROLE_UAC &&
2067 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002068 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2069 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002070 {
2071 /* Status of outgoing INFO request */
2072 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2073 PJ_LOG(4,(THIS_FILE,
2074 "Call %d: DTMF sent successfully with INFO",
2075 call_id));
2076 } else if (tsx->status_code >= 300) {
2077 PJ_LOG(4,(THIS_FILE,
2078 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2079 call_id,
2080 tsx->status_code,
2081 (int)tsx->status_text.slen,
2082 tsx->status_text.ptr));
2083 }
2084 } else if (tsx->role == PJSIP_ROLE_UAS &&
2085 tsx->state == PJSIP_TSX_STATE_TRYING)
2086 {
2087 /* Answer incoming INFO with 200/OK */
2088 pjsip_rx_data *rdata;
2089 pjsip_tx_data *tdata;
2090 pj_status_t status;
2091
2092 rdata = e->body.tsx_state.src.rdata;
2093
2094 if (rdata->msg_info.msg->body) {
2095 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2096 200, NULL, &tdata);
2097 if (status == PJ_SUCCESS)
2098 status = pjsip_tsx_send_msg(tsx, tdata);
2099
2100 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2101 call_id,
2102 (int)rdata->msg_info.msg->body->len,
2103 rdata->msg_info.msg->body->data));
2104 } else {
2105 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2106 400, NULL, &tdata);
2107 if (status == PJ_SUCCESS)
2108 status = pjsip_tsx_send_msg(tsx, tdata);
2109 }
2110 }
2111 }
2112}
2113
2114
2115/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002116 * Callback on media state changed event.
2117 * The action may connect the call to sound device, to file, or
2118 * to loop the call.
2119 */
2120static void on_call_media_state(pjsua_call_id call_id)
2121{
2122 pjsua_call_info call_info;
2123
2124 pjsua_call_get_info(call_id, &call_info);
2125
Benny Prijono91d20f42008-06-14 19:42:37 +00002126 /* Stop ringback */
2127 ring_stop(call_id);
2128
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002129 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
2130 pj_bool_t connect_sound = PJ_TRUE;
2131
2132 /* Loopback sound, if desired */
2133 if (app_config.auto_loop) {
2134 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2135 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00002136
2137 /* Automatically record conversation, if desired */
2138 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2139 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2140 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002141 }
2142
2143 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002144 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2145 app_config.wav_port != PJSUA_INVALID_ID)
2146 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002147 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2148 connect_sound = PJ_FALSE;
2149 }
2150
Benny Prijono7ca96da2006-08-07 12:11:40 +00002151 /* Put call in conference with other calls, if desired */
2152 if (app_config.auto_conf) {
2153 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002154 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002155 unsigned i;
2156
2157 /* Get all calls, and establish media connection between
2158 * this call and other calls.
2159 */
2160 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002161
Benny Prijono7ca96da2006-08-07 12:11:40 +00002162 for (i=0; i<call_cnt; ++i) {
2163 if (call_ids[i] == call_id)
2164 continue;
2165
2166 if (!pjsua_call_has_media(call_ids[i]))
2167 continue;
2168
2169 pjsua_conf_connect(call_info.conf_slot,
2170 pjsua_call_get_conf_port(call_ids[i]));
2171 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2172 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002173
2174 /* Automatically record conversation, if desired */
2175 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2176 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2177 app_config.rec_port);
2178 }
2179
Benny Prijono7ca96da2006-08-07 12:11:40 +00002180 }
2181
2182 /* Also connect call to local sound device */
2183 connect_sound = PJ_TRUE;
2184 }
2185
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002186 /* Otherwise connect to sound device */
2187 if (connect_sound) {
2188 pjsua_conf_connect(call_info.conf_slot, 0);
2189 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002190
2191 /* Automatically record conversation, if desired */
2192 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2193 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2194 pjsua_conf_connect(0, app_config.rec_port);
2195 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002196 }
2197
2198 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
2199
2200 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
2201 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2202 call_id));
2203 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
2204 PJ_LOG(3,(THIS_FILE,
2205 "Media for call %d is suspended (hold) by remote",
2206 call_id));
Benny Prijono096c56c2007-09-15 08:30:16 +00002207 } else if (call_info.media_status == PJSUA_CALL_MEDIA_ERROR) {
2208 pj_str_t reason = pj_str("ICE negotiation failed");
2209
2210 PJ_LOG(1,(THIS_FILE,
2211 "Media has reported error, disconnecting call"));
2212
2213 pjsua_call_hangup(call_id, 500, &reason, NULL);
2214
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002215 } else {
2216 PJ_LOG(3,(THIS_FILE,
2217 "Media for call %d is inactive",
2218 call_id));
2219 }
2220}
2221
Benny Prijono0875ae82006-12-26 00:11:48 +00002222/*
2223 * DTMF callback.
2224 */
2225static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2226{
2227 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2228}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002229
2230/*
2231 * Handler registration status has changed.
2232 */
2233static void on_reg_state(pjsua_acc_id acc_id)
2234{
2235 PJ_UNUSED_ARG(acc_id);
2236
2237 // Log already written.
2238}
2239
2240
2241/*
2242 * Handler on buddy state changed.
2243 */
2244static void on_buddy_state(pjsua_buddy_id buddy_id)
2245{
2246 pjsua_buddy_info info;
2247 pjsua_buddy_get_info(buddy_id, &info);
2248
2249 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2250 (int)info.uri.slen,
2251 info.uri.ptr,
2252 (int)info.status_text.slen,
2253 info.status_text.ptr));
2254}
2255
2256
2257/**
2258 * Incoming IM message (i.e. MESSAGE request)!
2259 */
2260static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2261 const pj_str_t *to, const pj_str_t *contact,
2262 const pj_str_t *mime_type, const pj_str_t *text)
2263{
2264 /* Note: call index may be -1 */
2265 PJ_UNUSED_ARG(call_id);
2266 PJ_UNUSED_ARG(to);
2267 PJ_UNUSED_ARG(contact);
2268 PJ_UNUSED_ARG(mime_type);
2269
Benny Prijonof4b538d2007-05-14 16:45:20 +00002270 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002271 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002272 (int)text->slen, text->ptr,
2273 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002274}
2275
2276
2277/**
2278 * Received typing indication
2279 */
2280static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2281 const pj_str_t *to, const pj_str_t *contact,
2282 pj_bool_t is_typing)
2283{
2284 PJ_UNUSED_ARG(call_id);
2285 PJ_UNUSED_ARG(to);
2286 PJ_UNUSED_ARG(contact);
2287
2288 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2289 (int)from->slen, from->ptr,
2290 (is_typing?"is typing..":"has stopped typing")));
2291}
2292
2293
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002294/**
2295 * Call transfer request status.
2296 */
2297static void on_call_transfer_status(pjsua_call_id call_id,
2298 int status_code,
2299 const pj_str_t *status_text,
2300 pj_bool_t final,
2301 pj_bool_t *p_cont)
2302{
2303 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2304 call_id, status_code,
2305 (int)status_text->slen, status_text->ptr,
2306 (final ? "[final]" : "")));
2307
2308 if (status_code/100 == 2) {
2309 PJ_LOG(3,(THIS_FILE,
2310 "Call %d: call transfered successfully, disconnecting call",
2311 call_id));
2312 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2313 *p_cont = PJ_FALSE;
2314 }
2315}
2316
2317
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002318/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002319 * Notification that call is being replaced.
2320 */
2321static void on_call_replaced(pjsua_call_id old_call_id,
2322 pjsua_call_id new_call_id)
2323{
2324 pjsua_call_info old_ci, new_ci;
2325
2326 pjsua_call_get_info(old_call_id, &old_ci);
2327 pjsua_call_get_info(new_call_id, &new_ci);
2328
2329 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2330 "call %d with %.*s",
2331 old_call_id,
2332 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2333 new_call_id,
2334 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2335}
2336
2337
2338/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002339 * NAT type detection callback.
2340 */
2341static void on_nat_detect(const pj_stun_nat_detect_result *res)
2342{
2343 if (res->status != PJ_SUCCESS) {
2344 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2345 } else {
2346 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2347 }
2348}
2349
2350
2351/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002352 * Print buddy list.
2353 */
2354static void print_buddy_list(void)
2355{
2356 pjsua_buddy_id ids[64];
2357 int i;
2358 unsigned count = PJ_ARRAY_SIZE(ids);
2359
2360 puts("Buddy list:");
2361
2362 pjsua_enum_buddies(ids, &count);
2363
2364 if (count == 0)
2365 puts(" -none-");
2366 else {
2367 for (i=0; i<(int)count; ++i) {
2368 pjsua_buddy_info info;
2369
2370 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2371 continue;
2372
Benny Prijono4461c7d2007-08-25 13:36:15 +00002373 printf(" [%2d] <%.*s> %.*s\n",
2374 ids[i]+1,
2375 (int)info.status_text.slen,
2376 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002377 (int)info.uri.slen,
2378 info.uri.ptr);
2379 }
2380 }
2381 puts("");
2382}
2383
2384
2385/*
2386 * Print account status.
2387 */
2388static void print_acc_status(int acc_id)
2389{
2390 char buf[80];
2391 pjsua_acc_info info;
2392
2393 pjsua_acc_get_info(acc_id, &info);
2394
2395 if (!info.has_registration) {
2396 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2397 (int)info.status_text.slen,
2398 info.status_text.ptr);
2399
2400 } else {
2401 pj_ansi_snprintf(buf, sizeof(buf),
2402 "%d/%.*s (expires=%d)",
2403 info.status,
2404 (int)info.status_text.slen,
2405 info.status_text.ptr,
2406 info.expires);
2407
2408 }
2409
2410 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2411 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002412 printf(" Online status: %.*s\n",
2413 (int)info.online_status_text.slen,
2414 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002415}
2416
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002417/* Playfile done notification, set timer to hangup calls */
2418pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2419{
2420 pj_time_val delay;
2421
2422 PJ_UNUSED_ARG(port);
2423 PJ_UNUSED_ARG(usr_data);
2424
2425 /* Just rewind WAV when it is played outside of call */
2426 if (pjsua_call_get_count() == 0) {
2427 pjsua_player_set_pos(app_config.wav_id, 0);
2428 return PJ_SUCCESS;
2429 }
2430
2431 /* Timer is already active */
2432 if (app_config.auto_hangup_timer.id == 1)
2433 return PJ_SUCCESS;
2434
2435 app_config.auto_hangup_timer.id = 1;
2436 delay.sec = 0;
2437 delay.msec = 200; /* Give 200 ms before hangup */
2438 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2439 &app_config.auto_hangup_timer,
2440 &delay);
2441
2442 return PJ_SUCCESS;
2443}
2444
2445/* Auto hangup timer callback */
2446static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2447 struct pj_timer_entry *entry)
2448{
2449 PJ_UNUSED_ARG(timer_heap);
2450 PJ_UNUSED_ARG(entry);
2451
2452 app_config.auto_hangup_timer.id = 0;
2453 pjsua_call_hangup_all();
2454}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002455
2456/*
2457 * Show a bit of help.
2458 */
2459static void keystroke_help(void)
2460{
2461 pjsua_acc_id acc_ids[16];
2462 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2463 int i;
2464
2465 printf(">>>>\n");
2466
2467 pjsua_enum_accs(acc_ids, &count);
2468
2469 printf("Account list:\n");
2470 for (i=0; i<(int)count; ++i)
2471 print_acc_status(acc_ids[i]);
2472
2473 print_buddy_list();
2474
2475 //puts("Commands:");
2476 puts("+=============================================================================+");
2477 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2478 puts("| | | |");
2479 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2480 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002481 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2482 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2483 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2484 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002485 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2486 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002487 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002488 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002489 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2490 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2491 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2492 puts("| | V Adjust audio Volume | f Save config |");
2493 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002494 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002495 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002496 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002497
2498 i = pjsua_call_get_count();
2499 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002500
2501 if (current_call != PJSUA_INVALID_ID) {
2502 pjsua_call_info ci;
2503 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2504 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2505 (int)ci.remote_info.slen, ci.remote_info.ptr,
2506 (int)ci.state_text.slen, ci.state_text.ptr);
2507 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002508}
2509
2510
2511/*
2512 * Input simple string
2513 */
2514static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2515{
2516 char *p;
2517
2518 printf("%s (empty to cancel): ", title); fflush(stdout);
2519 fgets(buf, len, stdin);
2520
2521 /* Remove trailing newlines. */
2522 for (p=buf; ; ++p) {
2523 if (*p=='\r' || *p=='\n') *p='\0';
2524 else if (!*p) break;
2525 }
2526
2527 if (!*buf)
2528 return PJ_FALSE;
2529
2530 return PJ_TRUE;
2531}
2532
2533
2534#define NO_NB -2
2535struct input_result
2536{
2537 int nb_result;
2538 char *uri_result;
2539};
2540
2541
2542/*
2543 * Input URL.
2544 */
2545static void ui_input_url(const char *title, char *buf, int len,
2546 struct input_result *result)
2547{
2548 result->nb_result = NO_NB;
2549 result->uri_result = NULL;
2550
2551 print_buddy_list();
2552
2553 printf("Choices:\n"
2554 " 0 For current dialog.\n"
2555 " -1 All %d buddies in buddy list\n"
2556 " [1 -%2d] Select from buddy list\n"
2557 " URL An URL\n"
2558 " <Enter> Empty input (or 'q') to cancel\n"
2559 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2560 printf("%s: ", title);
2561
2562 fflush(stdout);
2563 fgets(buf, len, stdin);
2564 len = strlen(buf);
2565
2566 /* Left trim */
2567 while (pj_isspace(*buf)) {
2568 ++buf;
2569 --len;
2570 }
2571
2572 /* Remove trailing newlines */
2573 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2574 buf[--len] = '\0';
2575
2576 if (len == 0 || buf[0]=='q')
2577 return;
2578
2579 if (pj_isdigit(*buf) || *buf=='-') {
2580
2581 int i;
2582
2583 if (*buf=='-')
2584 i = 1;
2585 else
2586 i = 0;
2587
2588 for (; i<len; ++i) {
2589 if (!pj_isdigit(buf[i])) {
2590 puts("Invalid input");
2591 return;
2592 }
2593 }
2594
2595 result->nb_result = my_atoi(buf);
2596
2597 if (result->nb_result >= 0 &&
2598 result->nb_result <= (int)pjsua_get_buddy_count())
2599 {
2600 return;
2601 }
2602 if (result->nb_result == -1)
2603 return;
2604
2605 puts("Invalid input");
2606 result->nb_result = NO_NB;
2607 return;
2608
2609 } else {
2610 pj_status_t status;
2611
2612 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2613 pjsua_perror(THIS_FILE, "Invalid URL", status);
2614 return;
2615 }
2616
2617 result->uri_result = buf;
2618 }
2619}
2620
2621/*
2622 * List the ports in conference bridge
2623 */
2624static void conf_list(void)
2625{
2626 unsigned i, count;
2627 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2628
2629 printf("Conference ports:\n");
2630
2631 count = PJ_ARRAY_SIZE(id);
2632 pjsua_enum_conf_ports(id, &count);
2633
2634 for (i=0; i<count; ++i) {
2635 char txlist[PJSUA_MAX_CALLS*4+10];
2636 unsigned j;
2637 pjsua_conf_port_info info;
2638
2639 pjsua_conf_get_port_info(id[i], &info);
2640
2641 txlist[0] = '\0';
2642 for (j=0; j<info.listener_cnt; ++j) {
2643 char s[10];
2644 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2645 pj_ansi_strcat(txlist, s);
2646 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002647 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002648 info.slot_id,
2649 info.clock_rate/1000,
2650 info.samples_per_frame * 1000 / info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002651 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002652 (int)info.name.slen,
2653 info.name.ptr,
2654 txlist);
2655
2656 }
2657 puts("");
2658}
2659
2660
2661/*
Benny Prijono56315612006-07-18 14:39:40 +00002662 * Send arbitrary request to remote host
2663 */
2664static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2665{
2666 pj_str_t str_method;
2667 pjsip_method method;
2668 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002669 pjsip_endpoint *endpt;
2670 pj_status_t status;
2671
2672 endpt = pjsua_get_pjsip_endpt();
2673
2674 str_method = pj_str(cstr_method);
2675 pjsip_method_init_np(&method, &str_method);
2676
Benny Prijonofff245c2007-04-02 11:44:47 +00002677 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002678
Benny Prijonob988d762007-12-05 04:30:04 +00002679 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002680 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002681 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002682 return;
2683 }
2684}
2685
2686
2687/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002688 * Change extended online status.
2689 */
2690static void change_online_status(void)
2691{
2692 char menuin[32];
2693 pj_bool_t online_status;
2694 pjrpid_element elem;
2695 int i, choice;
2696
2697 enum {
2698 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2699 };
2700
2701 struct opt {
2702 int id;
2703 char *name;
2704 } opts[] = {
2705 { AVAILABLE, "Available" },
2706 { BUSY, "Busy"},
2707 { OTP, "On the phone"},
2708 { IDLE, "Idle"},
2709 { AWAY, "Away"},
2710 { BRB, "Be right back"},
2711 { OFFLINE, "Offline"}
2712 };
2713
2714 printf("\n"
2715 "Choices:\n");
2716 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2717 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2718 }
2719
2720 if (!simple_input("Select status", menuin, sizeof(menuin)))
2721 return;
2722
2723 choice = atoi(menuin) - 1;
2724 if (choice < 0 || choice >= OPT_MAX) {
2725 puts("Invalid selection");
2726 return;
2727 }
2728
2729 pj_bzero(&elem, sizeof(elem));
2730 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2731
2732 online_status = PJ_TRUE;
2733
2734 switch (choice) {
2735 case AVAILABLE:
2736 break;
2737 case BUSY:
2738 elem.activity = PJRPID_ACTIVITY_BUSY;
2739 elem.note = pj_str("Busy");
2740 break;
2741 case OTP:
2742 elem.activity = PJRPID_ACTIVITY_BUSY;
2743 elem.note = pj_str("On the phone");
2744 break;
2745 case IDLE:
2746 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2747 elem.note = pj_str("Idle");
2748 break;
2749 case AWAY:
2750 elem.activity = PJRPID_ACTIVITY_AWAY;
2751 elem.note = pj_str("Away");
2752 break;
2753 case BRB:
2754 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2755 elem.note = pj_str("Be right back");
2756 break;
2757 case OFFLINE:
2758 online_status = PJ_FALSE;
2759 break;
2760 }
2761
2762 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2763}
2764
2765
2766/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002767 * Change codec priorities.
2768 */
2769static void manage_codec_prio(void)
2770{
2771 pjsua_codec_info c[32];
2772 unsigned i, count = PJ_ARRAY_SIZE(c);
2773 char input[32];
2774 char *codec, *prio;
2775 pj_str_t id;
2776 int new_prio;
2777 pj_status_t status;
2778
2779 printf("List of codecs:\n");
2780
2781 pjsua_enum_codecs(c, &count);
2782 for (i=0; i<count; ++i) {
2783 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2784 c[i].codec_id.ptr);
2785 }
2786
2787 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00002788 puts("Enter codec id and its new priority "
2789 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002790
Benny Prijono88accae2008-06-26 15:48:14 +00002791 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002792 fgets(input, sizeof(input), stdin);
2793 if (input[0]=='\r' || input[0]=='\n') {
2794 puts("Done");
2795 return;
2796 }
2797
2798 codec = strtok(input, " \t\r\n");
2799 prio = strtok(NULL, " \r\n");
2800
2801 if (!codec || !prio) {
2802 puts("Invalid input");
2803 return;
2804 }
2805
2806 new_prio = atoi(prio);
2807 if (new_prio < 0)
2808 new_prio = 0;
2809 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2810 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2811
2812 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2813 (pj_uint8_t)new_prio);
2814 if (status != PJ_SUCCESS)
2815 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2816}
2817
2818
2819/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002820 * Main "user interface" loop.
2821 */
2822void console_app_main(const pj_str_t *uri_to_call)
2823{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002824 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002825 char buf[128];
2826 char text[128];
2827 int i, count;
2828 char *uri;
2829 pj_str_t tmp;
2830 struct input_result result;
2831 pjsua_call_info call_info;
2832 pjsua_acc_info acc_info;
2833
2834
2835 /* If user specifies URI to call, then call the URI */
2836 if (uri_to_call->slen) {
2837 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2838 }
2839
2840 keystroke_help();
2841
2842 for (;;) {
2843
2844 printf(">>> ");
2845 fflush(stdout);
2846
Benny Prijono990042e2007-01-21 19:36:00 +00002847 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2848 /*
2849 * Be friendly to users who redirect commands into
2850 * program, when file ends, resume with kbd.
2851 * If exit is desired end script with q for quit
2852 */
2853 /* Reopen stdin/stdout/stderr to /dev/console */
2854#if defined(PJ_WIN32) && PJ_WIN32!=0
2855 if (freopen ("CONIN$", "r", stdin) == NULL) {
2856#else
2857 if (1) {
2858#endif
2859 puts("Cannot switch back to console from file redirection");
2860 menuin[0] = 'q';
2861 menuin[1] = '\0';
2862 } else {
2863 puts("Switched back to console from file redirection");
2864 continue;
2865 }
2866 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002867
Benny Prijonoebc32c32008-06-12 13:30:39 +00002868 if (cmd_echo) {
2869 printf("%s", menuin);
2870 }
2871
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002872 switch (menuin[0]) {
2873
2874 case 'm':
2875 /* Make call! : */
2876 printf("(You currently have %d calls)\n",
2877 pjsua_call_get_count());
2878
2879 uri = NULL;
2880 ui_input_url("Make call", buf, sizeof(buf), &result);
2881 if (result.nb_result != NO_NB) {
2882
2883 if (result.nb_result == -1 || result.nb_result == 0) {
2884 puts("You can't do that with make call!");
2885 continue;
2886 } else {
2887 pjsua_buddy_info binfo;
2888 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2889 uri = binfo.uri.ptr;
2890 }
2891
2892 } else if (result.uri_result) {
2893 uri = result.uri_result;
2894 }
2895
2896 tmp = pj_str(uri);
2897 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2898 break;
2899
2900 case 'M':
2901 /* Make multiple calls! : */
2902 printf("(You currently have %d calls)\n",
2903 pjsua_call_get_count());
2904
2905 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2906 continue;
2907
2908 count = my_atoi(menuin);
2909 if (count < 1)
2910 continue;
2911
2912 ui_input_url("Make call", buf, sizeof(buf), &result);
2913 if (result.nb_result != NO_NB) {
2914 pjsua_buddy_info binfo;
2915 if (result.nb_result == -1 || result.nb_result == 0) {
2916 puts("You can't do that with make call!");
2917 continue;
2918 }
2919 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2920 uri = binfo.uri.ptr;
2921 } else {
2922 uri = result.uri_result;
2923 }
2924
2925 for (i=0; i<my_atoi(menuin); ++i) {
2926 pj_status_t status;
2927
2928 tmp = pj_str(uri);
2929 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2930 NULL, NULL);
2931 if (status != PJ_SUCCESS)
2932 break;
2933 }
2934 break;
2935
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002936 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00002937 i = pjsua_detect_nat_type();
2938 if (i != PJ_SUCCESS)
2939 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002940 break;
2941
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002942 case 'i':
2943 /* Send instant messaeg */
2944
2945 /* i is for call index to send message, if any */
2946 i = -1;
2947
2948 /* Make compiler happy. */
2949 uri = NULL;
2950
2951 /* Input destination. */
2952 ui_input_url("Send IM to", buf, sizeof(buf), &result);
2953 if (result.nb_result != NO_NB) {
2954
2955 if (result.nb_result == -1) {
2956 puts("You can't send broadcast IM like that!");
2957 continue;
2958
2959 } else if (result.nb_result == 0) {
2960
2961 i = current_call;
2962
2963 } else {
2964 pjsua_buddy_info binfo;
2965 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2966 uri = binfo.uri.ptr;
2967 }
2968
2969 } else if (result.uri_result) {
2970 uri = result.uri_result;
2971 }
2972
2973
2974 /* Send typing indication. */
2975 if (i != -1)
2976 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
2977 else {
2978 pj_str_t tmp_uri = pj_str(uri);
2979 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
2980 }
2981
2982 /* Input the IM . */
2983 if (!simple_input("Message", text, sizeof(text))) {
2984 /*
2985 * Cancelled.
2986 * Send typing notification too, saying we're not typing.
2987 */
2988 if (i != -1)
2989 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
2990 else {
2991 pj_str_t tmp_uri = pj_str(uri);
2992 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
2993 }
2994 continue;
2995 }
2996
2997 tmp = pj_str(text);
2998
2999 /* Send the IM */
3000 if (i != -1)
3001 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3002 else {
3003 pj_str_t tmp_uri = pj_str(uri);
3004 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3005 }
3006
3007 break;
3008
3009 case 'a':
3010
3011 if (current_call != -1) {
3012 pjsua_call_get_info(current_call, &call_info);
3013 } else {
3014 /* Make compiler happy */
3015 call_info.role = PJSIP_ROLE_UAC;
3016 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3017 }
3018
3019 if (current_call == -1 ||
3020 call_info.role != PJSIP_ROLE_UAS ||
3021 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3022 {
3023 puts("No pending incoming call");
3024 fflush(stdout);
3025 continue;
3026
3027 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003028 int st_code;
3029 char contact[120];
3030 pj_str_t hname = { "Contact", 7 };
3031 pj_str_t hvalue;
3032 pjsip_generic_string_hdr hcontact;
3033 pjsua_msg_data msg_data;
3034
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003035 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3036 continue;
3037
Benny Prijono20d36722007-02-22 14:52:24 +00003038 st_code = my_atoi(buf);
3039 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003040 continue;
3041
Benny Prijono20d36722007-02-22 14:52:24 +00003042 pjsua_msg_data_init(&msg_data);
3043
3044 if (st_code/100 == 3) {
3045 if (!simple_input("Enter URL to be put in Contact",
3046 contact, sizeof(contact)))
3047 continue;
3048 hvalue = pj_str(contact);
3049 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3050
3051 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3052 }
3053
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003054 /*
3055 * Must check again!
3056 * Call may have been disconnected while we're waiting for
3057 * keyboard input.
3058 */
3059 if (current_call == -1) {
3060 puts("Call has been disconnected");
3061 fflush(stdout);
3062 continue;
3063 }
3064
Benny Prijono20d36722007-02-22 14:52:24 +00003065 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003066 }
3067
3068 break;
3069
3070
3071 case 'h':
3072
3073 if (current_call == -1) {
3074 puts("No current call");
3075 fflush(stdout);
3076 continue;
3077
3078 } else if (menuin[1] == 'a') {
3079
3080 /* Hangup all calls */
3081 pjsua_call_hangup_all();
3082
3083 } else {
3084
3085 /* Hangup current calls */
3086 pjsua_call_hangup(current_call, 0, NULL, NULL);
3087 }
3088 break;
3089
3090 case ']':
3091 case '[':
3092 /*
3093 * Cycle next/prev dialog.
3094 */
3095 if (menuin[0] == ']') {
3096 find_next_call();
3097
3098 } else {
3099 find_prev_call();
3100 }
3101
3102 if (current_call != -1) {
3103
3104 pjsua_call_get_info(current_call, &call_info);
3105 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3106 (int)call_info.remote_info.slen,
3107 call_info.remote_info.ptr));
3108
3109 } else {
3110 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3111 }
3112 break;
3113
3114
3115 case '>':
3116 case '<':
3117 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3118 break;
3119
3120 i = my_atoi(buf);
3121 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003122 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003123 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3124 } else {
3125 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3126 }
3127 break;
3128
3129
3130 case '+':
3131 if (menuin[1] == 'b') {
3132
3133 pjsua_buddy_config buddy_cfg;
3134 pjsua_buddy_id buddy_id;
3135 pj_status_t status;
3136
3137 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3138 break;
3139
3140 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3141 printf("Invalid SIP URI '%s'\n", buf);
3142 break;
3143 }
3144
Benny Prijonoac623b32006-07-03 15:19:31 +00003145 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003146
3147 buddy_cfg.uri = pj_str(buf);
3148 buddy_cfg.subscribe = PJ_TRUE;
3149
3150 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3151 if (status == PJ_SUCCESS) {
3152 printf("New buddy '%s' added at index %d\n",
3153 buf, buddy_id+1);
3154 }
3155
3156 } else if (menuin[1] == 'a') {
3157
Benny Prijonofb2b3652007-06-28 07:15:03 +00003158 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3159 pjsua_acc_config acc_cfg;
3160 pj_status_t status;
3161
3162 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3163 break;
3164 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3165 break;
3166 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3167 break;
3168 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3169 break;
3170 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3171 break;
3172
3173 pjsua_acc_config_default(&acc_cfg);
3174 acc_cfg.id = pj_str(id);
3175 acc_cfg.reg_uri = pj_str(registrar);
3176 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003177 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003178 acc_cfg.cred_info[0].realm = pj_str(realm);
3179 acc_cfg.cred_info[0].username = pj_str(uname);
3180 acc_cfg.cred_info[0].data_type = 0;
3181 acc_cfg.cred_info[0].data = pj_str(passwd);
3182
3183 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3184 if (status != PJ_SUCCESS) {
3185 pjsua_perror(THIS_FILE, "Error adding new account", status);
3186 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003187
3188 } else {
3189 printf("Invalid input %s\n", menuin);
3190 }
3191 break;
3192
3193 case '-':
3194 if (menuin[1] == 'b') {
3195 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3196 break;
3197
3198 i = my_atoi(buf) - 1;
3199
3200 if (!pjsua_buddy_is_valid(i)) {
3201 printf("Invalid buddy id %d\n", i);
3202 } else {
3203 pjsua_buddy_del(i);
3204 printf("Buddy %d deleted\n", i);
3205 }
3206
3207 } else if (menuin[1] == 'a') {
3208
3209 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3210 break;
3211
3212 i = my_atoi(buf);
3213
3214 if (!pjsua_acc_is_valid(i)) {
3215 printf("Invalid account id %d\n", i);
3216 } else {
3217 pjsua_acc_del(i);
3218 printf("Account %d deleted\n", i);
3219 }
3220
3221 } else {
3222 printf("Invalid input %s\n", menuin);
3223 }
3224 break;
3225
3226 case 'H':
3227 /*
3228 * Hold call.
3229 */
3230 if (current_call != -1) {
3231
3232 pjsua_call_set_hold(current_call, NULL);
3233
3234 } else {
3235 PJ_LOG(3,(THIS_FILE, "No current call"));
3236 }
3237 break;
3238
3239 case 'v':
3240 /*
3241 * Send re-INVITE (to release hold, etc).
3242 */
3243 if (current_call != -1) {
3244
3245 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3246
3247 } else {
3248 PJ_LOG(3,(THIS_FILE, "No current call"));
3249 }
3250 break;
3251
Benny Prijonoc08682e2007-10-04 06:17:58 +00003252 case 'U':
3253 /*
3254 * Send UPDATE
3255 */
3256 if (current_call != -1) {
3257
3258 pjsua_call_update(current_call, 0, NULL);
3259
3260 } else {
3261 PJ_LOG(3,(THIS_FILE, "No current call"));
3262 }
3263 break;
3264
3265 case 'C':
3266 if (menuin[1] == 'p') {
3267 manage_codec_prio();
3268 }
3269 break;
3270
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003271 case 'x':
3272 /*
3273 * Transfer call.
3274 */
3275 if (current_call == -1) {
3276
3277 PJ_LOG(3,(THIS_FILE, "No current call"));
3278
3279 } else {
3280 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003281 pjsua_msg_data msg_data;
3282 pjsip_generic_string_hdr refer_sub;
3283 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3284 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003285 pjsua_call_info ci;
3286
3287 pjsua_call_get_info(current_call, &ci);
3288 printf("Transfering current call [%d] %.*s\n",
3289 current_call,
3290 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003291
3292 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3293
3294 /* Check if call is still there. */
3295
3296 if (call != current_call) {
3297 puts("Call has been disconnected");
3298 continue;
3299 }
3300
Benny Prijonod524e822006-09-22 12:48:18 +00003301 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003302 if (app_config.no_refersub) {
3303 /* Add Refer-Sub: false in outgoing REFER request */
3304 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3305 &STR_FALSE);
3306 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3307 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003308 if (result.nb_result != NO_NB) {
3309 if (result.nb_result == -1 || result.nb_result == 0)
3310 puts("You can't do that with transfer call!");
3311 else {
3312 pjsua_buddy_info binfo;
3313 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003314 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003315 }
3316
3317 } else if (result.uri_result) {
3318 pj_str_t tmp;
3319 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003320 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003321 }
3322 }
3323 break;
3324
Benny Prijonof7b1c392006-11-11 16:46:34 +00003325 case 'X':
3326 /*
3327 * Transfer call with replaces.
3328 */
3329 if (current_call == -1) {
3330
3331 PJ_LOG(3,(THIS_FILE, "No current call"));
3332
3333 } else {
3334 int call = current_call;
3335 int dst_call;
3336 pjsua_msg_data msg_data;
3337 pjsip_generic_string_hdr refer_sub;
3338 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3339 pj_str_t STR_FALSE = { "false", 5 };
3340 pjsua_call_id ids[PJSUA_MAX_CALLS];
3341 pjsua_call_info ci;
3342 unsigned i, count;
3343
3344 count = PJ_ARRAY_SIZE(ids);
3345 pjsua_enum_calls(ids, &count);
3346
3347 if (count <= 1) {
3348 puts("There are no other calls");
3349 continue;
3350 }
3351
3352 pjsua_call_get_info(current_call, &ci);
3353 printf("Transfer call [%d] %.*s to one of the following:\n",
3354 current_call,
3355 (int)ci.remote_info.slen, ci.remote_info.ptr);
3356
3357 for (i=0; i<count; ++i) {
3358 pjsua_call_info call_info;
3359
3360 if (ids[i] == call)
3361 continue;
3362
3363 pjsua_call_get_info(ids[i], &call_info);
3364 printf("%d %.*s [%.*s]\n",
3365 ids[i],
3366 (int)call_info.remote_info.slen,
3367 call_info.remote_info.ptr,
3368 (int)call_info.state_text.slen,
3369 call_info.state_text.ptr);
3370 }
3371
3372 if (!simple_input("Enter call number to be replaced",
3373 buf, sizeof(buf)))
3374 continue;
3375
3376 dst_call = my_atoi(buf);
3377
3378 /* Check if call is still there. */
3379
3380 if (call != current_call) {
3381 puts("Call has been disconnected");
3382 continue;
3383 }
3384
3385 /* Check that destination call is valid. */
3386 if (dst_call == call) {
3387 puts("Destination call number must not be the same "
3388 "as the call being transfered");
3389 continue;
3390 }
3391 if (dst_call >= PJSUA_MAX_CALLS) {
3392 puts("Invalid destination call number");
3393 continue;
3394 }
3395 if (!pjsua_call_is_active(dst_call)) {
3396 puts("Invalid destination call number");
3397 continue;
3398 }
3399
3400 pjsua_msg_data_init(&msg_data);
3401 if (app_config.no_refersub) {
3402 /* Add Refer-Sub: false in outgoing REFER request */
3403 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3404 &STR_FALSE);
3405 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3406 }
3407
3408 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3409 }
3410 break;
3411
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003412 case '#':
3413 /*
3414 * Send DTMF strings.
3415 */
3416 if (current_call == -1) {
3417
3418 PJ_LOG(3,(THIS_FILE, "No current call"));
3419
3420 } else if (!pjsua_call_has_media(current_call)) {
3421
3422 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3423
3424 } else {
3425 pj_str_t digits;
3426 int call = current_call;
3427 pj_status_t status;
3428
3429 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3430 sizeof(buf)))
3431 {
3432 break;
3433 }
3434
3435 if (call != current_call) {
3436 puts("Call has been disconnected");
3437 continue;
3438 }
3439
3440 digits = pj_str(buf);
3441 status = pjsua_call_dial_dtmf(current_call, &digits);
3442 if (status != PJ_SUCCESS) {
3443 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3444 } else {
3445 puts("DTMF digits enqueued for transmission");
3446 }
3447 }
3448 break;
3449
Benny Prijonofeb69f42007-10-05 09:12:26 +00003450 case '*':
3451 /* Send DTMF with INFO */
3452 if (current_call == -1) {
3453
3454 PJ_LOG(3,(THIS_FILE, "No current call"));
3455
3456 } else {
3457 const pj_str_t SIP_INFO = pj_str("INFO");
3458 pj_str_t digits;
3459 int call = current_call;
3460 int i;
3461 pj_status_t status;
3462
3463 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3464 sizeof(buf)))
3465 {
3466 break;
3467 }
3468
3469 if (call != current_call) {
3470 puts("Call has been disconnected");
3471 continue;
3472 }
3473
3474 digits = pj_str(buf);
3475 for (i=0; i<digits.slen; ++i) {
3476 pjsua_msg_data msg_data;
3477 char body[80];
3478
3479 pjsua_msg_data_init(&msg_data);
3480 msg_data.content_type = pj_str("application/dtmf-relay");
3481
3482 pj_ansi_snprintf(body, sizeof(body),
3483 "Signal=%c\r\n"
3484 "Duration=160",
3485 buf[i]);
3486 msg_data.msg_body = pj_str(body);
3487
3488 status = pjsua_call_send_request(current_call, &SIP_INFO,
3489 &msg_data);
3490 if (status != PJ_SUCCESS) {
3491 break;
3492 }
3493 }
3494 }
3495 break;
3496
Benny Prijono56315612006-07-18 14:39:40 +00003497 case 'S':
3498 /*
3499 * Send arbitrary request
3500 */
3501 if (pjsua_acc_get_count() == 0) {
3502 puts("Sorry, need at least one account configured");
3503 break;
3504 }
3505
3506 puts("Send arbitrary request to remote host");
3507
3508 /* Input METHOD */
3509 if (!simple_input("Request method:",text,sizeof(text)))
3510 break;
3511
3512 /* Input destination URI */
3513 uri = NULL;
3514 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3515 if (result.nb_result != NO_NB) {
3516
3517 if (result.nb_result == -1 || result.nb_result == 0) {
3518 puts("Sorry you can't do that!");
3519 continue;
3520 } else {
3521 pjsua_buddy_info binfo;
3522 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3523 uri = binfo.uri.ptr;
3524 }
3525
3526 } else if (result.uri_result) {
3527 uri = result.uri_result;
3528 }
3529
3530 tmp = pj_str(uri);
3531
3532 send_request(text, &tmp);
3533 break;
3534
Benny Prijonoebc32c32008-06-12 13:30:39 +00003535 case 'e':
3536 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3537 pj_str_t tmp;
3538
3539 tmp.ptr = menuin+5;
3540 tmp.slen = pj_ansi_strlen(menuin)-6;
3541
3542 if (tmp.slen < 1) {
3543 puts("Usage: echo [0|1]");
3544 break;
3545 }
3546
Benny Prijonoddd02de2008-06-26 22:20:11 +00003547 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003548 }
3549 break;
3550
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003551 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003552 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3553 pj_str_t tmp;
3554 int delay;
3555
3556 tmp.ptr = menuin+6;
3557 tmp.slen = pj_ansi_strlen(menuin)-7;
3558
3559 if (tmp.slen < 1) {
3560 puts("Usage: sleep MSEC");
3561 break;
3562 }
3563
3564 delay = pj_strtoul(&tmp);
3565 if (delay < 0) delay = 0;
3566 pj_thread_sleep(delay);
3567 break;
3568 }
3569 /* Continue below */
3570
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003571 case 'u':
3572 /*
3573 * Subscribe/unsubscribe presence.
3574 */
3575 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3576 if (result.nb_result != NO_NB) {
3577 if (result.nb_result == -1) {
3578 int i, count;
3579 count = pjsua_get_buddy_count();
3580 for (i=0; i<count; ++i)
3581 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3582 } else if (result.nb_result == 0) {
3583 puts("Sorry, can only subscribe to buddy's presence, "
3584 "not from existing call");
3585 } else {
3586 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3587 }
3588
3589 } else if (result.uri_result) {
3590 puts("Sorry, can only subscribe to buddy's presence, "
3591 "not arbitrary URL (for now)");
3592 }
3593
3594 break;
3595
3596 case 'r':
3597 switch (menuin[1]) {
3598 case 'r':
3599 /*
3600 * Re-Register.
3601 */
3602 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3603 break;
3604 case 'u':
3605 /*
3606 * Unregister
3607 */
3608 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3609 break;
3610 }
3611 break;
3612
3613 case 't':
3614 pjsua_acc_get_info(current_acc, &acc_info);
3615 acc_info.online_status = !acc_info.online_status;
3616 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3617 printf("Setting %s online status to %s\n",
3618 acc_info.acc_uri.ptr,
3619 (acc_info.online_status?"online":"offline"));
3620 break;
3621
Benny Prijono4461c7d2007-08-25 13:36:15 +00003622 case 'T':
3623 change_online_status();
3624 break;
3625
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003626 case 'c':
3627 switch (menuin[1]) {
3628 case 'l':
3629 conf_list();
3630 break;
3631 case 'c':
3632 case 'd':
3633 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003634 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003635 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003636 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003637 const char *src_title, *dst_title;
3638
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003639 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003640
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003641 if (cnt != 3) {
3642 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003643
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003644 src_title = (menuin[1]=='c'?
3645 "Connect src port #":
3646 "Disconnect src port #");
3647 dst_title = (menuin[1]=='c'?
3648 "To dst port #":
3649 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003650
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003651 if (!simple_input(src_title, src_port, sizeof(src_port)))
3652 break;
3653
3654 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3655 break;
3656 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003657
3658 if (menuin[1]=='c') {
3659 status = pjsua_conf_connect(my_atoi(src_port),
3660 my_atoi(dst_port));
3661 } else {
3662 status = pjsua_conf_disconnect(my_atoi(src_port),
3663 my_atoi(dst_port));
3664 }
3665 if (status == PJ_SUCCESS) {
3666 puts("Success");
3667 } else {
3668 puts("ERROR!!");
3669 }
3670 }
3671 break;
3672 }
3673 break;
3674
Benny Prijono6dd967c2006-12-26 02:27:14 +00003675 case 'V':
3676 /* Adjust audio volume */
3677 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3678 if (simple_input(buf,text,sizeof(text))) {
3679 char *err;
3680 app_config.mic_level = (float)strtod(text, &err);
3681 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3682 }
3683 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3684 app_config.speaker_level);
3685 if (simple_input(buf,text,sizeof(text))) {
3686 char *err;
3687 app_config.speaker_level = (float)strtod(text, &err);
3688 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3689 }
3690
3691 break;
3692
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003693 case 'd':
3694 if (menuin[1] == 'c') {
3695 char settings[2000];
3696 int len;
3697
3698 len = write_settings(&app_config, settings, sizeof(settings));
3699 if (len < 1)
3700 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3701 else
3702 PJ_LOG(3,(THIS_FILE,
3703 "Dumping configuration (%d bytes):\n%s\n",
3704 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003705
3706 } else if (menuin[1] == 'q') {
3707
3708 if (current_call != PJSUA_INVALID_ID) {
Benny Prijono6eddd872008-03-21 13:46:08 +00003709 pjsua_call_dump(current_call, PJ_TRUE, some_buf,
3710 sizeof(some_buf), " ");
3711 PJ_LOG(3,(THIS_FILE, "\n%s", some_buf));
Benny Prijono819506c2006-06-22 22:29:51 +00003712 } else {
3713 PJ_LOG(3,(THIS_FILE, "No current call"));
3714 }
3715
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003716 } else {
3717 app_dump(menuin[1]=='d');
3718 }
3719 break;
3720
3721
3722 case 'f':
3723 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3724 char settings[2000];
3725 int len;
3726
3727 len = write_settings(&app_config, settings, sizeof(settings));
3728 if (len < 1)
3729 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3730 else {
3731 pj_oshandle_t fd;
3732 pj_status_t status;
3733
3734 status = pj_file_open(app_config.pool, buf,
3735 PJ_O_WRONLY, &fd);
3736 if (status != PJ_SUCCESS) {
3737 pjsua_perror(THIS_FILE, "Unable to open file", status);
3738 } else {
3739 pj_ssize_t size = len;
3740 pj_file_write(fd, settings, &size);
3741 pj_file_close(fd);
3742
3743 printf("Settings successfully written to '%s'\n", buf);
3744 }
3745 }
3746
3747 }
3748 break;
3749
3750
3751 case 'q':
3752 goto on_exit;
3753
3754
3755 default:
3756 if (menuin[0] != '\n' && menuin[0] != '\r') {
3757 printf("Invalid input %s", menuin);
3758 }
3759 keystroke_help();
3760 break;
3761 }
3762 }
3763
3764on_exit:
3765 ;
3766}
3767
3768
3769/*****************************************************************************
3770 * Public API
3771 */
3772
3773pj_status_t app_init(int argc, char *argv[])
3774{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003775 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003776 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003777 unsigned i;
3778 pj_status_t status;
3779
3780 /* Create pjsua */
3781 status = pjsua_create();
3782 if (status != PJ_SUCCESS)
3783 return status;
3784
3785 /* Create pool for application */
Benny Prijonod5696da2007-07-17 16:25:45 +00003786 app_config.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003787
3788 /* Initialize default config */
3789 default_config(&app_config);
3790
3791 /* Parse the arguments */
3792 status = parse_args(argc, argv, &app_config, &uri_arg);
3793 if (status != PJ_SUCCESS)
3794 return status;
3795
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003796 /* Initialize application callbacks */
3797 app_config.cfg.cb.on_call_state = &on_call_state;
3798 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3799 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003800 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003801 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003802 app_config.cfg.cb.on_reg_state = &on_reg_state;
3803 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3804 app_config.cfg.cb.on_pager = &on_pager;
3805 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003806 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003807 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003808 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003809
3810 /* Initialize pjsua */
3811 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3812 &app_config.media_cfg);
3813 if (status != PJ_SUCCESS)
3814 return status;
3815
Benny Prijonoe909eac2006-07-27 22:04:56 +00003816#ifdef STEREO_DEMO
3817 stereo_demo();
3818#endif
3819
Benny Prijono804ff0a2006-09-14 11:17:48 +00003820 /* Initialize calls data */
3821 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3822 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3823 app_config.call_data[i].timer.cb = &call_timeout_callback;
3824 }
3825
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003826 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003827 for (i=0; i<app_config.wav_count; ++i) {
3828 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003829 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00003830
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003831 if (app_config.auto_play_hangup)
3832 play_options |= PJMEDIA_FILE_NO_LOOP;
3833
3834 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00003835 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003836 if (status != PJ_SUCCESS)
3837 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003838
Benny Prijono72c04d22007-02-17 22:20:58 +00003839 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003840 app_config.wav_id = wav_id;
3841 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003842 if (app_config.auto_play_hangup) {
3843 pjmedia_port *port;
3844
3845 pjsua_player_get_port(app_config.wav_id, &port);
3846 status = pjmedia_wav_player_set_eof_cb(port, NULL,
3847 &on_playfile_done);
3848 if (status != PJ_SUCCESS)
3849 goto on_error;
3850
3851 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
3852 &hangup_timeout_callback);
3853 }
Benny Prijono32e4f492007-01-24 00:44:26 +00003854 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003855 }
3856
Benny Prijono4af234b2007-01-24 02:02:09 +00003857 /* Optionally registers tone players */
3858 for (i=0; i<app_config.tone_count; ++i) {
3859 pjmedia_port *tport;
3860 char name[80];
3861 pj_str_t label;
3862 pj_status_t status;
3863
3864 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3865 app_config.tones[i].freq1,
3866 app_config.tones[i].freq2);
3867 label = pj_str(name);
3868 status = pjmedia_tonegen_create2(app_config.pool, &label,
3869 8000, 1, 160, 16,
3870 PJMEDIA_TONEGEN_LOOP, &tport);
3871 if (status != PJ_SUCCESS) {
3872 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3873 goto on_error;
3874 }
3875
3876 status = pjsua_conf_add_port(app_config.pool, tport,
3877 &app_config.tone_slots[i]);
3878 pj_assert(status == PJ_SUCCESS);
3879
3880 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3881 pj_assert(status == PJ_SUCCESS);
3882 }
3883
Benny Prijono1ebd6142006-10-19 15:48:02 +00003884 /* Optionally create recorder file, if any. */
3885 if (app_config.rec_file.slen) {
3886 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3887 &app_config.rec_id);
3888 if (status != PJ_SUCCESS)
3889 goto on_error;
3890
3891 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3892 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003893
Benny Prijonoe347cb02007-02-14 14:36:13 +00003894 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3895
Benny Prijono91d20f42008-06-14 19:42:37 +00003896 /* Create ringback tones */
3897 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00003898 unsigned i, samples_per_frame;
3899 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00003900 pj_str_t name;
3901
Benny Prijono4f966892008-06-14 22:43:56 +00003902 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
3903 app_config.media_cfg.clock_rate *
3904 app_config.media_cfg.channel_count / 1000;
3905
Benny Prijono91d20f42008-06-14 19:42:37 +00003906 /* Ringback tone (call is ringing) */
3907 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00003908 status = pjmedia_tonegen_create2(app_config.pool, &name,
3909 app_config.media_cfg.clock_rate,
3910 app_config.media_cfg.channel_count,
3911 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003912 16, PJMEDIA_TONEGEN_LOOP,
3913 &app_config.ringback_port);
3914 if (status != PJ_SUCCESS)
3915 goto on_error;
3916
Benny Prijonoe28cec82008-06-14 20:40:53 +00003917 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00003918 for (i=0; i<RINGBACK_CNT; ++i) {
3919 tone[i].freq1 = RINGBACK_FREQ1;
3920 tone[i].freq2 = RINGBACK_FREQ2;
3921 tone[i].on_msec = RINGBACK_ON;
3922 tone[i].off_msec = RINGBACK_OFF;
3923 }
3924 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003925
Benny Prijono4f966892008-06-14 22:43:56 +00003926 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00003927 PJMEDIA_TONEGEN_LOOP);
3928
3929
Benny Prijono91d20f42008-06-14 19:42:37 +00003930 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
3931 &app_config.ringback_slot);
3932 if (status != PJ_SUCCESS)
3933 goto on_error;
3934
3935 /* Ring (to alert incoming call) */
3936 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00003937 status = pjmedia_tonegen_create2(app_config.pool, &name,
3938 app_config.media_cfg.clock_rate,
3939 app_config.media_cfg.channel_count,
3940 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003941 16, PJMEDIA_TONEGEN_LOOP,
3942 &app_config.ring_port);
3943 if (status != PJ_SUCCESS)
3944 goto on_error;
3945
Benny Prijono4f966892008-06-14 22:43:56 +00003946 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00003947 tone[i].freq1 = RING_FREQ1;
3948 tone[i].freq2 = RING_FREQ2;
3949 tone[i].on_msec = RING_ON;
3950 tone[i].off_msec = RING_OFF;
3951 }
Benny Prijono4f966892008-06-14 22:43:56 +00003952 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003953
Benny Prijono4f966892008-06-14 22:43:56 +00003954 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00003955 tone, PJMEDIA_TONEGEN_LOOP);
3956
Benny Prijono91d20f42008-06-14 19:42:37 +00003957 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
3958 &app_config.ring_slot);
3959 if (status != PJ_SUCCESS)
3960 goto on_error;
3961
3962 }
3963
Benny Prijono87ef89a2007-01-14 00:39:45 +00003964 /* Add UDP transport unless it's disabled. */
3965 if (!app_config.no_udp) {
3966 pjsua_acc_id aid;
3967
3968 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
3969 &app_config.udp_cfg,
3970 &transport_id);
3971 if (status != PJ_SUCCESS)
3972 goto on_error;
3973
3974 /* Add local account */
3975 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
3976 //pjsua_acc_set_transport(aid, transport_id);
3977 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00003978
3979 if (app_config.udp_cfg.port == 0) {
3980 pjsua_transport_info ti;
3981 pj_sockaddr_in *a;
3982
3983 pjsua_transport_get_info(transport_id, &ti);
3984 a = (pj_sockaddr_in*)&ti.local_addr;
3985
3986 tcp_cfg.port = pj_ntohs(a->sin_port);
3987 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00003988 }
3989
Benny Prijonoe93e2872006-06-28 16:46:49 +00003990 /* Add TCP transport unless it's disabled */
3991 if (!app_config.no_tcp) {
3992 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00003993 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00003994 &transport_id);
3995 if (status != PJ_SUCCESS)
3996 goto on_error;
3997
3998 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00003999 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004000 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4001
4002 }
4003
Benny Prijonoe93e2872006-06-28 16:46:49 +00004004
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004005#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4006 /* Add TLS transport when application wants one */
4007 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004008
4009 pjsua_acc_id acc_id;
4010
4011 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004012 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004013 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004014 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004015 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004016 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004017 if (status != PJ_SUCCESS)
4018 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004019
4020 /* Add local account */
4021 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4022 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004023 }
4024#endif
4025
Benny Prijonoe93e2872006-06-28 16:46:49 +00004026 if (transport_id == -1) {
4027 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
4028 status = -1;
4029 goto on_error;
4030 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004031
4032
4033 /* Add accounts */
4034 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004035 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004036 if (status != PJ_SUCCESS)
4037 goto on_error;
4038 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4039 }
4040
4041 /* Add buddies */
4042 for (i=0; i<app_config.buddy_cnt; ++i) {
4043 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4044 if (status != PJ_SUCCESS)
4045 goto on_error;
4046 }
4047
4048 /* Optionally set codec orders */
4049 for (i=0; i<app_config.codec_cnt; ++i) {
4050 pjsua_codec_set_priority(&app_config.codec_arg[i],
4051 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4052 }
4053
Benny Prijonofce28542007-12-09 15:41:10 +00004054 /* Optionally disable some codec */
4055 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4056 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4057 }
4058
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004059 /* Add RTP transports */
4060 status = pjsua_media_transports_create(&app_config.rtp_cfg);
4061 if (status != PJ_SUCCESS)
4062 goto on_error;
4063
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004064 /* Set sound device latency */
4065 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4066
Benny Prijono22a300a2006-06-14 20:04:55 +00004067 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004068#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004069 if (app_config.null_audio) {
4070 status = pjsua_set_null_snd_dev();
4071 if (status != PJ_SUCCESS)
4072 return status;
4073 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004074#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004075
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004076 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4077 app_config.playback_dev != PJSUA_INVALID_ID)
4078 {
4079 status = pjsua_set_snd_dev(app_config.capture_dev,
4080 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004081 if (status != PJ_SUCCESS)
4082 goto on_error;
4083 }
4084
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004085 return PJ_SUCCESS;
4086
4087on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004088 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004089 return status;
4090}
4091
4092
Benny Prijonoebc32c32008-06-12 13:30:39 +00004093static int stdout_refresh_proc(void *arg)
4094{
4095 PJ_UNUSED_ARG(arg);
4096
4097 /* Set thread to lowest priority so that it doesn't clobber
4098 * stdout output
4099 */
4100 pj_thread_set_prio(pj_thread_this(),
4101 pj_thread_get_prio_min(pj_thread_this()));
4102
4103 while (!stdout_refresh_quit) {
4104 pj_thread_sleep(stdout_refresh * 1000);
4105 puts(stdout_refresh_text);
4106 fflush(stdout);
4107 }
4108
4109 return 0;
4110}
4111
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004112pj_status_t app_main(void)
4113{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004114 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004115 pj_status_t status;
4116
4117 /* Start pjsua */
4118 status = pjsua_start();
4119 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004120 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004121 return status;
4122 }
4123
Benny Prijonoebc32c32008-06-12 13:30:39 +00004124 /* Start console refresh thread */
4125 if (stdout_refresh > 0) {
4126 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4127 NULL, 0, 0, &stdout_refresh_thread);
4128 }
4129
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004130 console_app_main(&uri_arg);
4131
Benny Prijonoebc32c32008-06-12 13:30:39 +00004132 if (stdout_refresh_thread) {
4133 stdout_refresh_quit = PJ_TRUE;
4134 pj_thread_join(stdout_refresh_thread);
4135 pj_thread_destroy(stdout_refresh_thread);
4136 }
4137
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004138 return PJ_SUCCESS;
4139}
4140
4141pj_status_t app_destroy(void)
4142{
Benny Prijonof762ee72006-12-01 11:14:37 +00004143 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004144 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004145
Benny Prijonoe909eac2006-07-27 22:04:56 +00004146#ifdef STEREO_DEMO
4147 if (app_config.snd) {
4148 pjmedia_snd_port_destroy(app_config.snd);
4149 app_config.snd = NULL;
4150 }
4151#endif
4152
Benny Prijono91d20f42008-06-14 19:42:37 +00004153 /* Close ringback port */
4154 if (app_config.ringback_port &&
4155 app_config.ringback_slot != PJSUA_INVALID_ID)
4156 {
4157 pjsua_conf_remove_port(app_config.ringback_slot);
4158 app_config.ringback_slot = PJSUA_INVALID_ID;
4159 pjmedia_port_destroy(app_config.ringback_port);
4160 app_config.ringback_port = NULL;
4161 }
4162
4163 /* Close ring port */
4164 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4165 pjsua_conf_remove_port(app_config.ring_slot);
4166 app_config.ring_slot = PJSUA_INVALID_ID;
4167 pjmedia_port_destroy(app_config.ring_port);
4168 app_config.ring_port = NULL;
4169 }
4170
Benny Prijono4af234b2007-01-24 02:02:09 +00004171 /* Close tone generators */
4172 for (i=0; i<app_config.tone_count; ++i) {
4173 pjsua_conf_remove_port(app_config.tone_slots[i]);
4174 }
4175
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004176 if (app_config.pool) {
4177 pj_pool_release(app_config.pool);
4178 app_config.pool = NULL;
4179 }
4180
Benny Prijonof762ee72006-12-01 11:14:37 +00004181 status = pjsua_destroy();
4182
4183 pj_bzero(&app_config, sizeof(app_config));
4184
4185 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004186}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004187
4188
4189#ifdef STEREO_DEMO
4190static void stereo_demo()
4191{
4192 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004193 pj_status_t status;
4194
4195 /* Disable existing sound device */
4196 conf = pjsua_set_no_snd_dev();
4197
Benny Prijonoe909eac2006-07-27 22:04:56 +00004198 /* Create stereo-mono splitter/combiner */
4199 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004200 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004201 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004202 2 * conf->info.samples_per_frame,
4203 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004204 0 /* options */,
4205 &splitter);
4206 pj_assert(status == PJ_SUCCESS);
4207
4208 /* Connect channel0 (left channel?) to conference port slot0 */
4209 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4210 0 /*options*/,
4211 conf);
4212 pj_assert(status == PJ_SUCCESS);
4213
4214 /* Create reverse channel for channel1 (right channel?)... */
4215 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4216 splitter,
4217 1 /* ch1 */,
4218 0 /* options */,
4219 &ch1);
4220 pj_assert(status == PJ_SUCCESS);
4221
4222 /* .. and register it to conference bridge (it would be slot1
4223 * if there's no other devices connected to the bridge)
4224 */
4225 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4226 pj_assert(status == PJ_SUCCESS);
4227
4228 /* Create sound device */
4229 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004230 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004231 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004232 2 * conf->info.samples_per_frame,
4233 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004234 0, &app_config.snd);
4235 pj_assert(status == PJ_SUCCESS);
4236
4237
4238 /* Connect the splitter to the sound device */
4239 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4240 pj_assert(status == PJ_SUCCESS);
4241
4242}
4243#endif
4244