blob: 97896b2f62bfda2844773f732efb6aa126b6b61e [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 Prijonofce28542007-12-09 15:41:10 +0000467 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000468 };
469 struct pj_getopt_option long_options[] = {
470 { "config-file",1, 0, OPT_CONFIG_FILE},
471 { "log-file", 1, 0, OPT_LOG_FILE},
472 { "log-level", 1, 0, OPT_LOG_LEVEL},
473 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
474 { "help", 0, 0, OPT_HELP},
475 { "version", 0, 0, OPT_VERSION},
476 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000477 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000478 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000479 { "null-audio", 0, 0, OPT_NULL_AUDIO},
480 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000481 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000482 { "no-tcp", 0, 0, OPT_NO_TCP},
483 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000484 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000485 { "proxy", 1, 0, OPT_PROXY},
486 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
487 { "registrar", 1, 0, OPT_REGISTRAR},
488 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000489 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000490 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000491 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492 { "id", 1, 0, OPT_ID},
493 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000494 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
495 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496 { "realm", 1, 0, OPT_REALM},
497 { "username", 1, 0, OPT_USERNAME},
498 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000499 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000500 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000501 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
503 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
504 { "no-presence", 0, 0, OPT_NO_PRESENCE},
505 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000506 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000507 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000508 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
510 { "auto-conf", 0, 0, OPT_AUTO_CONF},
511 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000512 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000513 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000515
Benny Prijonoc97608e2007-03-23 16:34:20 +0000516 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000517 { "use-turn", 0, 0, OPT_USE_TURN},
518 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
519 { "turn-srv", 1, 0, OPT_TURN_SRV},
520 { "turn-tcp", 0, 0, OPT_TURN_TCP},
521 { "turn-user", 1, 0, OPT_TURN_USER},
522 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
523
Benny Prijonod8179652008-01-23 20:39:07 +0000524#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
525 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000526 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000527#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000529 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 { "complexity", 1, 0, OPT_COMPLEXITY},
531 { "quality", 1, 0, OPT_QUALITY},
532 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000533 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000534 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono00cae612006-07-31 15:19:36 +0000535 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
536 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
537 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000539 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000540 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000541 { "duration", 1, 0, OPT_DURATION},
542 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000543 { "use-tls", 0, 0, OPT_USE_TLS},
544 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000545 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
546 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000547 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000548 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
549 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
550 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000551 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
552 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000553 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
554 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000555 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
556 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000557 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000558 { "no-tones", 0, 0, OPT_NO_TONES},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 { NULL, 0, 0, 0}
560 };
561 pj_status_t status;
562 pjsua_acc_config *cur_acc;
563 char *config_file = NULL;
564 unsigned i;
565
566 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000567 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000568 while ((c=pj_getopt_long(argc, argv, "", long_options,
569 &option_index)) != -1)
570 {
571 switch (c) {
572 case OPT_CONFIG_FILE:
573 config_file = pj_optarg;
574 break;
575 }
576 if (config_file)
577 break;
578 }
579
580 if (config_file) {
581 status = read_config_file(app_config.pool, config_file, &argc, &argv);
582 if (status != 0)
583 return status;
584 }
585
586 cfg->acc_cnt = 0;
587 cur_acc = &cfg->acc_cfg[0];
588
589
590 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
591 * read from config file.
592 */
593 pj_optind = 0;
594 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000595 pj_str_t tmp;
596 long lval;
597
598 switch (c) {
599
Benny Prijono6f137482006-06-15 11:31:36 +0000600 case OPT_CONFIG_FILE:
601 /* Ignore as this has been processed before */
602 break;
603
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000604 case OPT_LOG_FILE:
605 cfg->log_cfg.log_filename = pj_str(pj_optarg);
606 break;
607
608 case OPT_LOG_LEVEL:
609 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
610 if (c < 0 || c > 6) {
611 PJ_LOG(1,(THIS_FILE,
612 "Error: expecting integer value 0-6 "
613 "for --log-level"));
614 return PJ_EINVAL;
615 }
616 cfg->log_cfg.level = c;
617 pj_log_set_level( c );
618 break;
619
620 case OPT_APP_LOG_LEVEL:
621 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
622 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
623 PJ_LOG(1,(THIS_FILE,
624 "Error: expecting integer value 0-6 "
625 "for --app-log-level"));
626 return PJ_EINVAL;
627 }
628 break;
629
630 case OPT_HELP:
631 usage();
632 return PJ_EINVAL;
633
634 case OPT_VERSION: /* version */
635 pj_dump_config();
636 return PJ_EINVAL;
637
638 case OPT_NULL_AUDIO:
639 cfg->null_audio = PJ_TRUE;
640 break;
641
642 case OPT_CLOCK_RATE:
643 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000644 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000645 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000646 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000647 return PJ_EINVAL;
648 }
649 cfg->media_cfg.clock_rate = lval;
650 break;
651
Benny Prijonof3758ee2008-02-26 15:32:16 +0000652 case OPT_SND_CLOCK_RATE:
653 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000654 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000655 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000656 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000657 return PJ_EINVAL;
658 }
659 cfg->media_cfg.snd_clock_rate = lval;
660 break;
661
Benny Prijono7d60d052008-03-29 12:24:20 +0000662 case OPT_STEREO:
663 cfg->media_cfg.channel_count = 2;
664 break;
665
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000666 case OPT_LOCAL_PORT: /* local-port */
667 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000668 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000669 PJ_LOG(1,(THIS_FILE,
670 "Error: expecting integer value for "
671 "--local-port"));
672 return PJ_EINVAL;
673 }
674 cfg->udp_cfg.port = (pj_uint16_t)lval;
675 break;
676
Benny Prijono0a5cad82006-09-26 13:21:02 +0000677 case OPT_IP_ADDR: /* ip-addr */
678 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
679 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
680 break;
681
Benny Prijonoe93e2872006-06-28 16:46:49 +0000682 case OPT_NO_UDP: /* no-udp */
683 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000684 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
685 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000686 }
687
688 cfg->no_udp = PJ_TRUE;
689 break;
690
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000691 case OPT_NOREFERSUB: /* norefersub */
692 cfg->no_refersub = PJ_TRUE;
693 break;
694
Benny Prijonoe93e2872006-06-28 16:46:49 +0000695 case OPT_NO_TCP: /* no-tcp */
696 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000697 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
698 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000699 }
700
701 cfg->no_tcp = PJ_TRUE;
702 break;
703
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000704 case OPT_PROXY: /* proxy */
705 if (pjsua_verify_sip_url(pj_optarg) != 0) {
706 PJ_LOG(1,(THIS_FILE,
707 "Error: invalid SIP URL '%s' "
708 "in proxy argument", pj_optarg));
709 return PJ_EINVAL;
710 }
711 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
712 break;
713
714 case OPT_OUTBOUND_PROXY: /* outbound proxy */
715 if (pjsua_verify_sip_url(pj_optarg) != 0) {
716 PJ_LOG(1,(THIS_FILE,
717 "Error: invalid SIP URL '%s' "
718 "in outbound proxy argument", pj_optarg));
719 return PJ_EINVAL;
720 }
721 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
722 break;
723
724 case OPT_REGISTRAR: /* registrar */
725 if (pjsua_verify_sip_url(pj_optarg) != 0) {
726 PJ_LOG(1,(THIS_FILE,
727 "Error: invalid SIP URL '%s' in "
728 "registrar argument", pj_optarg));
729 return PJ_EINVAL;
730 }
731 cur_acc->reg_uri = pj_str(pj_optarg);
732 break;
733
734 case OPT_REG_TIMEOUT: /* reg-timeout */
735 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
736 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
737 PJ_LOG(1,(THIS_FILE,
738 "Error: invalid value for --reg-timeout "
739 "(expecting 1-3600)"));
740 return PJ_EINVAL;
741 }
742 break;
743
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000744 case OPT_PUBLISH: /* publish */
745 cur_acc->publish_enabled = PJ_TRUE;
746 break;
747
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000748 case OPT_100REL: /** 100rel */
749 cur_acc->require_100rel = PJ_TRUE;
750 cfg->cfg.require_100rel = PJ_TRUE;
751 break;
752
Benny Prijono48ab2b72007-11-08 09:24:30 +0000753 case OPT_USE_IMS: /* Activate IMS settings */
754 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000755 break;
756
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000757 case OPT_ID: /* id */
758 if (pjsua_verify_sip_url(pj_optarg) != 0) {
759 PJ_LOG(1,(THIS_FILE,
760 "Error: invalid SIP URL '%s' "
761 "in local id argument", pj_optarg));
762 return PJ_EINVAL;
763 }
764 cur_acc->id = pj_str(pj_optarg);
765 break;
766
767 case OPT_CONTACT: /* contact */
768 if (pjsua_verify_sip_url(pj_optarg) != 0) {
769 PJ_LOG(1,(THIS_FILE,
770 "Error: invalid SIP URL '%s' "
771 "in contact argument", pj_optarg));
772 return PJ_EINVAL;
773 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000774 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000775 break;
776
Benny Prijonofce28542007-12-09 15:41:10 +0000777 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000778 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000779 break;
780
781 case OPT_USE_COMPACT_FORM:
782 /* enable compact form - from Ticket #342 */
783 {
784 extern pj_bool_t pjsip_use_compact_form;
785 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
786 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
787
788 pjsip_use_compact_form = PJ_TRUE;
789 /* do not transmit Allow header */
790 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
791 /* Do not include rtpmap for static payload types (<96) */
792 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
793 }
794 break;
795
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000796 case OPT_NEXT_ACCOUNT: /* Add more account. */
797 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000798 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000799 break;
800
801 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000802 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000803 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000804 break;
805
806 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000807 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000808 break;
809
810 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000811 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
812 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000813#if PJSIP_HAS_DIGEST_AKA_AUTH
814 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
815 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
816 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
817#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000818 break;
819
820 case OPT_NEXT_CRED: /* next credential */
821 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000822 break;
823
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000824 case OPT_NAMESERVER: /* nameserver */
825 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
826 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
827 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
828 return PJ_ETOOMANY;
829 }
830 break;
831
Benny Prijonoebbf6892007-03-24 17:37:25 +0000832 case OPT_STUN_DOMAIN: /* STUN domain */
833 cfg->cfg.stun_domain = pj_str(pj_optarg);
834 break;
835
Benny Prijonoc97608e2007-03-23 16:34:20 +0000836 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000837 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000838 break;
839
840 case OPT_ADD_BUDDY: /* Add to buddy list. */
841 if (pjsua_verify_sip_url(pj_optarg) != 0) {
842 PJ_LOG(1,(THIS_FILE,
843 "Error: invalid URL '%s' in "
844 "--add-buddy option", pj_optarg));
845 return -1;
846 }
847 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
848 PJ_LOG(1,(THIS_FILE,
849 "Error: too many buddies in buddy list."));
850 return -1;
851 }
852 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
853 cfg->buddy_cnt++;
854 break;
855
856 case OPT_AUTO_PLAY:
857 cfg->auto_play = 1;
858 break;
859
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000860 case OPT_AUTO_PLAY_HANGUP:
861 cfg->auto_play_hangup = 1;
862 break;
863
Benny Prijono1ebd6142006-10-19 15:48:02 +0000864 case OPT_AUTO_REC:
865 cfg->auto_rec = 1;
866 break;
867
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000868 case OPT_AUTO_LOOP:
869 cfg->auto_loop = 1;
870 break;
871
Benny Prijono7ca96da2006-08-07 12:11:40 +0000872 case OPT_AUTO_CONF:
873 cfg->auto_conf = 1;
874 break;
875
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000876 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000877 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000878 break;
879
Benny Prijono4af234b2007-01-24 02:02:09 +0000880 case OPT_PLAY_TONE:
881 {
882 int f1, f2, on, off;
883 int n;
884
885 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
886 if (n != 4) {
887 puts("Expecting f1,f2,on,off in --play-tone");
888 return -1;
889 }
890
891 cfg->tones[cfg->tone_count].freq1 = (short)f1;
892 cfg->tones[cfg->tone_count].freq2 = (short)f2;
893 cfg->tones[cfg->tone_count].on_msec = (short)on;
894 cfg->tones[cfg->tone_count].off_msec = (short)off;
895 ++cfg->tone_count;
896 }
897 break;
898
Benny Prijono1ebd6142006-10-19 15:48:02 +0000899 case OPT_REC_FILE:
900 cfg->rec_file = pj_str(pj_optarg);
901 break;
902
Benny Prijonoc97608e2007-03-23 16:34:20 +0000903 case OPT_USE_ICE:
904 cfg->media_cfg.enable_ice = PJ_TRUE;
905 break;
906
Benny Prijonof76e1392008-06-06 14:51:48 +0000907 case OPT_USE_TURN:
908 cfg->media_cfg.enable_turn = PJ_TRUE;
909 break;
910
911 case OPT_ICE_NO_HOST:
912 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
913 break;
914
915 case OPT_TURN_SRV:
916 cfg->media_cfg.turn_server = pj_str(pj_optarg);
917 break;
918
919 case OPT_TURN_TCP:
920 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
921 break;
922
923 case OPT_TURN_USER:
924 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
925 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
926 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
927 break;
928
929 case OPT_TURN_PASSWD:
930 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
931 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
932 break;
933
Benny Prijonod8179652008-01-23 20:39:07 +0000934#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
935 case OPT_USE_SRTP:
936 app_config.cfg.use_srtp = my_atoi(pj_optarg);
937 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
938 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
939 return -1;
940 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000941 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +0000942 break;
Benny Prijonof6508982008-01-25 09:02:33 +0000943 case OPT_SRTP_SECURE:
944 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
945 if (!pj_isdigit(*pj_optarg) ||
946 app_config.cfg.srtp_secure_signaling > 2)
947 {
948 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
949 return -1;
950 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000951 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +0000952 break;
Benny Prijonod8179652008-01-23 20:39:07 +0000953#endif
954
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000955 case OPT_RTP_PORT:
956 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +0000957 if (cfg->rtp_cfg.port == 0) {
958 enum { START_PORT=4000 };
959 unsigned range;
960
961 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
962 cfg->rtp_cfg.port = START_PORT +
963 ((pj_rand() % range) & 0xFFFE);
964 }
965
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000966 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
967 PJ_LOG(1,(THIS_FILE,
968 "Error: rtp-port argument value "
969 "(expecting 1-65535"));
970 return -1;
971 }
972 break;
973
Benny Prijonofce28542007-12-09 15:41:10 +0000974 case OPT_DIS_CODEC:
975 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
976 break;
977
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000978 case OPT_ADD_CODEC:
979 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
980 break;
981
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000982 /* These options were no longer valid after new pjsua */
983 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000984 case OPT_COMPLEXITY:
985 cfg->complexity = my_atoi(pj_optarg);
986 if (cfg->complexity < 0 || cfg->complexity > 10) {
987 PJ_LOG(1,(THIS_FILE,
988 "Error: invalid --complexity (expecting 0-10"));
989 return -1;
990 }
991 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000992 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000993
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000994 case OPT_DURATION:
995 cfg->duration = my_atoi(pj_optarg);
996 break;
997
Benny Prijonof521eb02006-08-06 23:07:25 +0000998 case OPT_THREAD_CNT:
999 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1000 if (cfg->cfg.thread_cnt > 128) {
1001 PJ_LOG(1,(THIS_FILE,
1002 "Error: invalid --thread-cnt option"));
1003 return -1;
1004 }
1005 break;
1006
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001007 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001008 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1009 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001010 PJ_LOG(1,(THIS_FILE,
1011 "Error: invalid --ptime option"));
1012 return -1;
1013 }
1014 break;
1015
Benny Prijono0a12f002006-07-26 17:05:39 +00001016 case OPT_NO_VAD:
1017 cfg->media_cfg.no_vad = PJ_TRUE;
1018 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001019
Benny Prijonod79f25c2006-08-02 19:41:37 +00001020 case OPT_EC_TAIL:
1021 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1022 if (cfg->media_cfg.ec_tail_len > 1000) {
1023 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1024 "is too big"));
1025 return -1;
1026 }
1027 break;
1028
Benny Prijono0498d902006-06-19 14:49:14 +00001029 case OPT_QUALITY:
1030 cfg->media_cfg.quality = my_atoi(pj_optarg);
1031 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1032 PJ_LOG(1,(THIS_FILE,
1033 "Error: invalid --quality (expecting 0-10"));
1034 return -1;
1035 }
1036 break;
1037
Benny Prijono00cae612006-07-31 15:19:36 +00001038 case OPT_ILBC_MODE:
1039 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1040 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1041 PJ_LOG(1,(THIS_FILE,
1042 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1043 return -1;
1044 }
1045 break;
1046
1047 case OPT_RX_DROP_PCT:
1048 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1049 if (cfg->media_cfg.rx_drop_pct > 100) {
1050 PJ_LOG(1,(THIS_FILE,
1051 "Error: invalid --rx-drop-pct (expecting <= 100"));
1052 return -1;
1053 }
1054 break;
1055
1056 case OPT_TX_DROP_PCT:
1057 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1058 if (cfg->media_cfg.tx_drop_pct > 100) {
1059 PJ_LOG(1,(THIS_FILE,
1060 "Error: invalid --tx-drop-pct (expecting <= 100"));
1061 return -1;
1062 }
1063 break;
1064
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065 case OPT_AUTO_ANSWER:
1066 cfg->auto_answer = my_atoi(pj_optarg);
1067 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1068 PJ_LOG(1,(THIS_FILE,
1069 "Error: invalid code in --auto-answer "
1070 "(expecting 100-699"));
1071 return -1;
1072 }
1073 break;
1074
1075 case OPT_MAX_CALLS:
1076 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001077 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001078 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1079 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001080 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001081 return -1;
1082 }
1083 break;
1084
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001085 case OPT_USE_TLS:
1086 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001087#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1088 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1089 return -1;
1090#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001091 break;
1092
1093 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001094 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1095#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1096 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1097 return -1;
1098#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001099 break;
1100
Benny Prijonof3bbc132006-12-25 06:43:59 +00001101 case OPT_TLS_CERT_FILE:
1102 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1103#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1104 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1105 return -1;
1106#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001107 break;
1108
Benny Prijonof3bbc132006-12-25 06:43:59 +00001109 case OPT_TLS_PRIV_FILE:
1110 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1111 break;
1112
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001113 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001114 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1115#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1116 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1117 return -1;
1118#endif
1119 break;
1120
1121 case OPT_TLS_VERIFY_SERVER:
1122 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1123 break;
1124
1125 case OPT_TLS_VERIFY_CLIENT:
1126 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1127 break;
1128
1129 case OPT_TLS_NEG_TIMEOUT:
1130 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001131 break;
1132
Benny Prijono4e5d5512007-03-06 18:11:30 +00001133 case OPT_CAPTURE_DEV:
1134 cfg->capture_dev = atoi(pj_optarg);
1135 break;
1136
1137 case OPT_PLAYBACK_DEV:
1138 cfg->playback_dev = atoi(pj_optarg);
1139 break;
1140
Benny Prijonoebc32c32008-06-12 13:30:39 +00001141 case OPT_STDOUT_REFRESH:
1142 stdout_refresh = atoi(pj_optarg);
1143 break;
1144
1145 case OPT_STDOUT_REFRESH_TEXT:
1146 stdout_refresh_text = pj_optarg;
1147 break;
1148
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001149 case OPT_CAPTURE_LAT:
1150 cfg->capture_lat = atoi(pj_optarg);
1151 break;
1152
1153 case OPT_PLAYBACK_LAT:
1154 cfg->playback_lat = atoi(pj_optarg);
1155 break;
1156
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001157 case OPT_SND_AUTO_CLOSE:
1158 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1159 break;
1160
Benny Prijono91d20f42008-06-14 19:42:37 +00001161 case OPT_NO_TONES:
1162 cfg->no_tones = PJ_TRUE;
1163 break;
1164
Benny Prijono22a300a2006-06-14 20:04:55 +00001165 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001166 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001167 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001168 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001169 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001170 }
1171 }
1172
1173 if (pj_optind != argc) {
1174 pj_str_t uri_arg;
1175
1176 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1177 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1178 return -1;
1179 }
1180 uri_arg = pj_str(argv[pj_optind]);
1181 if (uri_to_call)
1182 *uri_to_call = uri_arg;
1183 pj_optind++;
1184
1185 /* Add URI to call to buddy list if it's not already there */
1186 for (i=0; i<cfg->buddy_cnt; ++i) {
1187 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1188 break;
1189 }
1190 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1191 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1192 }
1193
1194 } else {
1195 if (uri_to_call)
1196 uri_to_call->slen = 0;
1197 }
1198
1199 if (pj_optind != argc) {
1200 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1201 return PJ_EINVAL;
1202 }
1203
Benny Prijono56315612006-07-18 14:39:40 +00001204 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1205 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001206
1207 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001208 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1209
1210 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001211 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001212 acfg->cred_count++;
1213 }
1214
1215 /* When IMS mode is enabled for the account, verify that settings
1216 * are okay.
1217 */
1218 /* For now we check if IMS mode is activated by looking if
1219 * initial_auth is set.
1220 */
1221 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1222 /* Realm must point to the real domain */
1223 if (*acfg->cred_info[0].realm.ptr=='*') {
1224 PJ_LOG(1,(THIS_FILE,
1225 "Error: cannot use '*' as realm with IMS"));
1226 return PJ_EINVAL;
1227 }
1228
1229 /* Username for authentication must be in a@b format */
1230 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1231 PJ_LOG(1,(THIS_FILE,
1232 "Error: Username for authentication must "
1233 "be in user@domain format with IMS"));
1234 return PJ_EINVAL;
1235 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001236 }
1237 }
1238
1239
1240 return PJ_SUCCESS;
1241}
1242
1243
1244/*
1245 * Save account settings
1246 */
1247static void write_account_settings(int acc_index, pj_str_t *result)
1248{
1249 unsigned i;
1250 char line[128];
1251 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1252
1253
1254 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1255 pj_strcat2(result, line);
1256
1257
1258 /* Identity */
1259 if (acc_cfg->id.slen) {
1260 pj_ansi_sprintf(line, "--id %.*s\n",
1261 (int)acc_cfg->id.slen,
1262 acc_cfg->id.ptr);
1263 pj_strcat2(result, line);
1264 }
1265
1266 /* Registrar server */
1267 if (acc_cfg->reg_uri.slen) {
1268 pj_ansi_sprintf(line, "--registrar %.*s\n",
1269 (int)acc_cfg->reg_uri.slen,
1270 acc_cfg->reg_uri.ptr);
1271 pj_strcat2(result, line);
1272
1273 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1274 acc_cfg->reg_timeout);
1275 pj_strcat2(result, line);
1276 }
1277
1278 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001279 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001280 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001281 (int)acc_cfg->force_contact.slen,
1282 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001283 pj_strcat2(result, line);
1284 }
1285
Benny Prijonofce28542007-12-09 15:41:10 +00001286 /* */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001287 if (acc_cfg->allow_contact_rewrite==0)
Benny Prijonofce28542007-12-09 15:41:10 +00001288 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001289 pj_ansi_sprintf(line, "--contact-rewrite %i\n",
1290 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001291 pj_strcat2(result, line);
1292 }
1293
Benny Prijonod8179652008-01-23 20:39:07 +00001294#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1295 /* SRTP */
1296 if (acc_cfg->use_srtp) {
1297 pj_ansi_sprintf(line, "--use-srtp %i\n",
1298 (int)acc_cfg->use_srtp);
1299 pj_strcat2(result, line);
1300 }
1301#endif
1302
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001303 /* Proxy */
1304 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1305 pj_ansi_sprintf(line, "--proxy %.*s\n",
1306 (int)acc_cfg->proxy[i].slen,
1307 acc_cfg->proxy[i].ptr);
1308 pj_strcat2(result, line);
1309 }
1310
1311 /* Credentials */
1312 for (i=0; i<acc_cfg->cred_count; ++i) {
1313 if (acc_cfg->cred_info[i].realm.slen) {
1314 pj_ansi_sprintf(line, "--realm %.*s\n",
1315 (int)acc_cfg->cred_info[i].realm.slen,
1316 acc_cfg->cred_info[i].realm.ptr);
1317 pj_strcat2(result, line);
1318 }
1319
1320 if (acc_cfg->cred_info[i].username.slen) {
1321 pj_ansi_sprintf(line, "--username %.*s\n",
1322 (int)acc_cfg->cred_info[i].username.slen,
1323 acc_cfg->cred_info[i].username.ptr);
1324 pj_strcat2(result, line);
1325 }
1326
1327 if (acc_cfg->cred_info[i].data.slen) {
1328 pj_ansi_sprintf(line, "--password %.*s\n",
1329 (int)acc_cfg->cred_info[i].data.slen,
1330 acc_cfg->cred_info[i].data.ptr);
1331 pj_strcat2(result, line);
1332 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001333
1334 if (i != acc_cfg->cred_count - 1)
1335 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001336 }
1337
1338}
1339
1340
1341/*
1342 * Write settings.
1343 */
1344static int write_settings(const struct app_config *config,
1345 char *buf, pj_size_t max)
1346{
1347 unsigned acc_index;
1348 unsigned i;
1349 pj_str_t cfg;
1350 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001351 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001352
1353 PJ_UNUSED_ARG(max);
1354
1355 cfg.ptr = buf;
1356 cfg.slen = 0;
1357
1358 /* Logging. */
1359 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1360 pj_ansi_sprintf(line, "--log-level %d\n",
1361 config->log_cfg.level);
1362 pj_strcat2(&cfg, line);
1363
1364 pj_ansi_sprintf(line, "--app-log-level %d\n",
1365 config->log_cfg.console_level);
1366 pj_strcat2(&cfg, line);
1367
1368 if (config->log_cfg.log_filename.slen) {
1369 pj_ansi_sprintf(line, "--log-file %.*s\n",
1370 (int)config->log_cfg.log_filename.slen,
1371 config->log_cfg.log_filename.ptr);
1372 pj_strcat2(&cfg, line);
1373 }
1374
1375
1376 /* Save account settings. */
1377 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1378
1379 write_account_settings(acc_index, &cfg);
1380
1381 if (acc_index < config->acc_cnt-1)
1382 pj_strcat2(&cfg, "--next-account\n");
1383 }
1384
1385
1386 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1387
1388 /* Outbound proxy */
1389 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1390 pj_ansi_sprintf(line, "--outbound %.*s\n",
1391 (int)config->cfg.outbound_proxy[i].slen,
1392 config->cfg.outbound_proxy[i].ptr);
1393 pj_strcat2(&cfg, line);
1394 }
1395
1396
1397 /* UDP Transport. */
1398 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1399 pj_strcat2(&cfg, line);
1400
Benny Prijono0a5cad82006-09-26 13:21:02 +00001401 /* IP address, if any. */
1402 if (config->udp_cfg.public_addr.slen) {
1403 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1404 (int)config->udp_cfg.public_addr.slen,
1405 config->udp_cfg.public_addr.ptr);
1406 pj_strcat2(&cfg, line);
1407 }
1408
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001409 /* No TCP ? */
1410 if (config->no_tcp) {
1411 pj_strcat2(&cfg, "--no-tcp\n");
1412 }
1413
1414 /* No UDP ? */
1415 if (config->no_udp) {
1416 pj_strcat2(&cfg, "--no-udp\n");
1417 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001418
1419 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001420 if (config->cfg.stun_domain.slen) {
1421 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1422 (int)config->cfg.stun_domain.slen,
1423 config->cfg.stun_domain.ptr);
1424 pj_strcat2(&cfg, line);
1425 }
1426 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001427 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001428 (int)config->cfg.stun_host.slen,
1429 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001430 pj_strcat2(&cfg, line);
1431 }
1432
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001433 /* TLS */
1434 if (config->use_tls)
1435 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001436 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001437 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001438 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1439 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001440 pj_strcat2(&cfg, line);
1441 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001442 if (config->udp_cfg.tls_setting.cert_file.slen) {
1443 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1444 (int)config->udp_cfg.tls_setting.cert_file.slen,
1445 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001446 pj_strcat2(&cfg, line);
1447 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001448 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1449 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1450 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1451 config->udp_cfg.tls_setting.privkey_file.ptr);
1452 pj_strcat2(&cfg, line);
1453 }
1454
1455 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001456 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001457 (int)config->udp_cfg.tls_setting.password.slen,
1458 config->udp_cfg.tls_setting.password.ptr);
1459 pj_strcat2(&cfg, line);
1460 }
1461
1462 if (config->udp_cfg.tls_setting.verify_server)
1463 pj_strcat2(&cfg, "--tls-verify-server\n");
1464
1465 if (config->udp_cfg.tls_setting.verify_client)
1466 pj_strcat2(&cfg, "--tls-verify-client\n");
1467
1468 if (config->udp_cfg.tls_setting.timeout.sec) {
1469 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001470 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001471 pj_strcat2(&cfg, line);
1472 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001473
1474 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1475
Benny Prijonof6508982008-01-25 09:02:33 +00001476 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001477#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001478 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1479 pj_ansi_sprintf(line, "--use-srtp %d\n",
1480 app_config.cfg.use_srtp);
1481 pj_strcat2(&cfg, line);
1482 }
1483 if (app_config.cfg.srtp_secure_signaling !=
1484 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1485 {
1486 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1487 app_config.cfg.srtp_secure_signaling);
1488 pj_strcat2(&cfg, line);
1489 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001490#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001491
Benny Prijonof76e1392008-06-06 14:51:48 +00001492 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001493 if (config->media_cfg.enable_ice)
1494 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001495
1496 if (config->media_cfg.enable_turn)
1497 pj_strcat2(&cfg, "--use-turn\n");
1498
1499 if (config->media_cfg.ice_no_host_cands)
1500 pj_strcat2(&cfg, "--ice-no-host\n");
1501
1502 if (config->media_cfg.turn_server.slen) {
1503 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1504 (int)config->media_cfg.turn_server.slen,
1505 config->media_cfg.turn_server.ptr);
1506 pj_strcat2(&cfg, line);
1507 }
1508
1509 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1510 pj_strcat2(&cfg, "--turn-tcp\n");
1511
1512 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1513 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1514 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1515 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1516 pj_strcat2(&cfg, line);
1517 }
1518
1519 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1520 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1521 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1522 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1523 pj_strcat2(&cfg, line);
1524 }
1525
1526 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001527 if (config->null_audio)
1528 pj_strcat2(&cfg, "--null-audio\n");
1529 if (config->auto_play)
1530 pj_strcat2(&cfg, "--auto-play\n");
1531 if (config->auto_loop)
1532 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001533 if (config->auto_conf)
1534 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001535 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001536 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001537 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001538 pj_strcat2(&cfg, line);
1539 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001540 for (i=0; i<config->tone_count; ++i) {
1541 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1542 config->tones[i].freq1, config->tones[i].freq2,
1543 config->tones[i].on_msec, config->tones[i].off_msec);
1544 pj_strcat2(&cfg, line);
1545 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001546 if (config->rec_file.slen) {
1547 pj_ansi_sprintf(line, "--rec-file %s\n",
1548 config->rec_file.ptr);
1549 pj_strcat2(&cfg, line);
1550 }
1551 if (config->auto_rec)
1552 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001553 if (config->capture_dev != PJSUA_INVALID_ID) {
1554 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1555 pj_strcat2(&cfg, line);
1556 }
1557 if (config->playback_dev != PJSUA_INVALID_ID) {
1558 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1559 pj_strcat2(&cfg, line);
1560 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001561 if (config->media_cfg.snd_auto_close_time != -1) {
1562 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1563 config->media_cfg.snd_auto_close_time);
1564 pj_strcat2(&cfg, line);
1565 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001566 if (config->no_tones) {
1567 pj_strcat2(&cfg, "--no-tones\n");
1568 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001569
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001570 /* Sound device latency */
1571 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1572 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1573 pj_strcat2(&cfg, line);
1574 }
1575 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1576 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1577 pj_strcat2(&cfg, line);
1578 }
1579
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001580 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001581 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001582 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001583 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001584 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001585 } else {
1586 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1587 config->media_cfg.clock_rate);
1588 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001589 }
Benny Prijono70972992006-08-05 11:13:58 +00001590
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001591 if (config->media_cfg.snd_clock_rate &&
1592 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1593 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001594 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1595 config->media_cfg.snd_clock_rate);
1596 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001597 }
1598
Benny Prijono7d60d052008-03-29 12:24:20 +00001599 /* Stereo mode. */
1600 if (config->media_cfg.channel_count == 2) {
1601 pj_ansi_sprintf(line, "--stereo\n");
1602 pj_strcat2(&cfg, line);
1603 }
1604
Benny Prijono70972992006-08-05 11:13:58 +00001605 /* quality */
1606 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001607 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001608 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001609 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001610 } else {
1611 pj_ansi_sprintf(line, "#using default --quality %d\n",
1612 config->media_cfg.quality);
1613 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001614 }
Benny Prijono0498d902006-06-19 14:49:14 +00001615
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001616
1617 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001618 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001619 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001620 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001621 pj_strcat2(&cfg, line);
1622 }
1623
Benny Prijono70972992006-08-05 11:13:58 +00001624 /* no-vad */
1625 if (config->media_cfg.no_vad) {
1626 pj_strcat2(&cfg, "--no-vad\n");
1627 }
1628
1629 /* ec-tail */
1630 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1631 pj_ansi_sprintf(line, "--ec-tail %d\n",
1632 config->media_cfg.ec_tail_len);
1633 pj_strcat2(&cfg, line);
1634 } else {
1635 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1636 config->media_cfg.ec_tail_len);
1637 pj_strcat2(&cfg, line);
1638 }
1639
1640
1641 /* ilbc-mode */
1642 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1643 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1644 config->media_cfg.ilbc_mode);
1645 pj_strcat2(&cfg, line);
1646 } else {
1647 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1648 config->media_cfg.ilbc_mode);
1649 pj_strcat2(&cfg, line);
1650 }
1651
1652 /* RTP drop */
1653 if (config->media_cfg.tx_drop_pct) {
1654 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1655 config->media_cfg.tx_drop_pct);
1656 pj_strcat2(&cfg, line);
1657
1658 }
1659 if (config->media_cfg.rx_drop_pct) {
1660 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1661 config->media_cfg.rx_drop_pct);
1662 pj_strcat2(&cfg, line);
1663
1664 }
1665
1666
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001667 /* Start RTP port. */
1668 pj_ansi_sprintf(line, "--rtp-port %d\n",
1669 config->rtp_cfg.port);
1670 pj_strcat2(&cfg, line);
1671
1672 /* Add codec. */
1673 for (i=0; i<config->codec_cnt; ++i) {
1674 pj_ansi_sprintf(line, "--add-codec %s\n",
1675 config->codec_arg[i].ptr);
1676 pj_strcat2(&cfg, line);
1677 }
Benny Prijonofce28542007-12-09 15:41:10 +00001678 /* Disable codec */
1679 for (i=0; i<config->codec_dis_cnt; ++i) {
1680 pj_ansi_sprintf(line, "--dis-codec %s\n",
1681 config->codec_dis[i].ptr);
1682 pj_strcat2(&cfg, line);
1683 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001684
1685 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1686
1687 /* Auto-answer. */
1688 if (config->auto_answer != 0) {
1689 pj_ansi_sprintf(line, "--auto-answer %d\n",
1690 config->auto_answer);
1691 pj_strcat2(&cfg, line);
1692 }
1693
1694 /* Max calls. */
1695 pj_ansi_sprintf(line, "--max-calls %d\n",
1696 config->cfg.max_calls);
1697 pj_strcat2(&cfg, line);
1698
1699 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001700 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001701 pj_ansi_sprintf(line, "--duration %d\n",
1702 config->duration);
1703 pj_strcat2(&cfg, line);
1704 }
1705
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001706 /* norefersub ? */
1707 if (config->no_refersub) {
1708 pj_strcat2(&cfg, "--norefersub\n");
1709 }
1710
Benny Prijonofce28542007-12-09 15:41:10 +00001711 if (pjsip_use_compact_form)
1712 {
1713 pj_strcat2(&cfg, "--use-compact-form\n");
1714 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001715
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001716 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1717
1718 /* Add buddies. */
1719 for (i=0; i<config->buddy_cnt; ++i) {
1720 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1721 (int)config->buddy_cfg[i].uri.slen,
1722 config->buddy_cfg[i].uri.ptr);
1723 pj_strcat2(&cfg, line);
1724 }
1725
1726
1727 *(cfg.ptr + cfg.slen) = '\0';
1728 return cfg.slen;
1729}
1730
1731
1732/*
1733 * Dump application states.
1734 */
1735static void app_dump(pj_bool_t detail)
1736{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001737 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001738}
1739
1740
1741/*****************************************************************************
1742 * Console application
1743 */
1744
Benny Prijono91d20f42008-06-14 19:42:37 +00001745static void ringback_start(pjsua_call_id call_id)
1746{
1747 if (app_config.no_tones)
1748 return;
1749
1750 if (app_config.call_data[call_id].ringback_on)
1751 return;
1752
1753 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1754
1755 if (++app_config.ringback_cnt==1 &&
1756 app_config.ringback_slot!=PJSUA_INVALID_ID)
1757 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001758 pjsua_conf_connect(app_config.ringback_slot, 0);
1759 }
1760}
1761
1762static void ring_stop(pjsua_call_id call_id)
1763{
1764 if (app_config.no_tones)
1765 return;
1766
1767 if (app_config.call_data[call_id].ringback_on) {
1768 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1769
1770 pj_assert(app_config.ringback_cnt>0);
1771 if (--app_config.ringback_cnt == 0 &&
1772 app_config.ringback_slot!=PJSUA_INVALID_ID)
1773 {
1774 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001775 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001776 }
1777 }
1778
1779 if (app_config.call_data[call_id].ring_on) {
1780 app_config.call_data[call_id].ring_on = PJ_FALSE;
1781
1782 pj_assert(app_config.ring_cnt>0);
1783 if (--app_config.ring_cnt == 0 &&
1784 app_config.ring_slot!=PJSUA_INVALID_ID)
1785 {
1786 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001787 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001788 }
1789 }
1790}
1791
1792static void ring_start(pjsua_call_id call_id)
1793{
1794 if (app_config.no_tones)
1795 return;
1796
1797 if (app_config.call_data[call_id].ring_on)
1798 return;
1799
1800 app_config.call_data[call_id].ring_on = PJ_TRUE;
1801
1802 if (++app_config.ring_cnt==1 &&
1803 app_config.ring_slot!=PJSUA_INVALID_ID)
1804 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001805 pjsua_conf_connect(app_config.ring_slot, 0);
1806 }
1807}
1808
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001809/*
1810 * Find next call when current call is disconnected or when user
1811 * press ']'
1812 */
1813static pj_bool_t find_next_call(void)
1814{
1815 int i, max;
1816
1817 max = pjsua_call_get_max_count();
1818 for (i=current_call+1; i<max; ++i) {
1819 if (pjsua_call_is_active(i)) {
1820 current_call = i;
1821 return PJ_TRUE;
1822 }
1823 }
1824
1825 for (i=0; i<current_call; ++i) {
1826 if (pjsua_call_is_active(i)) {
1827 current_call = i;
1828 return PJ_TRUE;
1829 }
1830 }
1831
1832 current_call = PJSUA_INVALID_ID;
1833 return PJ_FALSE;
1834}
1835
1836
1837/*
1838 * Find previous call when user press '['
1839 */
1840static pj_bool_t find_prev_call(void)
1841{
1842 int i, max;
1843
1844 max = pjsua_call_get_max_count();
1845 for (i=current_call-1; i>=0; --i) {
1846 if (pjsua_call_is_active(i)) {
1847 current_call = i;
1848 return PJ_TRUE;
1849 }
1850 }
1851
1852 for (i=max-1; i>current_call; --i) {
1853 if (pjsua_call_is_active(i)) {
1854 current_call = i;
1855 return PJ_TRUE;
1856 }
1857 }
1858
1859 current_call = PJSUA_INVALID_ID;
1860 return PJ_FALSE;
1861}
1862
1863
Benny Prijono804ff0a2006-09-14 11:17:48 +00001864/* Callback from timer when the maximum call duration has been
1865 * exceeded.
1866 */
1867static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1868 struct pj_timer_entry *entry)
1869{
1870 pjsua_call_id call_id = entry->id;
1871 pjsua_msg_data msg_data;
1872 pjsip_generic_string_hdr warn;
1873 pj_str_t hname = pj_str("Warning");
1874 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1875
1876 PJ_UNUSED_ARG(timer_heap);
1877
Benny Prijono148c9dd2006-09-19 13:37:53 +00001878 if (call_id == PJSUA_INVALID_ID) {
1879 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1880 return;
1881 }
1882
Benny Prijono804ff0a2006-09-14 11:17:48 +00001883 /* Add warning header */
1884 pjsua_msg_data_init(&msg_data);
1885 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1886 pj_list_push_back(&msg_data.hdr_list, &warn);
1887
1888 /* Call duration has been exceeded; disconnect the call */
1889 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1890 "for call %d, disconnecting the call",
1891 app_config.duration, call_id));
1892 entry->id = PJSUA_INVALID_ID;
1893 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1894}
1895
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001896
1897/*
1898 * Handler when invite state has changed.
1899 */
1900static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1901{
1902 pjsua_call_info call_info;
1903
1904 PJ_UNUSED_ARG(e);
1905
1906 pjsua_call_get_info(call_id, &call_info);
1907
1908 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1909
Benny Prijono91d20f42008-06-14 19:42:37 +00001910 /* Stop all ringback for this call */
1911 ring_stop(call_id);
1912
Benny Prijono804ff0a2006-09-14 11:17:48 +00001913 /* Cancel duration timer, if any */
1914 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1915 struct call_data *cd = &app_config.call_data[call_id];
1916 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1917
1918 cd->timer.id = PJSUA_INVALID_ID;
1919 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1920 }
1921
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00001922 /* Rewind play file when hangup automatically,
1923 * since file is not looped
1924 */
1925 if (app_config.auto_play_hangup)
1926 pjsua_player_set_pos(app_config.wav_id, 0);
1927
1928
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001929 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1930 call_id,
1931 call_info.last_status,
1932 call_info.last_status_text.ptr));
1933
1934 if (call_id == current_call) {
1935 find_next_call();
1936 }
1937
Benny Prijono4be63b52006-11-25 14:50:25 +00001938 /* Dump media state upon disconnected */
1939 if (1) {
Benny Prijono6eddd872008-03-21 13:46:08 +00001940 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1941 sizeof(some_buf), " ");
Benny Prijono4be63b52006-11-25 14:50:25 +00001942 PJ_LOG(5,(THIS_FILE,
1943 "Call %d disconnected, dumping media stats\n%s",
Benny Prijono6eddd872008-03-21 13:46:08 +00001944 call_id, some_buf));
Benny Prijono4be63b52006-11-25 14:50:25 +00001945 }
1946
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001947 } else {
1948
Benny Prijono804ff0a2006-09-14 11:17:48 +00001949 if (app_config.duration!=NO_LIMIT &&
1950 call_info.state == PJSIP_INV_STATE_CONFIRMED)
1951 {
1952 /* Schedule timer to hangup call after the specified duration */
1953 struct call_data *cd = &app_config.call_data[call_id];
1954 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1955 pj_time_val delay;
1956
1957 cd->timer.id = call_id;
1958 delay.sec = app_config.duration;
1959 delay.msec = 0;
1960 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
1961 }
1962
Benny Prijono4be63b52006-11-25 14:50:25 +00001963 if (call_info.state == PJSIP_INV_STATE_EARLY) {
1964 int code;
1965 pj_str_t reason;
1966 pjsip_msg *msg;
1967
1968 /* This can only occur because of TX or RX message */
1969 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
1970
1971 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1972 msg = e->body.tsx_state.src.rdata->msg_info.msg;
1973 } else {
1974 msg = e->body.tsx_state.src.tdata->msg;
1975 }
1976
1977 code = msg->line.status.code;
1978 reason = msg->line.status.reason;
1979
Benny Prijono91d20f42008-06-14 19:42:37 +00001980 /* Start ringback for 180 for UAC unless there's SDP in 180 */
1981 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
1982 msg->body == NULL &&
1983 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
1984 {
1985 ringback_start(call_id);
1986 }
1987
Benny Prijono4be63b52006-11-25 14:50:25 +00001988 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
1989 call_id, call_info.state_text.ptr,
1990 code, (int)reason.slen, reason.ptr));
1991 } else {
1992 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
1993 call_id,
1994 call_info.state_text.ptr));
1995 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001996
1997 if (current_call==PJSUA_INVALID_ID)
1998 current_call = call_id;
1999
2000 }
2001}
2002
2003
2004/**
2005 * Handler when there is incoming call.
2006 */
2007static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2008 pjsip_rx_data *rdata)
2009{
2010 pjsua_call_info call_info;
2011
2012 PJ_UNUSED_ARG(acc_id);
2013 PJ_UNUSED_ARG(rdata);
2014
2015 pjsua_call_get_info(call_id, &call_info);
2016
Benny Prijono91d20f42008-06-14 19:42:37 +00002017 /* Start ringback */
2018 ring_start(call_id);
2019
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002020 if (app_config.auto_answer > 0) {
2021 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2022 }
2023
2024 if (app_config.auto_answer < 200) {
2025 PJ_LOG(3,(THIS_FILE,
2026 "Incoming call for account %d!\n"
2027 "From: %s\n"
2028 "To: %s\n"
2029 "Press a to answer or h to reject call",
2030 acc_id,
2031 call_info.remote_info.ptr,
2032 call_info.local_info.ptr));
2033 }
2034}
2035
2036
2037/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002038 * Handler when a transaction within a call has changed state.
2039 */
2040static void on_call_tsx_state(pjsua_call_id call_id,
2041 pjsip_transaction *tsx,
2042 pjsip_event *e)
2043{
2044 const pjsip_method info_method =
2045 {
2046 PJSIP_OTHER_METHOD,
2047 { "INFO", 4 }
2048 };
2049
2050 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2051 /*
2052 * Handle INFO method.
2053 */
2054 if (tsx->role == PJSIP_ROLE_UAC &&
2055 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002056 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2057 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002058 {
2059 /* Status of outgoing INFO request */
2060 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2061 PJ_LOG(4,(THIS_FILE,
2062 "Call %d: DTMF sent successfully with INFO",
2063 call_id));
2064 } else if (tsx->status_code >= 300) {
2065 PJ_LOG(4,(THIS_FILE,
2066 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2067 call_id,
2068 tsx->status_code,
2069 (int)tsx->status_text.slen,
2070 tsx->status_text.ptr));
2071 }
2072 } else if (tsx->role == PJSIP_ROLE_UAS &&
2073 tsx->state == PJSIP_TSX_STATE_TRYING)
2074 {
2075 /* Answer incoming INFO with 200/OK */
2076 pjsip_rx_data *rdata;
2077 pjsip_tx_data *tdata;
2078 pj_status_t status;
2079
2080 rdata = e->body.tsx_state.src.rdata;
2081
2082 if (rdata->msg_info.msg->body) {
2083 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2084 200, NULL, &tdata);
2085 if (status == PJ_SUCCESS)
2086 status = pjsip_tsx_send_msg(tsx, tdata);
2087
2088 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2089 call_id,
2090 (int)rdata->msg_info.msg->body->len,
2091 rdata->msg_info.msg->body->data));
2092 } else {
2093 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2094 400, NULL, &tdata);
2095 if (status == PJ_SUCCESS)
2096 status = pjsip_tsx_send_msg(tsx, tdata);
2097 }
2098 }
2099 }
2100}
2101
2102
2103/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002104 * Callback on media state changed event.
2105 * The action may connect the call to sound device, to file, or
2106 * to loop the call.
2107 */
2108static void on_call_media_state(pjsua_call_id call_id)
2109{
2110 pjsua_call_info call_info;
2111
2112 pjsua_call_get_info(call_id, &call_info);
2113
Benny Prijono91d20f42008-06-14 19:42:37 +00002114 /* Stop ringback */
2115 ring_stop(call_id);
2116
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002117 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
2118 pj_bool_t connect_sound = PJ_TRUE;
2119
2120 /* Loopback sound, if desired */
2121 if (app_config.auto_loop) {
2122 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2123 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00002124
2125 /* Automatically record conversation, if desired */
2126 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2127 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2128 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002129 }
2130
2131 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002132 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2133 app_config.wav_port != PJSUA_INVALID_ID)
2134 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002135 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2136 connect_sound = PJ_FALSE;
2137 }
2138
Benny Prijono7ca96da2006-08-07 12:11:40 +00002139 /* Put call in conference with other calls, if desired */
2140 if (app_config.auto_conf) {
2141 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002142 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002143 unsigned i;
2144
2145 /* Get all calls, and establish media connection between
2146 * this call and other calls.
2147 */
2148 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002149
Benny Prijono7ca96da2006-08-07 12:11:40 +00002150 for (i=0; i<call_cnt; ++i) {
2151 if (call_ids[i] == call_id)
2152 continue;
2153
2154 if (!pjsua_call_has_media(call_ids[i]))
2155 continue;
2156
2157 pjsua_conf_connect(call_info.conf_slot,
2158 pjsua_call_get_conf_port(call_ids[i]));
2159 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2160 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002161
2162 /* Automatically record conversation, if desired */
2163 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2164 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2165 app_config.rec_port);
2166 }
2167
Benny Prijono7ca96da2006-08-07 12:11:40 +00002168 }
2169
2170 /* Also connect call to local sound device */
2171 connect_sound = PJ_TRUE;
2172 }
2173
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002174 /* Otherwise connect to sound device */
2175 if (connect_sound) {
2176 pjsua_conf_connect(call_info.conf_slot, 0);
2177 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002178
2179 /* Automatically record conversation, if desired */
2180 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2181 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2182 pjsua_conf_connect(0, app_config.rec_port);
2183 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002184 }
2185
2186 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
2187
2188 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
2189 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2190 call_id));
2191 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
2192 PJ_LOG(3,(THIS_FILE,
2193 "Media for call %d is suspended (hold) by remote",
2194 call_id));
Benny Prijono096c56c2007-09-15 08:30:16 +00002195 } else if (call_info.media_status == PJSUA_CALL_MEDIA_ERROR) {
2196 pj_str_t reason = pj_str("ICE negotiation failed");
2197
2198 PJ_LOG(1,(THIS_FILE,
2199 "Media has reported error, disconnecting call"));
2200
2201 pjsua_call_hangup(call_id, 500, &reason, NULL);
2202
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002203 } else {
2204 PJ_LOG(3,(THIS_FILE,
2205 "Media for call %d is inactive",
2206 call_id));
2207 }
2208}
2209
Benny Prijono0875ae82006-12-26 00:11:48 +00002210/*
2211 * DTMF callback.
2212 */
2213static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2214{
2215 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2216}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002217
2218/*
2219 * Handler registration status has changed.
2220 */
2221static void on_reg_state(pjsua_acc_id acc_id)
2222{
2223 PJ_UNUSED_ARG(acc_id);
2224
2225 // Log already written.
2226}
2227
2228
2229/*
2230 * Handler on buddy state changed.
2231 */
2232static void on_buddy_state(pjsua_buddy_id buddy_id)
2233{
2234 pjsua_buddy_info info;
2235 pjsua_buddy_get_info(buddy_id, &info);
2236
2237 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2238 (int)info.uri.slen,
2239 info.uri.ptr,
2240 (int)info.status_text.slen,
2241 info.status_text.ptr));
2242}
2243
2244
2245/**
2246 * Incoming IM message (i.e. MESSAGE request)!
2247 */
2248static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2249 const pj_str_t *to, const pj_str_t *contact,
2250 const pj_str_t *mime_type, const pj_str_t *text)
2251{
2252 /* Note: call index may be -1 */
2253 PJ_UNUSED_ARG(call_id);
2254 PJ_UNUSED_ARG(to);
2255 PJ_UNUSED_ARG(contact);
2256 PJ_UNUSED_ARG(mime_type);
2257
Benny Prijonof4b538d2007-05-14 16:45:20 +00002258 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002259 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002260 (int)text->slen, text->ptr,
2261 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002262}
2263
2264
2265/**
2266 * Received typing indication
2267 */
2268static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2269 const pj_str_t *to, const pj_str_t *contact,
2270 pj_bool_t is_typing)
2271{
2272 PJ_UNUSED_ARG(call_id);
2273 PJ_UNUSED_ARG(to);
2274 PJ_UNUSED_ARG(contact);
2275
2276 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2277 (int)from->slen, from->ptr,
2278 (is_typing?"is typing..":"has stopped typing")));
2279}
2280
2281
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002282/**
2283 * Call transfer request status.
2284 */
2285static void on_call_transfer_status(pjsua_call_id call_id,
2286 int status_code,
2287 const pj_str_t *status_text,
2288 pj_bool_t final,
2289 pj_bool_t *p_cont)
2290{
2291 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2292 call_id, status_code,
2293 (int)status_text->slen, status_text->ptr,
2294 (final ? "[final]" : "")));
2295
2296 if (status_code/100 == 2) {
2297 PJ_LOG(3,(THIS_FILE,
2298 "Call %d: call transfered successfully, disconnecting call",
2299 call_id));
2300 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2301 *p_cont = PJ_FALSE;
2302 }
2303}
2304
2305
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002306/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002307 * Notification that call is being replaced.
2308 */
2309static void on_call_replaced(pjsua_call_id old_call_id,
2310 pjsua_call_id new_call_id)
2311{
2312 pjsua_call_info old_ci, new_ci;
2313
2314 pjsua_call_get_info(old_call_id, &old_ci);
2315 pjsua_call_get_info(new_call_id, &new_ci);
2316
2317 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2318 "call %d with %.*s",
2319 old_call_id,
2320 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2321 new_call_id,
2322 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2323}
2324
2325
2326/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002327 * NAT type detection callback.
2328 */
2329static void on_nat_detect(const pj_stun_nat_detect_result *res)
2330{
2331 if (res->status != PJ_SUCCESS) {
2332 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2333 } else {
2334 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2335 }
2336}
2337
2338
2339/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002340 * Print buddy list.
2341 */
2342static void print_buddy_list(void)
2343{
2344 pjsua_buddy_id ids[64];
2345 int i;
2346 unsigned count = PJ_ARRAY_SIZE(ids);
2347
2348 puts("Buddy list:");
2349
2350 pjsua_enum_buddies(ids, &count);
2351
2352 if (count == 0)
2353 puts(" -none-");
2354 else {
2355 for (i=0; i<(int)count; ++i) {
2356 pjsua_buddy_info info;
2357
2358 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2359 continue;
2360
Benny Prijono4461c7d2007-08-25 13:36:15 +00002361 printf(" [%2d] <%.*s> %.*s\n",
2362 ids[i]+1,
2363 (int)info.status_text.slen,
2364 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002365 (int)info.uri.slen,
2366 info.uri.ptr);
2367 }
2368 }
2369 puts("");
2370}
2371
2372
2373/*
2374 * Print account status.
2375 */
2376static void print_acc_status(int acc_id)
2377{
2378 char buf[80];
2379 pjsua_acc_info info;
2380
2381 pjsua_acc_get_info(acc_id, &info);
2382
2383 if (!info.has_registration) {
2384 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2385 (int)info.status_text.slen,
2386 info.status_text.ptr);
2387
2388 } else {
2389 pj_ansi_snprintf(buf, sizeof(buf),
2390 "%d/%.*s (expires=%d)",
2391 info.status,
2392 (int)info.status_text.slen,
2393 info.status_text.ptr,
2394 info.expires);
2395
2396 }
2397
2398 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2399 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002400 printf(" Online status: %.*s\n",
2401 (int)info.online_status_text.slen,
2402 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002403}
2404
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002405/* Playfile done notification, set timer to hangup calls */
2406pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2407{
2408 pj_time_val delay;
2409
2410 PJ_UNUSED_ARG(port);
2411 PJ_UNUSED_ARG(usr_data);
2412
2413 /* Just rewind WAV when it is played outside of call */
2414 if (pjsua_call_get_count() == 0) {
2415 pjsua_player_set_pos(app_config.wav_id, 0);
2416 return PJ_SUCCESS;
2417 }
2418
2419 /* Timer is already active */
2420 if (app_config.auto_hangup_timer.id == 1)
2421 return PJ_SUCCESS;
2422
2423 app_config.auto_hangup_timer.id = 1;
2424 delay.sec = 0;
2425 delay.msec = 200; /* Give 200 ms before hangup */
2426 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2427 &app_config.auto_hangup_timer,
2428 &delay);
2429
2430 return PJ_SUCCESS;
2431}
2432
2433/* Auto hangup timer callback */
2434static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2435 struct pj_timer_entry *entry)
2436{
2437 PJ_UNUSED_ARG(timer_heap);
2438 PJ_UNUSED_ARG(entry);
2439
2440 app_config.auto_hangup_timer.id = 0;
2441 pjsua_call_hangup_all();
2442}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002443
2444/*
2445 * Show a bit of help.
2446 */
2447static void keystroke_help(void)
2448{
2449 pjsua_acc_id acc_ids[16];
2450 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2451 int i;
2452
2453 printf(">>>>\n");
2454
2455 pjsua_enum_accs(acc_ids, &count);
2456
2457 printf("Account list:\n");
2458 for (i=0; i<(int)count; ++i)
2459 print_acc_status(acc_ids[i]);
2460
2461 print_buddy_list();
2462
2463 //puts("Commands:");
2464 puts("+=============================================================================+");
2465 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2466 puts("| | | |");
2467 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2468 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002469 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2470 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2471 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2472 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002473 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2474 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002475 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002476 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002477 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2478 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2479 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2480 puts("| | V Adjust audio Volume | f Save config |");
2481 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002482 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoebc32c32008-06-12 13:30:39 +00002483 puts("| q QUIT sleep MS echo [0|1] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002484 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002485
2486 i = pjsua_call_get_count();
2487 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002488
2489 if (current_call != PJSUA_INVALID_ID) {
2490 pjsua_call_info ci;
2491 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2492 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2493 (int)ci.remote_info.slen, ci.remote_info.ptr,
2494 (int)ci.state_text.slen, ci.state_text.ptr);
2495 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002496}
2497
2498
2499/*
2500 * Input simple string
2501 */
2502static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2503{
2504 char *p;
2505
2506 printf("%s (empty to cancel): ", title); fflush(stdout);
2507 fgets(buf, len, stdin);
2508
2509 /* Remove trailing newlines. */
2510 for (p=buf; ; ++p) {
2511 if (*p=='\r' || *p=='\n') *p='\0';
2512 else if (!*p) break;
2513 }
2514
2515 if (!*buf)
2516 return PJ_FALSE;
2517
2518 return PJ_TRUE;
2519}
2520
2521
2522#define NO_NB -2
2523struct input_result
2524{
2525 int nb_result;
2526 char *uri_result;
2527};
2528
2529
2530/*
2531 * Input URL.
2532 */
2533static void ui_input_url(const char *title, char *buf, int len,
2534 struct input_result *result)
2535{
2536 result->nb_result = NO_NB;
2537 result->uri_result = NULL;
2538
2539 print_buddy_list();
2540
2541 printf("Choices:\n"
2542 " 0 For current dialog.\n"
2543 " -1 All %d buddies in buddy list\n"
2544 " [1 -%2d] Select from buddy list\n"
2545 " URL An URL\n"
2546 " <Enter> Empty input (or 'q') to cancel\n"
2547 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2548 printf("%s: ", title);
2549
2550 fflush(stdout);
2551 fgets(buf, len, stdin);
2552 len = strlen(buf);
2553
2554 /* Left trim */
2555 while (pj_isspace(*buf)) {
2556 ++buf;
2557 --len;
2558 }
2559
2560 /* Remove trailing newlines */
2561 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2562 buf[--len] = '\0';
2563
2564 if (len == 0 || buf[0]=='q')
2565 return;
2566
2567 if (pj_isdigit(*buf) || *buf=='-') {
2568
2569 int i;
2570
2571 if (*buf=='-')
2572 i = 1;
2573 else
2574 i = 0;
2575
2576 for (; i<len; ++i) {
2577 if (!pj_isdigit(buf[i])) {
2578 puts("Invalid input");
2579 return;
2580 }
2581 }
2582
2583 result->nb_result = my_atoi(buf);
2584
2585 if (result->nb_result >= 0 &&
2586 result->nb_result <= (int)pjsua_get_buddy_count())
2587 {
2588 return;
2589 }
2590 if (result->nb_result == -1)
2591 return;
2592
2593 puts("Invalid input");
2594 result->nb_result = NO_NB;
2595 return;
2596
2597 } else {
2598 pj_status_t status;
2599
2600 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2601 pjsua_perror(THIS_FILE, "Invalid URL", status);
2602 return;
2603 }
2604
2605 result->uri_result = buf;
2606 }
2607}
2608
2609/*
2610 * List the ports in conference bridge
2611 */
2612static void conf_list(void)
2613{
2614 unsigned i, count;
2615 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2616
2617 printf("Conference ports:\n");
2618
2619 count = PJ_ARRAY_SIZE(id);
2620 pjsua_enum_conf_ports(id, &count);
2621
2622 for (i=0; i<count; ++i) {
2623 char txlist[PJSUA_MAX_CALLS*4+10];
2624 unsigned j;
2625 pjsua_conf_port_info info;
2626
2627 pjsua_conf_get_port_info(id[i], &info);
2628
2629 txlist[0] = '\0';
2630 for (j=0; j<info.listener_cnt; ++j) {
2631 char s[10];
2632 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2633 pj_ansi_strcat(txlist, s);
2634 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002635 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002636 info.slot_id,
2637 info.clock_rate/1000,
2638 info.samples_per_frame * 1000 / info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002639 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002640 (int)info.name.slen,
2641 info.name.ptr,
2642 txlist);
2643
2644 }
2645 puts("");
2646}
2647
2648
2649/*
Benny Prijono56315612006-07-18 14:39:40 +00002650 * Send arbitrary request to remote host
2651 */
2652static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2653{
2654 pj_str_t str_method;
2655 pjsip_method method;
2656 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002657 pjsip_endpoint *endpt;
2658 pj_status_t status;
2659
2660 endpt = pjsua_get_pjsip_endpt();
2661
2662 str_method = pj_str(cstr_method);
2663 pjsip_method_init_np(&method, &str_method);
2664
Benny Prijonofff245c2007-04-02 11:44:47 +00002665 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002666
Benny Prijonob988d762007-12-05 04:30:04 +00002667 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002668 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002669 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002670 return;
2671 }
2672}
2673
2674
2675/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002676 * Change extended online status.
2677 */
2678static void change_online_status(void)
2679{
2680 char menuin[32];
2681 pj_bool_t online_status;
2682 pjrpid_element elem;
2683 int i, choice;
2684
2685 enum {
2686 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2687 };
2688
2689 struct opt {
2690 int id;
2691 char *name;
2692 } opts[] = {
2693 { AVAILABLE, "Available" },
2694 { BUSY, "Busy"},
2695 { OTP, "On the phone"},
2696 { IDLE, "Idle"},
2697 { AWAY, "Away"},
2698 { BRB, "Be right back"},
2699 { OFFLINE, "Offline"}
2700 };
2701
2702 printf("\n"
2703 "Choices:\n");
2704 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2705 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2706 }
2707
2708 if (!simple_input("Select status", menuin, sizeof(menuin)))
2709 return;
2710
2711 choice = atoi(menuin) - 1;
2712 if (choice < 0 || choice >= OPT_MAX) {
2713 puts("Invalid selection");
2714 return;
2715 }
2716
2717 pj_bzero(&elem, sizeof(elem));
2718 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2719
2720 online_status = PJ_TRUE;
2721
2722 switch (choice) {
2723 case AVAILABLE:
2724 break;
2725 case BUSY:
2726 elem.activity = PJRPID_ACTIVITY_BUSY;
2727 elem.note = pj_str("Busy");
2728 break;
2729 case OTP:
2730 elem.activity = PJRPID_ACTIVITY_BUSY;
2731 elem.note = pj_str("On the phone");
2732 break;
2733 case IDLE:
2734 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2735 elem.note = pj_str("Idle");
2736 break;
2737 case AWAY:
2738 elem.activity = PJRPID_ACTIVITY_AWAY;
2739 elem.note = pj_str("Away");
2740 break;
2741 case BRB:
2742 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2743 elem.note = pj_str("Be right back");
2744 break;
2745 case OFFLINE:
2746 online_status = PJ_FALSE;
2747 break;
2748 }
2749
2750 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2751}
2752
2753
2754/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002755 * Change codec priorities.
2756 */
2757static void manage_codec_prio(void)
2758{
2759 pjsua_codec_info c[32];
2760 unsigned i, count = PJ_ARRAY_SIZE(c);
2761 char input[32];
2762 char *codec, *prio;
2763 pj_str_t id;
2764 int new_prio;
2765 pj_status_t status;
2766
2767 printf("List of codecs:\n");
2768
2769 pjsua_enum_codecs(c, &count);
2770 for (i=0; i<count; ++i) {
2771 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2772 c[i].codec_id.ptr);
2773 }
2774
2775 puts("");
2776 puts("Enter codec name and its new priority (e.g. \"speex/16000 200\"), empty to cancel:");
2777
2778 printf("Codec name and priority: ");
2779 fgets(input, sizeof(input), stdin);
2780 if (input[0]=='\r' || input[0]=='\n') {
2781 puts("Done");
2782 return;
2783 }
2784
2785 codec = strtok(input, " \t\r\n");
2786 prio = strtok(NULL, " \r\n");
2787
2788 if (!codec || !prio) {
2789 puts("Invalid input");
2790 return;
2791 }
2792
2793 new_prio = atoi(prio);
2794 if (new_prio < 0)
2795 new_prio = 0;
2796 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2797 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2798
2799 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2800 (pj_uint8_t)new_prio);
2801 if (status != PJ_SUCCESS)
2802 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2803}
2804
2805
2806/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002807 * Main "user interface" loop.
2808 */
2809void console_app_main(const pj_str_t *uri_to_call)
2810{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002811 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002812 char buf[128];
2813 char text[128];
2814 int i, count;
2815 char *uri;
2816 pj_str_t tmp;
2817 struct input_result result;
2818 pjsua_call_info call_info;
2819 pjsua_acc_info acc_info;
2820
2821
2822 /* If user specifies URI to call, then call the URI */
2823 if (uri_to_call->slen) {
2824 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2825 }
2826
2827 keystroke_help();
2828
2829 for (;;) {
2830
2831 printf(">>> ");
2832 fflush(stdout);
2833
Benny Prijono990042e2007-01-21 19:36:00 +00002834 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2835 /*
2836 * Be friendly to users who redirect commands into
2837 * program, when file ends, resume with kbd.
2838 * If exit is desired end script with q for quit
2839 */
2840 /* Reopen stdin/stdout/stderr to /dev/console */
2841#if defined(PJ_WIN32) && PJ_WIN32!=0
2842 if (freopen ("CONIN$", "r", stdin) == NULL) {
2843#else
2844 if (1) {
2845#endif
2846 puts("Cannot switch back to console from file redirection");
2847 menuin[0] = 'q';
2848 menuin[1] = '\0';
2849 } else {
2850 puts("Switched back to console from file redirection");
2851 continue;
2852 }
2853 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002854
Benny Prijonoebc32c32008-06-12 13:30:39 +00002855 if (cmd_echo) {
2856 printf("%s", menuin);
2857 }
2858
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002859 switch (menuin[0]) {
2860
2861 case 'm':
2862 /* Make call! : */
2863 printf("(You currently have %d calls)\n",
2864 pjsua_call_get_count());
2865
2866 uri = NULL;
2867 ui_input_url("Make call", buf, sizeof(buf), &result);
2868 if (result.nb_result != NO_NB) {
2869
2870 if (result.nb_result == -1 || result.nb_result == 0) {
2871 puts("You can't do that with make call!");
2872 continue;
2873 } else {
2874 pjsua_buddy_info binfo;
2875 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2876 uri = binfo.uri.ptr;
2877 }
2878
2879 } else if (result.uri_result) {
2880 uri = result.uri_result;
2881 }
2882
2883 tmp = pj_str(uri);
2884 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2885 break;
2886
2887 case 'M':
2888 /* Make multiple calls! : */
2889 printf("(You currently have %d calls)\n",
2890 pjsua_call_get_count());
2891
2892 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2893 continue;
2894
2895 count = my_atoi(menuin);
2896 if (count < 1)
2897 continue;
2898
2899 ui_input_url("Make call", buf, sizeof(buf), &result);
2900 if (result.nb_result != NO_NB) {
2901 pjsua_buddy_info binfo;
2902 if (result.nb_result == -1 || result.nb_result == 0) {
2903 puts("You can't do that with make call!");
2904 continue;
2905 }
2906 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2907 uri = binfo.uri.ptr;
2908 } else {
2909 uri = result.uri_result;
2910 }
2911
2912 for (i=0; i<my_atoi(menuin); ++i) {
2913 pj_status_t status;
2914
2915 tmp = pj_str(uri);
2916 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2917 NULL, NULL);
2918 if (status != PJ_SUCCESS)
2919 break;
2920 }
2921 break;
2922
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002923 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00002924 i = pjsua_detect_nat_type();
2925 if (i != PJ_SUCCESS)
2926 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002927 break;
2928
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002929 case 'i':
2930 /* Send instant messaeg */
2931
2932 /* i is for call index to send message, if any */
2933 i = -1;
2934
2935 /* Make compiler happy. */
2936 uri = NULL;
2937
2938 /* Input destination. */
2939 ui_input_url("Send IM to", buf, sizeof(buf), &result);
2940 if (result.nb_result != NO_NB) {
2941
2942 if (result.nb_result == -1) {
2943 puts("You can't send broadcast IM like that!");
2944 continue;
2945
2946 } else if (result.nb_result == 0) {
2947
2948 i = current_call;
2949
2950 } else {
2951 pjsua_buddy_info binfo;
2952 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2953 uri = binfo.uri.ptr;
2954 }
2955
2956 } else if (result.uri_result) {
2957 uri = result.uri_result;
2958 }
2959
2960
2961 /* Send typing indication. */
2962 if (i != -1)
2963 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
2964 else {
2965 pj_str_t tmp_uri = pj_str(uri);
2966 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
2967 }
2968
2969 /* Input the IM . */
2970 if (!simple_input("Message", text, sizeof(text))) {
2971 /*
2972 * Cancelled.
2973 * Send typing notification too, saying we're not typing.
2974 */
2975 if (i != -1)
2976 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
2977 else {
2978 pj_str_t tmp_uri = pj_str(uri);
2979 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
2980 }
2981 continue;
2982 }
2983
2984 tmp = pj_str(text);
2985
2986 /* Send the IM */
2987 if (i != -1)
2988 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
2989 else {
2990 pj_str_t tmp_uri = pj_str(uri);
2991 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
2992 }
2993
2994 break;
2995
2996 case 'a':
2997
2998 if (current_call != -1) {
2999 pjsua_call_get_info(current_call, &call_info);
3000 } else {
3001 /* Make compiler happy */
3002 call_info.role = PJSIP_ROLE_UAC;
3003 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3004 }
3005
3006 if (current_call == -1 ||
3007 call_info.role != PJSIP_ROLE_UAS ||
3008 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3009 {
3010 puts("No pending incoming call");
3011 fflush(stdout);
3012 continue;
3013
3014 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003015 int st_code;
3016 char contact[120];
3017 pj_str_t hname = { "Contact", 7 };
3018 pj_str_t hvalue;
3019 pjsip_generic_string_hdr hcontact;
3020 pjsua_msg_data msg_data;
3021
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003022 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3023 continue;
3024
Benny Prijono20d36722007-02-22 14:52:24 +00003025 st_code = my_atoi(buf);
3026 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003027 continue;
3028
Benny Prijono20d36722007-02-22 14:52:24 +00003029 pjsua_msg_data_init(&msg_data);
3030
3031 if (st_code/100 == 3) {
3032 if (!simple_input("Enter URL to be put in Contact",
3033 contact, sizeof(contact)))
3034 continue;
3035 hvalue = pj_str(contact);
3036 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3037
3038 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3039 }
3040
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003041 /*
3042 * Must check again!
3043 * Call may have been disconnected while we're waiting for
3044 * keyboard input.
3045 */
3046 if (current_call == -1) {
3047 puts("Call has been disconnected");
3048 fflush(stdout);
3049 continue;
3050 }
3051
Benny Prijono20d36722007-02-22 14:52:24 +00003052 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003053 }
3054
3055 break;
3056
3057
3058 case 'h':
3059
3060 if (current_call == -1) {
3061 puts("No current call");
3062 fflush(stdout);
3063 continue;
3064
3065 } else if (menuin[1] == 'a') {
3066
3067 /* Hangup all calls */
3068 pjsua_call_hangup_all();
3069
3070 } else {
3071
3072 /* Hangup current calls */
3073 pjsua_call_hangup(current_call, 0, NULL, NULL);
3074 }
3075 break;
3076
3077 case ']':
3078 case '[':
3079 /*
3080 * Cycle next/prev dialog.
3081 */
3082 if (menuin[0] == ']') {
3083 find_next_call();
3084
3085 } else {
3086 find_prev_call();
3087 }
3088
3089 if (current_call != -1) {
3090
3091 pjsua_call_get_info(current_call, &call_info);
3092 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3093 (int)call_info.remote_info.slen,
3094 call_info.remote_info.ptr));
3095
3096 } else {
3097 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3098 }
3099 break;
3100
3101
3102 case '>':
3103 case '<':
3104 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3105 break;
3106
3107 i = my_atoi(buf);
3108 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003109 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003110 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3111 } else {
3112 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3113 }
3114 break;
3115
3116
3117 case '+':
3118 if (menuin[1] == 'b') {
3119
3120 pjsua_buddy_config buddy_cfg;
3121 pjsua_buddy_id buddy_id;
3122 pj_status_t status;
3123
3124 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3125 break;
3126
3127 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3128 printf("Invalid SIP URI '%s'\n", buf);
3129 break;
3130 }
3131
Benny Prijonoac623b32006-07-03 15:19:31 +00003132 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003133
3134 buddy_cfg.uri = pj_str(buf);
3135 buddy_cfg.subscribe = PJ_TRUE;
3136
3137 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3138 if (status == PJ_SUCCESS) {
3139 printf("New buddy '%s' added at index %d\n",
3140 buf, buddy_id+1);
3141 }
3142
3143 } else if (menuin[1] == 'a') {
3144
Benny Prijonofb2b3652007-06-28 07:15:03 +00003145 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3146 pjsua_acc_config acc_cfg;
3147 pj_status_t status;
3148
3149 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3150 break;
3151 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3152 break;
3153 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3154 break;
3155 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3156 break;
3157 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3158 break;
3159
3160 pjsua_acc_config_default(&acc_cfg);
3161 acc_cfg.id = pj_str(id);
3162 acc_cfg.reg_uri = pj_str(registrar);
3163 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003164 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003165 acc_cfg.cred_info[0].realm = pj_str(realm);
3166 acc_cfg.cred_info[0].username = pj_str(uname);
3167 acc_cfg.cred_info[0].data_type = 0;
3168 acc_cfg.cred_info[0].data = pj_str(passwd);
3169
3170 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3171 if (status != PJ_SUCCESS) {
3172 pjsua_perror(THIS_FILE, "Error adding new account", status);
3173 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003174
3175 } else {
3176 printf("Invalid input %s\n", menuin);
3177 }
3178 break;
3179
3180 case '-':
3181 if (menuin[1] == 'b') {
3182 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3183 break;
3184
3185 i = my_atoi(buf) - 1;
3186
3187 if (!pjsua_buddy_is_valid(i)) {
3188 printf("Invalid buddy id %d\n", i);
3189 } else {
3190 pjsua_buddy_del(i);
3191 printf("Buddy %d deleted\n", i);
3192 }
3193
3194 } else if (menuin[1] == 'a') {
3195
3196 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3197 break;
3198
3199 i = my_atoi(buf);
3200
3201 if (!pjsua_acc_is_valid(i)) {
3202 printf("Invalid account id %d\n", i);
3203 } else {
3204 pjsua_acc_del(i);
3205 printf("Account %d deleted\n", i);
3206 }
3207
3208 } else {
3209 printf("Invalid input %s\n", menuin);
3210 }
3211 break;
3212
3213 case 'H':
3214 /*
3215 * Hold call.
3216 */
3217 if (current_call != -1) {
3218
3219 pjsua_call_set_hold(current_call, NULL);
3220
3221 } else {
3222 PJ_LOG(3,(THIS_FILE, "No current call"));
3223 }
3224 break;
3225
3226 case 'v':
3227 /*
3228 * Send re-INVITE (to release hold, etc).
3229 */
3230 if (current_call != -1) {
3231
3232 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3233
3234 } else {
3235 PJ_LOG(3,(THIS_FILE, "No current call"));
3236 }
3237 break;
3238
Benny Prijonoc08682e2007-10-04 06:17:58 +00003239 case 'U':
3240 /*
3241 * Send UPDATE
3242 */
3243 if (current_call != -1) {
3244
3245 pjsua_call_update(current_call, 0, NULL);
3246
3247 } else {
3248 PJ_LOG(3,(THIS_FILE, "No current call"));
3249 }
3250 break;
3251
3252 case 'C':
3253 if (menuin[1] == 'p') {
3254 manage_codec_prio();
3255 }
3256 break;
3257
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003258 case 'x':
3259 /*
3260 * Transfer call.
3261 */
3262 if (current_call == -1) {
3263
3264 PJ_LOG(3,(THIS_FILE, "No current call"));
3265
3266 } else {
3267 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003268 pjsua_msg_data msg_data;
3269 pjsip_generic_string_hdr refer_sub;
3270 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3271 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003272 pjsua_call_info ci;
3273
3274 pjsua_call_get_info(current_call, &ci);
3275 printf("Transfering current call [%d] %.*s\n",
3276 current_call,
3277 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003278
3279 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3280
3281 /* Check if call is still there. */
3282
3283 if (call != current_call) {
3284 puts("Call has been disconnected");
3285 continue;
3286 }
3287
Benny Prijonod524e822006-09-22 12:48:18 +00003288 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003289 if (app_config.no_refersub) {
3290 /* Add Refer-Sub: false in outgoing REFER request */
3291 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3292 &STR_FALSE);
3293 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3294 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003295 if (result.nb_result != NO_NB) {
3296 if (result.nb_result == -1 || result.nb_result == 0)
3297 puts("You can't do that with transfer call!");
3298 else {
3299 pjsua_buddy_info binfo;
3300 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003301 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003302 }
3303
3304 } else if (result.uri_result) {
3305 pj_str_t tmp;
3306 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003307 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003308 }
3309 }
3310 break;
3311
Benny Prijonof7b1c392006-11-11 16:46:34 +00003312 case 'X':
3313 /*
3314 * Transfer call with replaces.
3315 */
3316 if (current_call == -1) {
3317
3318 PJ_LOG(3,(THIS_FILE, "No current call"));
3319
3320 } else {
3321 int call = current_call;
3322 int dst_call;
3323 pjsua_msg_data msg_data;
3324 pjsip_generic_string_hdr refer_sub;
3325 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3326 pj_str_t STR_FALSE = { "false", 5 };
3327 pjsua_call_id ids[PJSUA_MAX_CALLS];
3328 pjsua_call_info ci;
3329 unsigned i, count;
3330
3331 count = PJ_ARRAY_SIZE(ids);
3332 pjsua_enum_calls(ids, &count);
3333
3334 if (count <= 1) {
3335 puts("There are no other calls");
3336 continue;
3337 }
3338
3339 pjsua_call_get_info(current_call, &ci);
3340 printf("Transfer call [%d] %.*s to one of the following:\n",
3341 current_call,
3342 (int)ci.remote_info.slen, ci.remote_info.ptr);
3343
3344 for (i=0; i<count; ++i) {
3345 pjsua_call_info call_info;
3346
3347 if (ids[i] == call)
3348 continue;
3349
3350 pjsua_call_get_info(ids[i], &call_info);
3351 printf("%d %.*s [%.*s]\n",
3352 ids[i],
3353 (int)call_info.remote_info.slen,
3354 call_info.remote_info.ptr,
3355 (int)call_info.state_text.slen,
3356 call_info.state_text.ptr);
3357 }
3358
3359 if (!simple_input("Enter call number to be replaced",
3360 buf, sizeof(buf)))
3361 continue;
3362
3363 dst_call = my_atoi(buf);
3364
3365 /* Check if call is still there. */
3366
3367 if (call != current_call) {
3368 puts("Call has been disconnected");
3369 continue;
3370 }
3371
3372 /* Check that destination call is valid. */
3373 if (dst_call == call) {
3374 puts("Destination call number must not be the same "
3375 "as the call being transfered");
3376 continue;
3377 }
3378 if (dst_call >= PJSUA_MAX_CALLS) {
3379 puts("Invalid destination call number");
3380 continue;
3381 }
3382 if (!pjsua_call_is_active(dst_call)) {
3383 puts("Invalid destination call number");
3384 continue;
3385 }
3386
3387 pjsua_msg_data_init(&msg_data);
3388 if (app_config.no_refersub) {
3389 /* Add Refer-Sub: false in outgoing REFER request */
3390 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3391 &STR_FALSE);
3392 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3393 }
3394
3395 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3396 }
3397 break;
3398
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003399 case '#':
3400 /*
3401 * Send DTMF strings.
3402 */
3403 if (current_call == -1) {
3404
3405 PJ_LOG(3,(THIS_FILE, "No current call"));
3406
3407 } else if (!pjsua_call_has_media(current_call)) {
3408
3409 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3410
3411 } else {
3412 pj_str_t digits;
3413 int call = current_call;
3414 pj_status_t status;
3415
3416 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3417 sizeof(buf)))
3418 {
3419 break;
3420 }
3421
3422 if (call != current_call) {
3423 puts("Call has been disconnected");
3424 continue;
3425 }
3426
3427 digits = pj_str(buf);
3428 status = pjsua_call_dial_dtmf(current_call, &digits);
3429 if (status != PJ_SUCCESS) {
3430 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3431 } else {
3432 puts("DTMF digits enqueued for transmission");
3433 }
3434 }
3435 break;
3436
Benny Prijonofeb69f42007-10-05 09:12:26 +00003437 case '*':
3438 /* Send DTMF with INFO */
3439 if (current_call == -1) {
3440
3441 PJ_LOG(3,(THIS_FILE, "No current call"));
3442
3443 } else {
3444 const pj_str_t SIP_INFO = pj_str("INFO");
3445 pj_str_t digits;
3446 int call = current_call;
3447 int i;
3448 pj_status_t status;
3449
3450 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3451 sizeof(buf)))
3452 {
3453 break;
3454 }
3455
3456 if (call != current_call) {
3457 puts("Call has been disconnected");
3458 continue;
3459 }
3460
3461 digits = pj_str(buf);
3462 for (i=0; i<digits.slen; ++i) {
3463 pjsua_msg_data msg_data;
3464 char body[80];
3465
3466 pjsua_msg_data_init(&msg_data);
3467 msg_data.content_type = pj_str("application/dtmf-relay");
3468
3469 pj_ansi_snprintf(body, sizeof(body),
3470 "Signal=%c\r\n"
3471 "Duration=160",
3472 buf[i]);
3473 msg_data.msg_body = pj_str(body);
3474
3475 status = pjsua_call_send_request(current_call, &SIP_INFO,
3476 &msg_data);
3477 if (status != PJ_SUCCESS) {
3478 break;
3479 }
3480 }
3481 }
3482 break;
3483
Benny Prijono56315612006-07-18 14:39:40 +00003484 case 'S':
3485 /*
3486 * Send arbitrary request
3487 */
3488 if (pjsua_acc_get_count() == 0) {
3489 puts("Sorry, need at least one account configured");
3490 break;
3491 }
3492
3493 puts("Send arbitrary request to remote host");
3494
3495 /* Input METHOD */
3496 if (!simple_input("Request method:",text,sizeof(text)))
3497 break;
3498
3499 /* Input destination URI */
3500 uri = NULL;
3501 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3502 if (result.nb_result != NO_NB) {
3503
3504 if (result.nb_result == -1 || result.nb_result == 0) {
3505 puts("Sorry you can't do that!");
3506 continue;
3507 } else {
3508 pjsua_buddy_info binfo;
3509 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3510 uri = binfo.uri.ptr;
3511 }
3512
3513 } else if (result.uri_result) {
3514 uri = result.uri_result;
3515 }
3516
3517 tmp = pj_str(uri);
3518
3519 send_request(text, &tmp);
3520 break;
3521
Benny Prijonoebc32c32008-06-12 13:30:39 +00003522 case 'e':
3523 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3524 pj_str_t tmp;
3525
3526 tmp.ptr = menuin+5;
3527 tmp.slen = pj_ansi_strlen(menuin)-6;
3528
3529 if (tmp.slen < 1) {
3530 puts("Usage: echo [0|1]");
3531 break;
3532 }
3533
3534 cmd_echo = pj_strtoul(&tmp);
3535 }
3536 break;
3537
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003538 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003539 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3540 pj_str_t tmp;
3541 int delay;
3542
3543 tmp.ptr = menuin+6;
3544 tmp.slen = pj_ansi_strlen(menuin)-7;
3545
3546 if (tmp.slen < 1) {
3547 puts("Usage: sleep MSEC");
3548 break;
3549 }
3550
3551 delay = pj_strtoul(&tmp);
3552 if (delay < 0) delay = 0;
3553 pj_thread_sleep(delay);
3554 break;
3555 }
3556 /* Continue below */
3557
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003558 case 'u':
3559 /*
3560 * Subscribe/unsubscribe presence.
3561 */
3562 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3563 if (result.nb_result != NO_NB) {
3564 if (result.nb_result == -1) {
3565 int i, count;
3566 count = pjsua_get_buddy_count();
3567 for (i=0; i<count; ++i)
3568 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3569 } else if (result.nb_result == 0) {
3570 puts("Sorry, can only subscribe to buddy's presence, "
3571 "not from existing call");
3572 } else {
3573 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3574 }
3575
3576 } else if (result.uri_result) {
3577 puts("Sorry, can only subscribe to buddy's presence, "
3578 "not arbitrary URL (for now)");
3579 }
3580
3581 break;
3582
3583 case 'r':
3584 switch (menuin[1]) {
3585 case 'r':
3586 /*
3587 * Re-Register.
3588 */
3589 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3590 break;
3591 case 'u':
3592 /*
3593 * Unregister
3594 */
3595 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3596 break;
3597 }
3598 break;
3599
3600 case 't':
3601 pjsua_acc_get_info(current_acc, &acc_info);
3602 acc_info.online_status = !acc_info.online_status;
3603 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3604 printf("Setting %s online status to %s\n",
3605 acc_info.acc_uri.ptr,
3606 (acc_info.online_status?"online":"offline"));
3607 break;
3608
Benny Prijono4461c7d2007-08-25 13:36:15 +00003609 case 'T':
3610 change_online_status();
3611 break;
3612
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003613 case 'c':
3614 switch (menuin[1]) {
3615 case 'l':
3616 conf_list();
3617 break;
3618 case 'c':
3619 case 'd':
3620 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003621 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003622 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003623 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003624 const char *src_title, *dst_title;
3625
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003626 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003627
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003628 if (cnt != 3) {
3629 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003630
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003631 src_title = (menuin[1]=='c'?
3632 "Connect src port #":
3633 "Disconnect src port #");
3634 dst_title = (menuin[1]=='c'?
3635 "To dst port #":
3636 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003637
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003638 if (!simple_input(src_title, src_port, sizeof(src_port)))
3639 break;
3640
3641 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3642 break;
3643 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003644
3645 if (menuin[1]=='c') {
3646 status = pjsua_conf_connect(my_atoi(src_port),
3647 my_atoi(dst_port));
3648 } else {
3649 status = pjsua_conf_disconnect(my_atoi(src_port),
3650 my_atoi(dst_port));
3651 }
3652 if (status == PJ_SUCCESS) {
3653 puts("Success");
3654 } else {
3655 puts("ERROR!!");
3656 }
3657 }
3658 break;
3659 }
3660 break;
3661
Benny Prijono6dd967c2006-12-26 02:27:14 +00003662 case 'V':
3663 /* Adjust audio volume */
3664 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3665 if (simple_input(buf,text,sizeof(text))) {
3666 char *err;
3667 app_config.mic_level = (float)strtod(text, &err);
3668 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3669 }
3670 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3671 app_config.speaker_level);
3672 if (simple_input(buf,text,sizeof(text))) {
3673 char *err;
3674 app_config.speaker_level = (float)strtod(text, &err);
3675 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3676 }
3677
3678 break;
3679
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003680 case 'd':
3681 if (menuin[1] == 'c') {
3682 char settings[2000];
3683 int len;
3684
3685 len = write_settings(&app_config, settings, sizeof(settings));
3686 if (len < 1)
3687 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3688 else
3689 PJ_LOG(3,(THIS_FILE,
3690 "Dumping configuration (%d bytes):\n%s\n",
3691 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003692
3693 } else if (menuin[1] == 'q') {
3694
3695 if (current_call != PJSUA_INVALID_ID) {
Benny Prijono6eddd872008-03-21 13:46:08 +00003696 pjsua_call_dump(current_call, PJ_TRUE, some_buf,
3697 sizeof(some_buf), " ");
3698 PJ_LOG(3,(THIS_FILE, "\n%s", some_buf));
Benny Prijono819506c2006-06-22 22:29:51 +00003699 } else {
3700 PJ_LOG(3,(THIS_FILE, "No current call"));
3701 }
3702
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003703 } else {
3704 app_dump(menuin[1]=='d');
3705 }
3706 break;
3707
3708
3709 case 'f':
3710 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3711 char settings[2000];
3712 int len;
3713
3714 len = write_settings(&app_config, settings, sizeof(settings));
3715 if (len < 1)
3716 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3717 else {
3718 pj_oshandle_t fd;
3719 pj_status_t status;
3720
3721 status = pj_file_open(app_config.pool, buf,
3722 PJ_O_WRONLY, &fd);
3723 if (status != PJ_SUCCESS) {
3724 pjsua_perror(THIS_FILE, "Unable to open file", status);
3725 } else {
3726 pj_ssize_t size = len;
3727 pj_file_write(fd, settings, &size);
3728 pj_file_close(fd);
3729
3730 printf("Settings successfully written to '%s'\n", buf);
3731 }
3732 }
3733
3734 }
3735 break;
3736
3737
3738 case 'q':
3739 goto on_exit;
3740
3741
3742 default:
3743 if (menuin[0] != '\n' && menuin[0] != '\r') {
3744 printf("Invalid input %s", menuin);
3745 }
3746 keystroke_help();
3747 break;
3748 }
3749 }
3750
3751on_exit:
3752 ;
3753}
3754
3755
3756/*****************************************************************************
3757 * Public API
3758 */
3759
3760pj_status_t app_init(int argc, char *argv[])
3761{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003762 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003763 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003764 unsigned i;
3765 pj_status_t status;
3766
3767 /* Create pjsua */
3768 status = pjsua_create();
3769 if (status != PJ_SUCCESS)
3770 return status;
3771
3772 /* Create pool for application */
Benny Prijonod5696da2007-07-17 16:25:45 +00003773 app_config.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003774
3775 /* Initialize default config */
3776 default_config(&app_config);
3777
3778 /* Parse the arguments */
3779 status = parse_args(argc, argv, &app_config, &uri_arg);
3780 if (status != PJ_SUCCESS)
3781 return status;
3782
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003783 /* Initialize application callbacks */
3784 app_config.cfg.cb.on_call_state = &on_call_state;
3785 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3786 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003787 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003788 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003789 app_config.cfg.cb.on_reg_state = &on_reg_state;
3790 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3791 app_config.cfg.cb.on_pager = &on_pager;
3792 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003793 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003794 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003795 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003796
3797 /* Initialize pjsua */
3798 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3799 &app_config.media_cfg);
3800 if (status != PJ_SUCCESS)
3801 return status;
3802
Benny Prijonoe909eac2006-07-27 22:04:56 +00003803#ifdef STEREO_DEMO
3804 stereo_demo();
3805#endif
3806
Benny Prijono804ff0a2006-09-14 11:17:48 +00003807 /* Initialize calls data */
3808 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3809 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3810 app_config.call_data[i].timer.cb = &call_timeout_callback;
3811 }
3812
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003813 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003814 for (i=0; i<app_config.wav_count; ++i) {
3815 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003816 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00003817
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003818 if (app_config.auto_play_hangup)
3819 play_options |= PJMEDIA_FILE_NO_LOOP;
3820
3821 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00003822 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003823 if (status != PJ_SUCCESS)
3824 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003825
Benny Prijono72c04d22007-02-17 22:20:58 +00003826 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003827 app_config.wav_id = wav_id;
3828 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003829 if (app_config.auto_play_hangup) {
3830 pjmedia_port *port;
3831
3832 pjsua_player_get_port(app_config.wav_id, &port);
3833 status = pjmedia_wav_player_set_eof_cb(port, NULL,
3834 &on_playfile_done);
3835 if (status != PJ_SUCCESS)
3836 goto on_error;
3837
3838 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
3839 &hangup_timeout_callback);
3840 }
Benny Prijono32e4f492007-01-24 00:44:26 +00003841 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003842 }
3843
Benny Prijono4af234b2007-01-24 02:02:09 +00003844 /* Optionally registers tone players */
3845 for (i=0; i<app_config.tone_count; ++i) {
3846 pjmedia_port *tport;
3847 char name[80];
3848 pj_str_t label;
3849 pj_status_t status;
3850
3851 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3852 app_config.tones[i].freq1,
3853 app_config.tones[i].freq2);
3854 label = pj_str(name);
3855 status = pjmedia_tonegen_create2(app_config.pool, &label,
3856 8000, 1, 160, 16,
3857 PJMEDIA_TONEGEN_LOOP, &tport);
3858 if (status != PJ_SUCCESS) {
3859 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3860 goto on_error;
3861 }
3862
3863 status = pjsua_conf_add_port(app_config.pool, tport,
3864 &app_config.tone_slots[i]);
3865 pj_assert(status == PJ_SUCCESS);
3866
3867 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3868 pj_assert(status == PJ_SUCCESS);
3869 }
3870
Benny Prijono1ebd6142006-10-19 15:48:02 +00003871 /* Optionally create recorder file, if any. */
3872 if (app_config.rec_file.slen) {
3873 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3874 &app_config.rec_id);
3875 if (status != PJ_SUCCESS)
3876 goto on_error;
3877
3878 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3879 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003880
Benny Prijonoe347cb02007-02-14 14:36:13 +00003881 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3882
Benny Prijono91d20f42008-06-14 19:42:37 +00003883 /* Create ringback tones */
3884 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00003885 unsigned i, samples_per_frame;
3886 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00003887 pj_str_t name;
3888
Benny Prijono4f966892008-06-14 22:43:56 +00003889 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
3890 app_config.media_cfg.clock_rate *
3891 app_config.media_cfg.channel_count / 1000;
3892
Benny Prijono91d20f42008-06-14 19:42:37 +00003893 /* Ringback tone (call is ringing) */
3894 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00003895 status = pjmedia_tonegen_create2(app_config.pool, &name,
3896 app_config.media_cfg.clock_rate,
3897 app_config.media_cfg.channel_count,
3898 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003899 16, PJMEDIA_TONEGEN_LOOP,
3900 &app_config.ringback_port);
3901 if (status != PJ_SUCCESS)
3902 goto on_error;
3903
Benny Prijonoe28cec82008-06-14 20:40:53 +00003904 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00003905 for (i=0; i<RINGBACK_CNT; ++i) {
3906 tone[i].freq1 = RINGBACK_FREQ1;
3907 tone[i].freq2 = RINGBACK_FREQ2;
3908 tone[i].on_msec = RINGBACK_ON;
3909 tone[i].off_msec = RINGBACK_OFF;
3910 }
3911 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003912
Benny Prijono4f966892008-06-14 22:43:56 +00003913 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00003914 PJMEDIA_TONEGEN_LOOP);
3915
3916
Benny Prijono91d20f42008-06-14 19:42:37 +00003917 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
3918 &app_config.ringback_slot);
3919 if (status != PJ_SUCCESS)
3920 goto on_error;
3921
3922 /* Ring (to alert incoming call) */
3923 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00003924 status = pjmedia_tonegen_create2(app_config.pool, &name,
3925 app_config.media_cfg.clock_rate,
3926 app_config.media_cfg.channel_count,
3927 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003928 16, PJMEDIA_TONEGEN_LOOP,
3929 &app_config.ring_port);
3930 if (status != PJ_SUCCESS)
3931 goto on_error;
3932
Benny Prijono4f966892008-06-14 22:43:56 +00003933 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00003934 tone[i].freq1 = RING_FREQ1;
3935 tone[i].freq2 = RING_FREQ2;
3936 tone[i].on_msec = RING_ON;
3937 tone[i].off_msec = RING_OFF;
3938 }
Benny Prijono4f966892008-06-14 22:43:56 +00003939 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003940
Benny Prijono4f966892008-06-14 22:43:56 +00003941 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00003942 tone, PJMEDIA_TONEGEN_LOOP);
3943
Benny Prijono91d20f42008-06-14 19:42:37 +00003944 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
3945 &app_config.ring_slot);
3946 if (status != PJ_SUCCESS)
3947 goto on_error;
3948
3949 }
3950
Benny Prijono87ef89a2007-01-14 00:39:45 +00003951 /* Add UDP transport unless it's disabled. */
3952 if (!app_config.no_udp) {
3953 pjsua_acc_id aid;
3954
3955 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
3956 &app_config.udp_cfg,
3957 &transport_id);
3958 if (status != PJ_SUCCESS)
3959 goto on_error;
3960
3961 /* Add local account */
3962 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
3963 //pjsua_acc_set_transport(aid, transport_id);
3964 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00003965
3966 if (app_config.udp_cfg.port == 0) {
3967 pjsua_transport_info ti;
3968 pj_sockaddr_in *a;
3969
3970 pjsua_transport_get_info(transport_id, &ti);
3971 a = (pj_sockaddr_in*)&ti.local_addr;
3972
3973 tcp_cfg.port = pj_ntohs(a->sin_port);
3974 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00003975 }
3976
Benny Prijonoe93e2872006-06-28 16:46:49 +00003977 /* Add TCP transport unless it's disabled */
3978 if (!app_config.no_tcp) {
3979 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00003980 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00003981 &transport_id);
3982 if (status != PJ_SUCCESS)
3983 goto on_error;
3984
3985 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00003986 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00003987 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3988
3989 }
3990
Benny Prijonoe93e2872006-06-28 16:46:49 +00003991
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003992#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
3993 /* Add TLS transport when application wants one */
3994 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00003995
3996 pjsua_acc_id acc_id;
3997
3998 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00003999 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004000 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004001 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004002 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004003 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004004 if (status != PJ_SUCCESS)
4005 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004006
4007 /* Add local account */
4008 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4009 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004010 }
4011#endif
4012
Benny Prijonoe93e2872006-06-28 16:46:49 +00004013 if (transport_id == -1) {
4014 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
4015 status = -1;
4016 goto on_error;
4017 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004018
4019
4020 /* Add accounts */
4021 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004022 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004023 if (status != PJ_SUCCESS)
4024 goto on_error;
4025 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4026 }
4027
4028 /* Add buddies */
4029 for (i=0; i<app_config.buddy_cnt; ++i) {
4030 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4031 if (status != PJ_SUCCESS)
4032 goto on_error;
4033 }
4034
4035 /* Optionally set codec orders */
4036 for (i=0; i<app_config.codec_cnt; ++i) {
4037 pjsua_codec_set_priority(&app_config.codec_arg[i],
4038 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4039 }
4040
Benny Prijonofce28542007-12-09 15:41:10 +00004041 /* Optionally disable some codec */
4042 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4043 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4044 }
4045
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004046 /* Add RTP transports */
4047 status = pjsua_media_transports_create(&app_config.rtp_cfg);
4048 if (status != PJ_SUCCESS)
4049 goto on_error;
4050
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004051 /* Set sound device latency */
4052 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4053
Benny Prijono22a300a2006-06-14 20:04:55 +00004054 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004055#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004056 if (app_config.null_audio) {
4057 status = pjsua_set_null_snd_dev();
4058 if (status != PJ_SUCCESS)
4059 return status;
4060 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004061#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004062
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004063 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4064 app_config.playback_dev != PJSUA_INVALID_ID)
4065 {
4066 status = pjsua_set_snd_dev(app_config.capture_dev,
4067 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004068 if (status != PJ_SUCCESS)
4069 goto on_error;
4070 }
4071
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004072 return PJ_SUCCESS;
4073
4074on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004075 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004076 return status;
4077}
4078
4079
Benny Prijonoebc32c32008-06-12 13:30:39 +00004080static int stdout_refresh_proc(void *arg)
4081{
4082 PJ_UNUSED_ARG(arg);
4083
4084 /* Set thread to lowest priority so that it doesn't clobber
4085 * stdout output
4086 */
4087 pj_thread_set_prio(pj_thread_this(),
4088 pj_thread_get_prio_min(pj_thread_this()));
4089
4090 while (!stdout_refresh_quit) {
4091 pj_thread_sleep(stdout_refresh * 1000);
4092 puts(stdout_refresh_text);
4093 fflush(stdout);
4094 }
4095
4096 return 0;
4097}
4098
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004099pj_status_t app_main(void)
4100{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004101 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004102 pj_status_t status;
4103
4104 /* Start pjsua */
4105 status = pjsua_start();
4106 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004107 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004108 return status;
4109 }
4110
Benny Prijonoebc32c32008-06-12 13:30:39 +00004111 /* Start console refresh thread */
4112 if (stdout_refresh > 0) {
4113 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4114 NULL, 0, 0, &stdout_refresh_thread);
4115 }
4116
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004117 console_app_main(&uri_arg);
4118
Benny Prijonoebc32c32008-06-12 13:30:39 +00004119 if (stdout_refresh_thread) {
4120 stdout_refresh_quit = PJ_TRUE;
4121 pj_thread_join(stdout_refresh_thread);
4122 pj_thread_destroy(stdout_refresh_thread);
4123 }
4124
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004125 return PJ_SUCCESS;
4126}
4127
4128pj_status_t app_destroy(void)
4129{
Benny Prijonof762ee72006-12-01 11:14:37 +00004130 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004131 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004132
Benny Prijonoe909eac2006-07-27 22:04:56 +00004133#ifdef STEREO_DEMO
4134 if (app_config.snd) {
4135 pjmedia_snd_port_destroy(app_config.snd);
4136 app_config.snd = NULL;
4137 }
4138#endif
4139
Benny Prijono91d20f42008-06-14 19:42:37 +00004140 /* Close ringback port */
4141 if (app_config.ringback_port &&
4142 app_config.ringback_slot != PJSUA_INVALID_ID)
4143 {
4144 pjsua_conf_remove_port(app_config.ringback_slot);
4145 app_config.ringback_slot = PJSUA_INVALID_ID;
4146 pjmedia_port_destroy(app_config.ringback_port);
4147 app_config.ringback_port = NULL;
4148 }
4149
4150 /* Close ring port */
4151 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4152 pjsua_conf_remove_port(app_config.ring_slot);
4153 app_config.ring_slot = PJSUA_INVALID_ID;
4154 pjmedia_port_destroy(app_config.ring_port);
4155 app_config.ring_port = NULL;
4156 }
4157
Benny Prijono4af234b2007-01-24 02:02:09 +00004158 /* Close tone generators */
4159 for (i=0; i<app_config.tone_count; ++i) {
4160 pjsua_conf_remove_port(app_config.tone_slots[i]);
4161 }
4162
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004163 if (app_config.pool) {
4164 pj_pool_release(app_config.pool);
4165 app_config.pool = NULL;
4166 }
4167
Benny Prijonof762ee72006-12-01 11:14:37 +00004168 status = pjsua_destroy();
4169
4170 pj_bzero(&app_config, sizeof(app_config));
4171
4172 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004173}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004174
4175
4176#ifdef STEREO_DEMO
4177static void stereo_demo()
4178{
4179 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004180 pj_status_t status;
4181
4182 /* Disable existing sound device */
4183 conf = pjsua_set_no_snd_dev();
4184
Benny Prijonoe909eac2006-07-27 22:04:56 +00004185 /* Create stereo-mono splitter/combiner */
4186 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004187 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004188 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004189 2 * conf->info.samples_per_frame,
4190 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004191 0 /* options */,
4192 &splitter);
4193 pj_assert(status == PJ_SUCCESS);
4194
4195 /* Connect channel0 (left channel?) to conference port slot0 */
4196 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4197 0 /*options*/,
4198 conf);
4199 pj_assert(status == PJ_SUCCESS);
4200
4201 /* Create reverse channel for channel1 (right channel?)... */
4202 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4203 splitter,
4204 1 /* ch1 */,
4205 0 /* options */,
4206 &ch1);
4207 pj_assert(status == PJ_SUCCESS);
4208
4209 /* .. and register it to conference bridge (it would be slot1
4210 * if there's no other devices connected to the bridge)
4211 */
4212 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4213 pj_assert(status == PJ_SUCCESS);
4214
4215 /* Create sound device */
4216 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004217 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004218 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004219 2 * conf->info.samples_per_frame,
4220 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004221 0, &app_config.snd);
4222 pj_assert(status == PJ_SUCCESS);
4223
4224
4225 /* Connect the splitter to the sound device */
4226 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4227 pj_assert(status == PJ_SUCCESS);
4228
4229}
4230#endif
4231