blob: b2b2e76ca630e7522e84f5a210131dc7854c0fb5 [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
Nanang Izzuddin660eee82008-07-17 14:54:03 +0000130static char some_buf[1024 * 3];
Benny Prijono6eddd872008-03-21 13:46:08 +0000131
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)");
Benny Prijonoe10db842008-07-01 15:31:59 +0000209 puts (" --tls-srv-name Specify TLS server name for multi-hosting server (optional)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000210
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000211 puts ("");
212 puts ("Media Options:");
213 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000214 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000215 puts (" --clock-rate=N Override conference bridge clock rate");
216 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000217 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000218 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000219 puts (" --play-file=file Register WAV file in conference bridge.");
220 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000221 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
222 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
223 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000224 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000225 puts (" --auto-play Automatically play the file (to incoming calls only)");
226 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000227 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000228 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
229 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000230 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000231 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000232 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000233 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono00cae612006-07-31 15:19:36 +0000234 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000235 puts (" --capture-dev=id Audio capture device ID (default=-1)");
236 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000237 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000238 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000239 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
240 puts (" Specify N=-1 (default) to disable this feature.");
241 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000242 puts (" --no-tones Disable audible tones");
Benny Prijono00cae612006-07-31 15:19:36 +0000243
Benny Prijonof76e1392008-06-06 14:51:48 +0000244 puts ("");
245 puts ("Media Transport Options:");
246 puts (" --use-ice Enable ICE (default:no)");
247 puts (" --ice-no-host Disable ICE host candidates");
248 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
249 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
250 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
251 puts (" --use-turn Enable TURN relay with ICE (default:no)");
252 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
253 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
254 puts (" --turn-user TURN username");
255 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000256
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000257 puts ("");
258 puts ("Buddy List (can be more than one):");
259 puts (" --add-buddy url Add the specified URL to the buddy list.");
260 puts ("");
261 puts ("User Agent options:");
262 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
263 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000264 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000265 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000266 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000267 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000268
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000269 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000270 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
271 puts ("");
272
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000273 fflush(stdout);
274}
275
276
277/* Set default config. */
278static void default_config(struct app_config *cfg)
279{
Benny Prijono56315612006-07-18 14:39:40 +0000280 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000281 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000282
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000283 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000284 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000285 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
286
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000287 pjsua_logging_config_default(&cfg->log_cfg);
288 pjsua_media_config_default(&cfg->media_cfg);
289 pjsua_transport_config_default(&cfg->udp_cfg);
290 cfg->udp_cfg.port = 5060;
291 pjsua_transport_config_default(&cfg->rtp_cfg);
292 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000293 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000294 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000295 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000296 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000297 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000298 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000299 cfg->capture_dev = PJSUA_INVALID_ID;
300 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000301 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
302 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000303 cfg->ringback_slot = PJSUA_INVALID_ID;
304 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000305
306 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
307 pjsua_acc_config_default(&cfg->acc_cfg[i]);
308
309 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
310 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000311}
312
313
314/*
315 * Read command arguments from config file.
316 */
317static int read_config_file(pj_pool_t *pool, const char *filename,
318 int *app_argc, char ***app_argv)
319{
320 int i;
321 FILE *fhnd;
322 char line[200];
323 int argc = 0;
324 char **argv;
325 enum { MAX_ARGS = 64 };
326
327 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
328 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
329 argv[argc++] = *app_argv[0];
330
331 /* Open config file. */
332 fhnd = fopen(filename, "rt");
333 if (!fhnd) {
334 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
335 fflush(stdout);
336 return -1;
337 }
338
339 /* Scan tokens in the file. */
340 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000341 char *token;
342 char *p;
343 const char *whitespace = " \t\r\n";
344 char cDelimiter;
345 int len, token_len;
346
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000347 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000348
349 // Trim ending newlines
350 len = strlen(line);
351 if (line[len-1]=='\n')
352 line[--len] = '\0';
353 if (line[len-1]=='\r')
354 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355
Benny Prijonobf5b4692007-06-28 03:20:17 +0000356 if (len==0) continue;
357
358 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
359 // first, scan whitespaces
360 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
361
362 if (*p == '\0') // are we done yet?
363 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000364
Benny Prijonobf5b4692007-06-28 03:20:17 +0000365 if (*p == '"' || *p == '\'') { // is token a quoted string
366 cDelimiter = *p++; // save quote delimiter
367 token = p;
368
369 while (*p != '\0' && *p != cDelimiter) p++;
370
371 if (*p == '\0') // found end of the line, but,
372 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000373
Benny Prijonobf5b4692007-06-28 03:20:17 +0000374 } else { // token's not a quoted string
375 token = p;
376
377 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
378
379 cDelimiter = *p;
380 }
381
382 *p = '\0';
383 token_len = p-token;
384
385 if (token_len > 0) {
386 if (*token == '#')
387 break; // ignore remainder of line
388
389 argv[argc] = pj_pool_alloc(pool, token_len + 1);
390 pj_memcpy(argv[argc], token, token_len + 1);
391 ++argc;
392 }
393
394 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395 }
396 }
397
398 /* Copy arguments from command line */
399 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
400 argv[argc++] = (*app_argv)[i];
401
402 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
403 PJ_LOG(1,(THIS_FILE,
404 "Too many arguments specified in cmd line/config file"));
405 fflush(stdout);
406 fclose(fhnd);
407 return -1;
408 }
409
410 fclose(fhnd);
411
412 /* Assign the new command line back to the original command line. */
413 *app_argc = argc;
414 *app_argv = argv;
415 return 0;
416
417}
418
419static int my_atoi(const char *cs)
420{
421 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000422
423 pj_cstr(&s, cs);
424 if (cs[0] == '-') {
425 s.ptr++, s.slen--;
426 return 0 - (int)pj_strtoul(&s);
427 } else if (cs[0] == '+') {
428 s.ptr++, s.slen--;
429 return pj_strtoul(&s);
430 } else {
431 return pj_strtoul(&s);
432 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000433}
434
435
436/* Parse arguments. */
437static pj_status_t parse_args(int argc, char *argv[],
438 struct app_config *cfg,
439 pj_str_t *uri_to_call)
440{
441 int c;
442 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000443 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000444 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000445 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
446 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000447 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000448 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000449 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000450 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000451 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
452 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijonof76e1392008-06-06 14:51:48 +0000453 OPT_USE_TURN,OPT_ICE_NO_HOST, OPT_TURN_SRV, OPT_TURN_TCP,
454 OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000455 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
456 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000457 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijonod79f25c2006-08-02 19:41:37 +0000458 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000459 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000460 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000461 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000462 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
463 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000464 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000465 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Benny Prijono91d20f42008-06-14 19:42:37 +0000466 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000467 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000468#ifdef _IONBF
469 OPT_STDOUT_NO_BUF,
470#endif
Benny Prijonofce28542007-12-09 15:41:10 +0000471 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000472 };
473 struct pj_getopt_option long_options[] = {
474 { "config-file",1, 0, OPT_CONFIG_FILE},
475 { "log-file", 1, 0, OPT_LOG_FILE},
476 { "log-level", 1, 0, OPT_LOG_LEVEL},
477 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
478 { "help", 0, 0, OPT_HELP},
479 { "version", 0, 0, OPT_VERSION},
480 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000481 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000482 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000483 { "null-audio", 0, 0, OPT_NULL_AUDIO},
484 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000485 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000486 { "no-tcp", 0, 0, OPT_NO_TCP},
487 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000488 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000489 { "proxy", 1, 0, OPT_PROXY},
490 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
491 { "registrar", 1, 0, OPT_REGISTRAR},
492 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000493 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000494 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000495 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496 { "id", 1, 0, OPT_ID},
497 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000498 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
499 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000500 { "realm", 1, 0, OPT_REALM},
501 { "username", 1, 0, OPT_USERNAME},
502 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000503 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000504 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000505 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000506 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
507 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
508 { "no-presence", 0, 0, OPT_NO_PRESENCE},
509 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000511 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000512 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000513 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
514 { "auto-conf", 0, 0, OPT_AUTO_CONF},
515 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000516 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000517 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000518 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000519
Benny Prijonoc97608e2007-03-23 16:34:20 +0000520 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000521 { "use-turn", 0, 0, OPT_USE_TURN},
522 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
523 { "turn-srv", 1, 0, OPT_TURN_SRV},
524 { "turn-tcp", 0, 0, OPT_TURN_TCP},
525 { "turn-user", 1, 0, OPT_TURN_USER},
526 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
527
Benny Prijonod8179652008-01-23 20:39:07 +0000528#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
529 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000530 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000531#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000532 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000533 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 { "complexity", 1, 0, OPT_COMPLEXITY},
535 { "quality", 1, 0, OPT_QUALITY},
536 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000537 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000538 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono00cae612006-07-31 15:19:36 +0000539 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
540 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
541 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000543 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000544 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000545 { "duration", 1, 0, OPT_DURATION},
546 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000547 { "use-tls", 0, 0, OPT_USE_TLS},
548 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000549 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
550 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000551 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000552 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
553 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
554 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000555 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000556 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
557 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000558 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
559 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000560 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
561 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000562#ifdef _IONBF
563 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
564#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000565 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000566 { "no-tones", 0, 0, OPT_NO_TONES},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000567 { NULL, 0, 0, 0}
568 };
569 pj_status_t status;
570 pjsua_acc_config *cur_acc;
571 char *config_file = NULL;
572 unsigned i;
573
574 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000575 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000576 while ((c=pj_getopt_long(argc, argv, "", long_options,
577 &option_index)) != -1)
578 {
579 switch (c) {
580 case OPT_CONFIG_FILE:
581 config_file = pj_optarg;
582 break;
583 }
584 if (config_file)
585 break;
586 }
587
588 if (config_file) {
589 status = read_config_file(app_config.pool, config_file, &argc, &argv);
590 if (status != 0)
591 return status;
592 }
593
594 cfg->acc_cnt = 0;
595 cur_acc = &cfg->acc_cfg[0];
596
597
598 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
599 * read from config file.
600 */
601 pj_optind = 0;
602 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000603 pj_str_t tmp;
604 long lval;
605
606 switch (c) {
607
Benny Prijono6f137482006-06-15 11:31:36 +0000608 case OPT_CONFIG_FILE:
609 /* Ignore as this has been processed before */
610 break;
611
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000612 case OPT_LOG_FILE:
613 cfg->log_cfg.log_filename = pj_str(pj_optarg);
614 break;
615
616 case OPT_LOG_LEVEL:
617 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
618 if (c < 0 || c > 6) {
619 PJ_LOG(1,(THIS_FILE,
620 "Error: expecting integer value 0-6 "
621 "for --log-level"));
622 return PJ_EINVAL;
623 }
624 cfg->log_cfg.level = c;
625 pj_log_set_level( c );
626 break;
627
628 case OPT_APP_LOG_LEVEL:
629 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
630 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
631 PJ_LOG(1,(THIS_FILE,
632 "Error: expecting integer value 0-6 "
633 "for --app-log-level"));
634 return PJ_EINVAL;
635 }
636 break;
637
638 case OPT_HELP:
639 usage();
640 return PJ_EINVAL;
641
642 case OPT_VERSION: /* version */
643 pj_dump_config();
644 return PJ_EINVAL;
645
646 case OPT_NULL_AUDIO:
647 cfg->null_audio = PJ_TRUE;
648 break;
649
650 case OPT_CLOCK_RATE:
651 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000652 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000653 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000654 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000655 return PJ_EINVAL;
656 }
657 cfg->media_cfg.clock_rate = lval;
658 break;
659
Benny Prijonof3758ee2008-02-26 15:32:16 +0000660 case OPT_SND_CLOCK_RATE:
661 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000662 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000663 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000664 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000665 return PJ_EINVAL;
666 }
667 cfg->media_cfg.snd_clock_rate = lval;
668 break;
669
Benny Prijono7d60d052008-03-29 12:24:20 +0000670 case OPT_STEREO:
671 cfg->media_cfg.channel_count = 2;
672 break;
673
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000674 case OPT_LOCAL_PORT: /* local-port */
675 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000676 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000677 PJ_LOG(1,(THIS_FILE,
678 "Error: expecting integer value for "
679 "--local-port"));
680 return PJ_EINVAL;
681 }
682 cfg->udp_cfg.port = (pj_uint16_t)lval;
683 break;
684
Benny Prijono0a5cad82006-09-26 13:21:02 +0000685 case OPT_IP_ADDR: /* ip-addr */
686 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
687 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
688 break;
689
Benny Prijonoe93e2872006-06-28 16:46:49 +0000690 case OPT_NO_UDP: /* no-udp */
691 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000692 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
693 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000694 }
695
696 cfg->no_udp = PJ_TRUE;
697 break;
698
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000699 case OPT_NOREFERSUB: /* norefersub */
700 cfg->no_refersub = PJ_TRUE;
701 break;
702
Benny Prijonoe93e2872006-06-28 16:46:49 +0000703 case OPT_NO_TCP: /* no-tcp */
704 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000705 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
706 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000707 }
708
709 cfg->no_tcp = PJ_TRUE;
710 break;
711
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000712 case OPT_PROXY: /* proxy */
713 if (pjsua_verify_sip_url(pj_optarg) != 0) {
714 PJ_LOG(1,(THIS_FILE,
715 "Error: invalid SIP URL '%s' "
716 "in proxy argument", pj_optarg));
717 return PJ_EINVAL;
718 }
719 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
720 break;
721
722 case OPT_OUTBOUND_PROXY: /* outbound proxy */
723 if (pjsua_verify_sip_url(pj_optarg) != 0) {
724 PJ_LOG(1,(THIS_FILE,
725 "Error: invalid SIP URL '%s' "
726 "in outbound proxy argument", pj_optarg));
727 return PJ_EINVAL;
728 }
729 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
730 break;
731
732 case OPT_REGISTRAR: /* registrar */
733 if (pjsua_verify_sip_url(pj_optarg) != 0) {
734 PJ_LOG(1,(THIS_FILE,
735 "Error: invalid SIP URL '%s' in "
736 "registrar argument", pj_optarg));
737 return PJ_EINVAL;
738 }
739 cur_acc->reg_uri = pj_str(pj_optarg);
740 break;
741
742 case OPT_REG_TIMEOUT: /* reg-timeout */
743 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
744 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
745 PJ_LOG(1,(THIS_FILE,
746 "Error: invalid value for --reg-timeout "
747 "(expecting 1-3600)"));
748 return PJ_EINVAL;
749 }
750 break;
751
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000752 case OPT_PUBLISH: /* publish */
753 cur_acc->publish_enabled = PJ_TRUE;
754 break;
755
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000756 case OPT_100REL: /** 100rel */
757 cur_acc->require_100rel = PJ_TRUE;
758 cfg->cfg.require_100rel = PJ_TRUE;
759 break;
760
Benny Prijono48ab2b72007-11-08 09:24:30 +0000761 case OPT_USE_IMS: /* Activate IMS settings */
762 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000763 break;
764
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000765 case OPT_ID: /* id */
766 if (pjsua_verify_sip_url(pj_optarg) != 0) {
767 PJ_LOG(1,(THIS_FILE,
768 "Error: invalid SIP URL '%s' "
769 "in local id argument", pj_optarg));
770 return PJ_EINVAL;
771 }
772 cur_acc->id = pj_str(pj_optarg);
773 break;
774
775 case OPT_CONTACT: /* contact */
776 if (pjsua_verify_sip_url(pj_optarg) != 0) {
777 PJ_LOG(1,(THIS_FILE,
778 "Error: invalid SIP URL '%s' "
779 "in contact argument", pj_optarg));
780 return PJ_EINVAL;
781 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000782 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000783 break;
784
Benny Prijonofce28542007-12-09 15:41:10 +0000785 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000786 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000787 break;
788
789 case OPT_USE_COMPACT_FORM:
790 /* enable compact form - from Ticket #342 */
791 {
792 extern pj_bool_t pjsip_use_compact_form;
793 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
794 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
795
796 pjsip_use_compact_form = PJ_TRUE;
797 /* do not transmit Allow header */
798 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
799 /* Do not include rtpmap for static payload types (<96) */
800 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
801 }
802 break;
803
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000804 case OPT_NEXT_ACCOUNT: /* Add more account. */
805 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000806 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000807 break;
808
809 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000810 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000811 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000812 break;
813
814 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000815 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000816 break;
817
818 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000819 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
820 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000821#if PJSIP_HAS_DIGEST_AKA_AUTH
822 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
823 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
824 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
825#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000826 break;
827
828 case OPT_NEXT_CRED: /* next credential */
829 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000830 break;
831
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000832 case OPT_NAMESERVER: /* nameserver */
833 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
834 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
835 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
836 return PJ_ETOOMANY;
837 }
838 break;
839
Benny Prijonoebbf6892007-03-24 17:37:25 +0000840 case OPT_STUN_DOMAIN: /* STUN domain */
841 cfg->cfg.stun_domain = pj_str(pj_optarg);
842 break;
843
Benny Prijonoc97608e2007-03-23 16:34:20 +0000844 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000845 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000846 break;
847
848 case OPT_ADD_BUDDY: /* Add to buddy list. */
849 if (pjsua_verify_sip_url(pj_optarg) != 0) {
850 PJ_LOG(1,(THIS_FILE,
851 "Error: invalid URL '%s' in "
852 "--add-buddy option", pj_optarg));
853 return -1;
854 }
855 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
856 PJ_LOG(1,(THIS_FILE,
857 "Error: too many buddies in buddy list."));
858 return -1;
859 }
860 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
861 cfg->buddy_cnt++;
862 break;
863
864 case OPT_AUTO_PLAY:
865 cfg->auto_play = 1;
866 break;
867
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000868 case OPT_AUTO_PLAY_HANGUP:
869 cfg->auto_play_hangup = 1;
870 break;
871
Benny Prijono1ebd6142006-10-19 15:48:02 +0000872 case OPT_AUTO_REC:
873 cfg->auto_rec = 1;
874 break;
875
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000876 case OPT_AUTO_LOOP:
877 cfg->auto_loop = 1;
878 break;
879
Benny Prijono7ca96da2006-08-07 12:11:40 +0000880 case OPT_AUTO_CONF:
881 cfg->auto_conf = 1;
882 break;
883
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000884 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000885 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000886 break;
887
Benny Prijono4af234b2007-01-24 02:02:09 +0000888 case OPT_PLAY_TONE:
889 {
890 int f1, f2, on, off;
891 int n;
892
893 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
894 if (n != 4) {
895 puts("Expecting f1,f2,on,off in --play-tone");
896 return -1;
897 }
898
899 cfg->tones[cfg->tone_count].freq1 = (short)f1;
900 cfg->tones[cfg->tone_count].freq2 = (short)f2;
901 cfg->tones[cfg->tone_count].on_msec = (short)on;
902 cfg->tones[cfg->tone_count].off_msec = (short)off;
903 ++cfg->tone_count;
904 }
905 break;
906
Benny Prijono1ebd6142006-10-19 15:48:02 +0000907 case OPT_REC_FILE:
908 cfg->rec_file = pj_str(pj_optarg);
909 break;
910
Benny Prijonoc97608e2007-03-23 16:34:20 +0000911 case OPT_USE_ICE:
912 cfg->media_cfg.enable_ice = PJ_TRUE;
913 break;
914
Benny Prijonof76e1392008-06-06 14:51:48 +0000915 case OPT_USE_TURN:
916 cfg->media_cfg.enable_turn = PJ_TRUE;
917 break;
918
919 case OPT_ICE_NO_HOST:
920 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
921 break;
922
923 case OPT_TURN_SRV:
924 cfg->media_cfg.turn_server = pj_str(pj_optarg);
925 break;
926
927 case OPT_TURN_TCP:
928 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
929 break;
930
931 case OPT_TURN_USER:
932 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
933 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
934 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
935 break;
936
937 case OPT_TURN_PASSWD:
938 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
939 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
940 break;
941
Benny Prijonod8179652008-01-23 20:39:07 +0000942#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
943 case OPT_USE_SRTP:
944 app_config.cfg.use_srtp = my_atoi(pj_optarg);
945 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
946 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
947 return -1;
948 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000949 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +0000950 break;
Benny Prijonof6508982008-01-25 09:02:33 +0000951 case OPT_SRTP_SECURE:
952 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
953 if (!pj_isdigit(*pj_optarg) ||
954 app_config.cfg.srtp_secure_signaling > 2)
955 {
956 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
957 return -1;
958 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000959 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +0000960 break;
Benny Prijonod8179652008-01-23 20:39:07 +0000961#endif
962
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000963 case OPT_RTP_PORT:
964 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +0000965 if (cfg->rtp_cfg.port == 0) {
966 enum { START_PORT=4000 };
967 unsigned range;
968
969 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
970 cfg->rtp_cfg.port = START_PORT +
971 ((pj_rand() % range) & 0xFFFE);
972 }
973
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000974 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
975 PJ_LOG(1,(THIS_FILE,
976 "Error: rtp-port argument value "
977 "(expecting 1-65535"));
978 return -1;
979 }
980 break;
981
Benny Prijonofce28542007-12-09 15:41:10 +0000982 case OPT_DIS_CODEC:
983 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
984 break;
985
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000986 case OPT_ADD_CODEC:
987 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
988 break;
989
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000990 /* These options were no longer valid after new pjsua */
991 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 case OPT_COMPLEXITY:
993 cfg->complexity = my_atoi(pj_optarg);
994 if (cfg->complexity < 0 || cfg->complexity > 10) {
995 PJ_LOG(1,(THIS_FILE,
996 "Error: invalid --complexity (expecting 0-10"));
997 return -1;
998 }
999 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001000 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001001
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001002 case OPT_DURATION:
1003 cfg->duration = my_atoi(pj_optarg);
1004 break;
1005
Benny Prijonof521eb02006-08-06 23:07:25 +00001006 case OPT_THREAD_CNT:
1007 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1008 if (cfg->cfg.thread_cnt > 128) {
1009 PJ_LOG(1,(THIS_FILE,
1010 "Error: invalid --thread-cnt option"));
1011 return -1;
1012 }
1013 break;
1014
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001015 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001016 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1017 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001018 PJ_LOG(1,(THIS_FILE,
1019 "Error: invalid --ptime option"));
1020 return -1;
1021 }
1022 break;
1023
Benny Prijono0a12f002006-07-26 17:05:39 +00001024 case OPT_NO_VAD:
1025 cfg->media_cfg.no_vad = PJ_TRUE;
1026 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001027
Benny Prijonod79f25c2006-08-02 19:41:37 +00001028 case OPT_EC_TAIL:
1029 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1030 if (cfg->media_cfg.ec_tail_len > 1000) {
1031 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1032 "is too big"));
1033 return -1;
1034 }
1035 break;
1036
Benny Prijono0498d902006-06-19 14:49:14 +00001037 case OPT_QUALITY:
1038 cfg->media_cfg.quality = my_atoi(pj_optarg);
1039 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1040 PJ_LOG(1,(THIS_FILE,
1041 "Error: invalid --quality (expecting 0-10"));
1042 return -1;
1043 }
1044 break;
1045
Benny Prijono00cae612006-07-31 15:19:36 +00001046 case OPT_ILBC_MODE:
1047 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1048 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1049 PJ_LOG(1,(THIS_FILE,
1050 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1051 return -1;
1052 }
1053 break;
1054
1055 case OPT_RX_DROP_PCT:
1056 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1057 if (cfg->media_cfg.rx_drop_pct > 100) {
1058 PJ_LOG(1,(THIS_FILE,
1059 "Error: invalid --rx-drop-pct (expecting <= 100"));
1060 return -1;
1061 }
1062 break;
1063
1064 case OPT_TX_DROP_PCT:
1065 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1066 if (cfg->media_cfg.tx_drop_pct > 100) {
1067 PJ_LOG(1,(THIS_FILE,
1068 "Error: invalid --tx-drop-pct (expecting <= 100"));
1069 return -1;
1070 }
1071 break;
1072
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001073 case OPT_AUTO_ANSWER:
1074 cfg->auto_answer = my_atoi(pj_optarg);
1075 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1076 PJ_LOG(1,(THIS_FILE,
1077 "Error: invalid code in --auto-answer "
1078 "(expecting 100-699"));
1079 return -1;
1080 }
1081 break;
1082
1083 case OPT_MAX_CALLS:
1084 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001085 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001086 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1087 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001088 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089 return -1;
1090 }
1091 break;
1092
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001093 case OPT_USE_TLS:
1094 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001095#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
1101 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001102 cfg->udp_cfg.tls_setting.ca_list_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_CERT_FILE:
1110 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1111#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1112 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1113 return -1;
1114#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001115 break;
1116
Benny Prijonof3bbc132006-12-25 06:43:59 +00001117 case OPT_TLS_PRIV_FILE:
1118 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1119 break;
1120
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001121 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001122 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1123#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1124 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1125 return -1;
1126#endif
1127 break;
1128
1129 case OPT_TLS_VERIFY_SERVER:
1130 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1131 break;
1132
1133 case OPT_TLS_VERIFY_CLIENT:
1134 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1135 break;
1136
1137 case OPT_TLS_NEG_TIMEOUT:
1138 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001139 break;
1140
Benny Prijonoe10db842008-07-01 15:31:59 +00001141 case OPT_TLS_SRV_NAME:
1142 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1143 break;
1144
Benny Prijono4e5d5512007-03-06 18:11:30 +00001145 case OPT_CAPTURE_DEV:
1146 cfg->capture_dev = atoi(pj_optarg);
1147 break;
1148
1149 case OPT_PLAYBACK_DEV:
1150 cfg->playback_dev = atoi(pj_optarg);
1151 break;
1152
Benny Prijonoebc32c32008-06-12 13:30:39 +00001153 case OPT_STDOUT_REFRESH:
1154 stdout_refresh = atoi(pj_optarg);
1155 break;
1156
1157 case OPT_STDOUT_REFRESH_TEXT:
1158 stdout_refresh_text = pj_optarg;
1159 break;
1160
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001161#ifdef _IONBF
1162 case OPT_STDOUT_NO_BUF:
1163 setvbuf(stdout, NULL, _IONBF, 0);
1164 break;
1165#endif
1166
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001167 case OPT_CAPTURE_LAT:
1168 cfg->capture_lat = atoi(pj_optarg);
1169 break;
1170
1171 case OPT_PLAYBACK_LAT:
1172 cfg->playback_lat = atoi(pj_optarg);
1173 break;
1174
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001175 case OPT_SND_AUTO_CLOSE:
1176 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1177 break;
1178
Benny Prijono91d20f42008-06-14 19:42:37 +00001179 case OPT_NO_TONES:
1180 cfg->no_tones = PJ_TRUE;
1181 break;
1182
Benny Prijono22a300a2006-06-14 20:04:55 +00001183 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001184 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001185 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001186 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001187 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001188 }
1189 }
1190
1191 if (pj_optind != argc) {
1192 pj_str_t uri_arg;
1193
1194 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1195 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1196 return -1;
1197 }
1198 uri_arg = pj_str(argv[pj_optind]);
1199 if (uri_to_call)
1200 *uri_to_call = uri_arg;
1201 pj_optind++;
1202
1203 /* Add URI to call to buddy list if it's not already there */
1204 for (i=0; i<cfg->buddy_cnt; ++i) {
1205 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1206 break;
1207 }
1208 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1209 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1210 }
1211
1212 } else {
1213 if (uri_to_call)
1214 uri_to_call->slen = 0;
1215 }
1216
1217 if (pj_optind != argc) {
1218 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1219 return PJ_EINVAL;
1220 }
1221
Benny Prijono56315612006-07-18 14:39:40 +00001222 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1223 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001224
1225 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001226 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1227
1228 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001229 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001230 acfg->cred_count++;
1231 }
1232
1233 /* When IMS mode is enabled for the account, verify that settings
1234 * are okay.
1235 */
1236 /* For now we check if IMS mode is activated by looking if
1237 * initial_auth is set.
1238 */
1239 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1240 /* Realm must point to the real domain */
1241 if (*acfg->cred_info[0].realm.ptr=='*') {
1242 PJ_LOG(1,(THIS_FILE,
1243 "Error: cannot use '*' as realm with IMS"));
1244 return PJ_EINVAL;
1245 }
1246
1247 /* Username for authentication must be in a@b format */
1248 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1249 PJ_LOG(1,(THIS_FILE,
1250 "Error: Username for authentication must "
1251 "be in user@domain format with IMS"));
1252 return PJ_EINVAL;
1253 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001254 }
1255 }
1256
1257
1258 return PJ_SUCCESS;
1259}
1260
1261
1262/*
1263 * Save account settings
1264 */
1265static void write_account_settings(int acc_index, pj_str_t *result)
1266{
1267 unsigned i;
1268 char line[128];
1269 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1270
1271
1272 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1273 pj_strcat2(result, line);
1274
1275
1276 /* Identity */
1277 if (acc_cfg->id.slen) {
1278 pj_ansi_sprintf(line, "--id %.*s\n",
1279 (int)acc_cfg->id.slen,
1280 acc_cfg->id.ptr);
1281 pj_strcat2(result, line);
1282 }
1283
1284 /* Registrar server */
1285 if (acc_cfg->reg_uri.slen) {
1286 pj_ansi_sprintf(line, "--registrar %.*s\n",
1287 (int)acc_cfg->reg_uri.slen,
1288 acc_cfg->reg_uri.ptr);
1289 pj_strcat2(result, line);
1290
1291 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1292 acc_cfg->reg_timeout);
1293 pj_strcat2(result, line);
1294 }
1295
1296 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001297 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001298 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001299 (int)acc_cfg->force_contact.slen,
1300 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001301 pj_strcat2(result, line);
1302 }
1303
Benny Prijonofce28542007-12-09 15:41:10 +00001304 /* */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001305 if (acc_cfg->allow_contact_rewrite==0)
Benny Prijonofce28542007-12-09 15:41:10 +00001306 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001307 pj_ansi_sprintf(line, "--contact-rewrite %i\n",
1308 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001309 pj_strcat2(result, line);
1310 }
1311
Benny Prijonod8179652008-01-23 20:39:07 +00001312#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1313 /* SRTP */
1314 if (acc_cfg->use_srtp) {
1315 pj_ansi_sprintf(line, "--use-srtp %i\n",
1316 (int)acc_cfg->use_srtp);
1317 pj_strcat2(result, line);
1318 }
1319#endif
1320
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001321 /* Proxy */
1322 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1323 pj_ansi_sprintf(line, "--proxy %.*s\n",
1324 (int)acc_cfg->proxy[i].slen,
1325 acc_cfg->proxy[i].ptr);
1326 pj_strcat2(result, line);
1327 }
1328
1329 /* Credentials */
1330 for (i=0; i<acc_cfg->cred_count; ++i) {
1331 if (acc_cfg->cred_info[i].realm.slen) {
1332 pj_ansi_sprintf(line, "--realm %.*s\n",
1333 (int)acc_cfg->cred_info[i].realm.slen,
1334 acc_cfg->cred_info[i].realm.ptr);
1335 pj_strcat2(result, line);
1336 }
1337
1338 if (acc_cfg->cred_info[i].username.slen) {
1339 pj_ansi_sprintf(line, "--username %.*s\n",
1340 (int)acc_cfg->cred_info[i].username.slen,
1341 acc_cfg->cred_info[i].username.ptr);
1342 pj_strcat2(result, line);
1343 }
1344
1345 if (acc_cfg->cred_info[i].data.slen) {
1346 pj_ansi_sprintf(line, "--password %.*s\n",
1347 (int)acc_cfg->cred_info[i].data.slen,
1348 acc_cfg->cred_info[i].data.ptr);
1349 pj_strcat2(result, line);
1350 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001351
1352 if (i != acc_cfg->cred_count - 1)
1353 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001354 }
1355
1356}
1357
1358
1359/*
1360 * Write settings.
1361 */
1362static int write_settings(const struct app_config *config,
1363 char *buf, pj_size_t max)
1364{
1365 unsigned acc_index;
1366 unsigned i;
1367 pj_str_t cfg;
1368 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001369 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001370
1371 PJ_UNUSED_ARG(max);
1372
1373 cfg.ptr = buf;
1374 cfg.slen = 0;
1375
1376 /* Logging. */
1377 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1378 pj_ansi_sprintf(line, "--log-level %d\n",
1379 config->log_cfg.level);
1380 pj_strcat2(&cfg, line);
1381
1382 pj_ansi_sprintf(line, "--app-log-level %d\n",
1383 config->log_cfg.console_level);
1384 pj_strcat2(&cfg, line);
1385
1386 if (config->log_cfg.log_filename.slen) {
1387 pj_ansi_sprintf(line, "--log-file %.*s\n",
1388 (int)config->log_cfg.log_filename.slen,
1389 config->log_cfg.log_filename.ptr);
1390 pj_strcat2(&cfg, line);
1391 }
1392
1393
1394 /* Save account settings. */
1395 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1396
1397 write_account_settings(acc_index, &cfg);
1398
1399 if (acc_index < config->acc_cnt-1)
1400 pj_strcat2(&cfg, "--next-account\n");
1401 }
1402
1403
1404 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1405
1406 /* Outbound proxy */
1407 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1408 pj_ansi_sprintf(line, "--outbound %.*s\n",
1409 (int)config->cfg.outbound_proxy[i].slen,
1410 config->cfg.outbound_proxy[i].ptr);
1411 pj_strcat2(&cfg, line);
1412 }
1413
1414
1415 /* UDP Transport. */
1416 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1417 pj_strcat2(&cfg, line);
1418
Benny Prijono0a5cad82006-09-26 13:21:02 +00001419 /* IP address, if any. */
1420 if (config->udp_cfg.public_addr.slen) {
1421 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1422 (int)config->udp_cfg.public_addr.slen,
1423 config->udp_cfg.public_addr.ptr);
1424 pj_strcat2(&cfg, line);
1425 }
1426
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001427 /* No TCP ? */
1428 if (config->no_tcp) {
1429 pj_strcat2(&cfg, "--no-tcp\n");
1430 }
1431
1432 /* No UDP ? */
1433 if (config->no_udp) {
1434 pj_strcat2(&cfg, "--no-udp\n");
1435 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001436
1437 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001438 if (config->cfg.stun_domain.slen) {
1439 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1440 (int)config->cfg.stun_domain.slen,
1441 config->cfg.stun_domain.ptr);
1442 pj_strcat2(&cfg, line);
1443 }
1444 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001445 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001446 (int)config->cfg.stun_host.slen,
1447 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001448 pj_strcat2(&cfg, line);
1449 }
1450
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001451 /* TLS */
1452 if (config->use_tls)
1453 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001454 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001455 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001456 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1457 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001458 pj_strcat2(&cfg, line);
1459 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001460 if (config->udp_cfg.tls_setting.cert_file.slen) {
1461 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1462 (int)config->udp_cfg.tls_setting.cert_file.slen,
1463 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001464 pj_strcat2(&cfg, line);
1465 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001466 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1467 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1468 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1469 config->udp_cfg.tls_setting.privkey_file.ptr);
1470 pj_strcat2(&cfg, line);
1471 }
1472
1473 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001474 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001475 (int)config->udp_cfg.tls_setting.password.slen,
1476 config->udp_cfg.tls_setting.password.ptr);
1477 pj_strcat2(&cfg, line);
1478 }
1479
Benny Prijonoe10db842008-07-01 15:31:59 +00001480 if (config->udp_cfg.tls_setting.server_name.slen) {
1481 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1482 (int)config->udp_cfg.tls_setting.server_name.slen,
1483 config->udp_cfg.tls_setting.server_name.ptr);
1484 pj_strcat2(&cfg, line);
1485 }
1486
Benny Prijonof3bbc132006-12-25 06:43:59 +00001487 if (config->udp_cfg.tls_setting.verify_server)
1488 pj_strcat2(&cfg, "--tls-verify-server\n");
1489
1490 if (config->udp_cfg.tls_setting.verify_client)
1491 pj_strcat2(&cfg, "--tls-verify-client\n");
1492
1493 if (config->udp_cfg.tls_setting.timeout.sec) {
1494 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001495 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001496 pj_strcat2(&cfg, line);
1497 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001498
1499 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1500
Benny Prijonof6508982008-01-25 09:02:33 +00001501 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001502#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001503 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1504 pj_ansi_sprintf(line, "--use-srtp %d\n",
1505 app_config.cfg.use_srtp);
1506 pj_strcat2(&cfg, line);
1507 }
1508 if (app_config.cfg.srtp_secure_signaling !=
1509 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1510 {
1511 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1512 app_config.cfg.srtp_secure_signaling);
1513 pj_strcat2(&cfg, line);
1514 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001515#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001516
Benny Prijonof76e1392008-06-06 14:51:48 +00001517 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001518 if (config->media_cfg.enable_ice)
1519 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001520
1521 if (config->media_cfg.enable_turn)
1522 pj_strcat2(&cfg, "--use-turn\n");
1523
1524 if (config->media_cfg.ice_no_host_cands)
1525 pj_strcat2(&cfg, "--ice-no-host\n");
1526
1527 if (config->media_cfg.turn_server.slen) {
1528 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1529 (int)config->media_cfg.turn_server.slen,
1530 config->media_cfg.turn_server.ptr);
1531 pj_strcat2(&cfg, line);
1532 }
1533
1534 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1535 pj_strcat2(&cfg, "--turn-tcp\n");
1536
1537 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1538 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1539 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1540 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1541 pj_strcat2(&cfg, line);
1542 }
1543
1544 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1545 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1546 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1547 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1548 pj_strcat2(&cfg, line);
1549 }
1550
1551 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001552 if (config->null_audio)
1553 pj_strcat2(&cfg, "--null-audio\n");
1554 if (config->auto_play)
1555 pj_strcat2(&cfg, "--auto-play\n");
1556 if (config->auto_loop)
1557 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001558 if (config->auto_conf)
1559 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001560 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001561 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001562 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001563 pj_strcat2(&cfg, line);
1564 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001565 for (i=0; i<config->tone_count; ++i) {
1566 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1567 config->tones[i].freq1, config->tones[i].freq2,
1568 config->tones[i].on_msec, config->tones[i].off_msec);
1569 pj_strcat2(&cfg, line);
1570 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001571 if (config->rec_file.slen) {
1572 pj_ansi_sprintf(line, "--rec-file %s\n",
1573 config->rec_file.ptr);
1574 pj_strcat2(&cfg, line);
1575 }
1576 if (config->auto_rec)
1577 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001578 if (config->capture_dev != PJSUA_INVALID_ID) {
1579 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1580 pj_strcat2(&cfg, line);
1581 }
1582 if (config->playback_dev != PJSUA_INVALID_ID) {
1583 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1584 pj_strcat2(&cfg, line);
1585 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001586 if (config->media_cfg.snd_auto_close_time != -1) {
1587 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1588 config->media_cfg.snd_auto_close_time);
1589 pj_strcat2(&cfg, line);
1590 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001591 if (config->no_tones) {
1592 pj_strcat2(&cfg, "--no-tones\n");
1593 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001594
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001595 /* Sound device latency */
1596 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1597 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1598 pj_strcat2(&cfg, line);
1599 }
1600 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1601 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1602 pj_strcat2(&cfg, line);
1603 }
1604
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001605 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001606 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001607 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001608 config->media_cfg.clock_rate);
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 --clock-rate %d\n",
1612 config->media_cfg.clock_rate);
1613 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001614 }
Benny Prijono70972992006-08-05 11:13:58 +00001615
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001616 if (config->media_cfg.snd_clock_rate &&
1617 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1618 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001619 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1620 config->media_cfg.snd_clock_rate);
1621 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001622 }
1623
Benny Prijono7d60d052008-03-29 12:24:20 +00001624 /* Stereo mode. */
1625 if (config->media_cfg.channel_count == 2) {
1626 pj_ansi_sprintf(line, "--stereo\n");
1627 pj_strcat2(&cfg, line);
1628 }
1629
Benny Prijono70972992006-08-05 11:13:58 +00001630 /* quality */
1631 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001632 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001633 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001634 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001635 } else {
1636 pj_ansi_sprintf(line, "#using default --quality %d\n",
1637 config->media_cfg.quality);
1638 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001639 }
Benny Prijono0498d902006-06-19 14:49:14 +00001640
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001641
1642 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001643 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001644 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001645 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001646 pj_strcat2(&cfg, line);
1647 }
1648
Benny Prijono70972992006-08-05 11:13:58 +00001649 /* no-vad */
1650 if (config->media_cfg.no_vad) {
1651 pj_strcat2(&cfg, "--no-vad\n");
1652 }
1653
1654 /* ec-tail */
1655 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1656 pj_ansi_sprintf(line, "--ec-tail %d\n",
1657 config->media_cfg.ec_tail_len);
1658 pj_strcat2(&cfg, line);
1659 } else {
1660 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1661 config->media_cfg.ec_tail_len);
1662 pj_strcat2(&cfg, line);
1663 }
1664
1665
1666 /* ilbc-mode */
1667 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1668 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1669 config->media_cfg.ilbc_mode);
1670 pj_strcat2(&cfg, line);
1671 } else {
1672 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1673 config->media_cfg.ilbc_mode);
1674 pj_strcat2(&cfg, line);
1675 }
1676
1677 /* RTP drop */
1678 if (config->media_cfg.tx_drop_pct) {
1679 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1680 config->media_cfg.tx_drop_pct);
1681 pj_strcat2(&cfg, line);
1682
1683 }
1684 if (config->media_cfg.rx_drop_pct) {
1685 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1686 config->media_cfg.rx_drop_pct);
1687 pj_strcat2(&cfg, line);
1688
1689 }
1690
1691
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001692 /* Start RTP port. */
1693 pj_ansi_sprintf(line, "--rtp-port %d\n",
1694 config->rtp_cfg.port);
1695 pj_strcat2(&cfg, line);
1696
1697 /* Add codec. */
1698 for (i=0; i<config->codec_cnt; ++i) {
1699 pj_ansi_sprintf(line, "--add-codec %s\n",
1700 config->codec_arg[i].ptr);
1701 pj_strcat2(&cfg, line);
1702 }
Benny Prijonofce28542007-12-09 15:41:10 +00001703 /* Disable codec */
1704 for (i=0; i<config->codec_dis_cnt; ++i) {
1705 pj_ansi_sprintf(line, "--dis-codec %s\n",
1706 config->codec_dis[i].ptr);
1707 pj_strcat2(&cfg, line);
1708 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001709
1710 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1711
1712 /* Auto-answer. */
1713 if (config->auto_answer != 0) {
1714 pj_ansi_sprintf(line, "--auto-answer %d\n",
1715 config->auto_answer);
1716 pj_strcat2(&cfg, line);
1717 }
1718
1719 /* Max calls. */
1720 pj_ansi_sprintf(line, "--max-calls %d\n",
1721 config->cfg.max_calls);
1722 pj_strcat2(&cfg, line);
1723
1724 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001725 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001726 pj_ansi_sprintf(line, "--duration %d\n",
1727 config->duration);
1728 pj_strcat2(&cfg, line);
1729 }
1730
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001731 /* norefersub ? */
1732 if (config->no_refersub) {
1733 pj_strcat2(&cfg, "--norefersub\n");
1734 }
1735
Benny Prijonofce28542007-12-09 15:41:10 +00001736 if (pjsip_use_compact_form)
1737 {
1738 pj_strcat2(&cfg, "--use-compact-form\n");
1739 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001740
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001741 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1742
1743 /* Add buddies. */
1744 for (i=0; i<config->buddy_cnt; ++i) {
1745 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1746 (int)config->buddy_cfg[i].uri.slen,
1747 config->buddy_cfg[i].uri.ptr);
1748 pj_strcat2(&cfg, line);
1749 }
1750
1751
1752 *(cfg.ptr + cfg.slen) = '\0';
1753 return cfg.slen;
1754}
1755
1756
1757/*
1758 * Dump application states.
1759 */
1760static void app_dump(pj_bool_t detail)
1761{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001762 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001763}
1764
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001765/*
1766 * Print log of call states. Since call states may be too long for logger,
1767 * printing it is a bit tricky, it should be printed part by part as long
1768 * as the logger can accept.
1769 */
1770static void log_call_dump(int call_id) {
1771 unsigned call_dump_len;
1772 unsigned part_len;
1773 unsigned part_idx;
1774 unsigned log_decor;
1775
1776 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1777 sizeof(some_buf), " ");
1778 call_dump_len = strlen(some_buf);
1779
1780 log_decor = pj_log_get_decor();
1781 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1782 PJ_LOG(3,(THIS_FILE, "\n"));
1783 pj_log_set_decor(0);
1784
1785 part_idx = 0;
1786 part_len = PJ_LOG_MAX_SIZE-80;
1787 while (part_idx < call_dump_len) {
1788 char p_orig, *p;
1789
1790 p = &some_buf[part_idx];
1791 if (part_idx + part_len > call_dump_len)
1792 part_len = call_dump_len - part_idx;
1793 p_orig = p[part_len];
1794 p[part_len] = '\0';
1795 PJ_LOG(3,(THIS_FILE, "%s", p));
1796 p[part_len] = p_orig;
1797 part_idx += part_len;
1798 }
1799 pj_log_set_decor(log_decor);
1800}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001801
1802/*****************************************************************************
1803 * Console application
1804 */
1805
Benny Prijono91d20f42008-06-14 19:42:37 +00001806static void ringback_start(pjsua_call_id call_id)
1807{
1808 if (app_config.no_tones)
1809 return;
1810
1811 if (app_config.call_data[call_id].ringback_on)
1812 return;
1813
1814 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1815
1816 if (++app_config.ringback_cnt==1 &&
1817 app_config.ringback_slot!=PJSUA_INVALID_ID)
1818 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001819 pjsua_conf_connect(app_config.ringback_slot, 0);
1820 }
1821}
1822
1823static void ring_stop(pjsua_call_id call_id)
1824{
1825 if (app_config.no_tones)
1826 return;
1827
1828 if (app_config.call_data[call_id].ringback_on) {
1829 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1830
1831 pj_assert(app_config.ringback_cnt>0);
1832 if (--app_config.ringback_cnt == 0 &&
1833 app_config.ringback_slot!=PJSUA_INVALID_ID)
1834 {
1835 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001836 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001837 }
1838 }
1839
1840 if (app_config.call_data[call_id].ring_on) {
1841 app_config.call_data[call_id].ring_on = PJ_FALSE;
1842
1843 pj_assert(app_config.ring_cnt>0);
1844 if (--app_config.ring_cnt == 0 &&
1845 app_config.ring_slot!=PJSUA_INVALID_ID)
1846 {
1847 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001848 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001849 }
1850 }
1851}
1852
1853static void ring_start(pjsua_call_id call_id)
1854{
1855 if (app_config.no_tones)
1856 return;
1857
1858 if (app_config.call_data[call_id].ring_on)
1859 return;
1860
1861 app_config.call_data[call_id].ring_on = PJ_TRUE;
1862
1863 if (++app_config.ring_cnt==1 &&
1864 app_config.ring_slot!=PJSUA_INVALID_ID)
1865 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001866 pjsua_conf_connect(app_config.ring_slot, 0);
1867 }
1868}
1869
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001870/*
1871 * Find next call when current call is disconnected or when user
1872 * press ']'
1873 */
1874static pj_bool_t find_next_call(void)
1875{
1876 int i, max;
1877
1878 max = pjsua_call_get_max_count();
1879 for (i=current_call+1; i<max; ++i) {
1880 if (pjsua_call_is_active(i)) {
1881 current_call = i;
1882 return PJ_TRUE;
1883 }
1884 }
1885
1886 for (i=0; i<current_call; ++i) {
1887 if (pjsua_call_is_active(i)) {
1888 current_call = i;
1889 return PJ_TRUE;
1890 }
1891 }
1892
1893 current_call = PJSUA_INVALID_ID;
1894 return PJ_FALSE;
1895}
1896
1897
1898/*
1899 * Find previous call when user press '['
1900 */
1901static pj_bool_t find_prev_call(void)
1902{
1903 int i, max;
1904
1905 max = pjsua_call_get_max_count();
1906 for (i=current_call-1; i>=0; --i) {
1907 if (pjsua_call_is_active(i)) {
1908 current_call = i;
1909 return PJ_TRUE;
1910 }
1911 }
1912
1913 for (i=max-1; i>current_call; --i) {
1914 if (pjsua_call_is_active(i)) {
1915 current_call = i;
1916 return PJ_TRUE;
1917 }
1918 }
1919
1920 current_call = PJSUA_INVALID_ID;
1921 return PJ_FALSE;
1922}
1923
1924
Benny Prijono804ff0a2006-09-14 11:17:48 +00001925/* Callback from timer when the maximum call duration has been
1926 * exceeded.
1927 */
1928static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1929 struct pj_timer_entry *entry)
1930{
1931 pjsua_call_id call_id = entry->id;
1932 pjsua_msg_data msg_data;
1933 pjsip_generic_string_hdr warn;
1934 pj_str_t hname = pj_str("Warning");
1935 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1936
1937 PJ_UNUSED_ARG(timer_heap);
1938
Benny Prijono148c9dd2006-09-19 13:37:53 +00001939 if (call_id == PJSUA_INVALID_ID) {
1940 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1941 return;
1942 }
1943
Benny Prijono804ff0a2006-09-14 11:17:48 +00001944 /* Add warning header */
1945 pjsua_msg_data_init(&msg_data);
1946 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1947 pj_list_push_back(&msg_data.hdr_list, &warn);
1948
1949 /* Call duration has been exceeded; disconnect the call */
1950 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1951 "for call %d, disconnecting the call",
1952 app_config.duration, call_id));
1953 entry->id = PJSUA_INVALID_ID;
1954 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1955}
1956
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001957
1958/*
1959 * Handler when invite state has changed.
1960 */
1961static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1962{
1963 pjsua_call_info call_info;
1964
1965 PJ_UNUSED_ARG(e);
1966
1967 pjsua_call_get_info(call_id, &call_info);
1968
1969 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1970
Benny Prijono91d20f42008-06-14 19:42:37 +00001971 /* Stop all ringback for this call */
1972 ring_stop(call_id);
1973
Benny Prijono804ff0a2006-09-14 11:17:48 +00001974 /* Cancel duration timer, if any */
1975 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1976 struct call_data *cd = &app_config.call_data[call_id];
1977 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1978
1979 cd->timer.id = PJSUA_INVALID_ID;
1980 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1981 }
1982
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00001983 /* Rewind play file when hangup automatically,
1984 * since file is not looped
1985 */
1986 if (app_config.auto_play_hangup)
1987 pjsua_player_set_pos(app_config.wav_id, 0);
1988
1989
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001990 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1991 call_id,
1992 call_info.last_status,
1993 call_info.last_status_text.ptr));
1994
1995 if (call_id == current_call) {
1996 find_next_call();
1997 }
1998
Benny Prijono4be63b52006-11-25 14:50:25 +00001999 /* Dump media state upon disconnected */
2000 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002001 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002002 "Call %d disconnected, dumping media stats..",
2003 call_id));
2004 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002005 }
2006
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002007 } else {
2008
Benny Prijono804ff0a2006-09-14 11:17:48 +00002009 if (app_config.duration!=NO_LIMIT &&
2010 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2011 {
2012 /* Schedule timer to hangup call after the specified duration */
2013 struct call_data *cd = &app_config.call_data[call_id];
2014 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2015 pj_time_val delay;
2016
2017 cd->timer.id = call_id;
2018 delay.sec = app_config.duration;
2019 delay.msec = 0;
2020 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2021 }
2022
Benny Prijono4be63b52006-11-25 14:50:25 +00002023 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2024 int code;
2025 pj_str_t reason;
2026 pjsip_msg *msg;
2027
2028 /* This can only occur because of TX or RX message */
2029 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2030
2031 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2032 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2033 } else {
2034 msg = e->body.tsx_state.src.tdata->msg;
2035 }
2036
2037 code = msg->line.status.code;
2038 reason = msg->line.status.reason;
2039
Benny Prijono91d20f42008-06-14 19:42:37 +00002040 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2041 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2042 msg->body == NULL &&
2043 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2044 {
2045 ringback_start(call_id);
2046 }
2047
Benny Prijono4be63b52006-11-25 14:50:25 +00002048 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2049 call_id, call_info.state_text.ptr,
2050 code, (int)reason.slen, reason.ptr));
2051 } else {
2052 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2053 call_id,
2054 call_info.state_text.ptr));
2055 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002056
2057 if (current_call==PJSUA_INVALID_ID)
2058 current_call = call_id;
2059
2060 }
2061}
2062
2063
2064/**
2065 * Handler when there is incoming call.
2066 */
2067static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2068 pjsip_rx_data *rdata)
2069{
2070 pjsua_call_info call_info;
2071
2072 PJ_UNUSED_ARG(acc_id);
2073 PJ_UNUSED_ARG(rdata);
2074
2075 pjsua_call_get_info(call_id, &call_info);
2076
Benny Prijono91d20f42008-06-14 19:42:37 +00002077 /* Start ringback */
2078 ring_start(call_id);
2079
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002080 if (app_config.auto_answer > 0) {
2081 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2082 }
2083
2084 if (app_config.auto_answer < 200) {
2085 PJ_LOG(3,(THIS_FILE,
2086 "Incoming call for account %d!\n"
2087 "From: %s\n"
2088 "To: %s\n"
2089 "Press a to answer or h to reject call",
2090 acc_id,
2091 call_info.remote_info.ptr,
2092 call_info.local_info.ptr));
2093 }
2094}
2095
2096
2097/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002098 * Handler when a transaction within a call has changed state.
2099 */
2100static void on_call_tsx_state(pjsua_call_id call_id,
2101 pjsip_transaction *tsx,
2102 pjsip_event *e)
2103{
2104 const pjsip_method info_method =
2105 {
2106 PJSIP_OTHER_METHOD,
2107 { "INFO", 4 }
2108 };
2109
2110 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2111 /*
2112 * Handle INFO method.
2113 */
2114 if (tsx->role == PJSIP_ROLE_UAC &&
2115 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002116 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2117 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002118 {
2119 /* Status of outgoing INFO request */
2120 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2121 PJ_LOG(4,(THIS_FILE,
2122 "Call %d: DTMF sent successfully with INFO",
2123 call_id));
2124 } else if (tsx->status_code >= 300) {
2125 PJ_LOG(4,(THIS_FILE,
2126 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2127 call_id,
2128 tsx->status_code,
2129 (int)tsx->status_text.slen,
2130 tsx->status_text.ptr));
2131 }
2132 } else if (tsx->role == PJSIP_ROLE_UAS &&
2133 tsx->state == PJSIP_TSX_STATE_TRYING)
2134 {
2135 /* Answer incoming INFO with 200/OK */
2136 pjsip_rx_data *rdata;
2137 pjsip_tx_data *tdata;
2138 pj_status_t status;
2139
2140 rdata = e->body.tsx_state.src.rdata;
2141
2142 if (rdata->msg_info.msg->body) {
2143 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2144 200, NULL, &tdata);
2145 if (status == PJ_SUCCESS)
2146 status = pjsip_tsx_send_msg(tsx, tdata);
2147
2148 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2149 call_id,
2150 (int)rdata->msg_info.msg->body->len,
2151 rdata->msg_info.msg->body->data));
2152 } else {
2153 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2154 400, NULL, &tdata);
2155 if (status == PJ_SUCCESS)
2156 status = pjsip_tsx_send_msg(tsx, tdata);
2157 }
2158 }
2159 }
2160}
2161
2162
2163/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002164 * Callback on media state changed event.
2165 * The action may connect the call to sound device, to file, or
2166 * to loop the call.
2167 */
2168static void on_call_media_state(pjsua_call_id call_id)
2169{
2170 pjsua_call_info call_info;
2171
2172 pjsua_call_get_info(call_id, &call_info);
2173
Benny Prijono91d20f42008-06-14 19:42:37 +00002174 /* Stop ringback */
2175 ring_stop(call_id);
2176
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002177 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
2178 pj_bool_t connect_sound = PJ_TRUE;
2179
2180 /* Loopback sound, if desired */
2181 if (app_config.auto_loop) {
2182 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2183 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00002184
2185 /* Automatically record conversation, if desired */
2186 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2187 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2188 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002189 }
2190
2191 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002192 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2193 app_config.wav_port != PJSUA_INVALID_ID)
2194 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002195 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2196 connect_sound = PJ_FALSE;
2197 }
2198
Benny Prijono7ca96da2006-08-07 12:11:40 +00002199 /* Put call in conference with other calls, if desired */
2200 if (app_config.auto_conf) {
2201 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002202 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002203 unsigned i;
2204
2205 /* Get all calls, and establish media connection between
2206 * this call and other calls.
2207 */
2208 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002209
Benny Prijono7ca96da2006-08-07 12:11:40 +00002210 for (i=0; i<call_cnt; ++i) {
2211 if (call_ids[i] == call_id)
2212 continue;
2213
2214 if (!pjsua_call_has_media(call_ids[i]))
2215 continue;
2216
2217 pjsua_conf_connect(call_info.conf_slot,
2218 pjsua_call_get_conf_port(call_ids[i]));
2219 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2220 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002221
2222 /* Automatically record conversation, if desired */
2223 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2224 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2225 app_config.rec_port);
2226 }
2227
Benny Prijono7ca96da2006-08-07 12:11:40 +00002228 }
2229
2230 /* Also connect call to local sound device */
2231 connect_sound = PJ_TRUE;
2232 }
2233
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002234 /* Otherwise connect to sound device */
2235 if (connect_sound) {
2236 pjsua_conf_connect(call_info.conf_slot, 0);
2237 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002238
2239 /* Automatically record conversation, if desired */
2240 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2241 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2242 pjsua_conf_connect(0, app_config.rec_port);
2243 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002244 }
2245
2246 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
2247
2248 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
2249 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2250 call_id));
2251 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
2252 PJ_LOG(3,(THIS_FILE,
2253 "Media for call %d is suspended (hold) by remote",
2254 call_id));
Benny Prijono096c56c2007-09-15 08:30:16 +00002255 } else if (call_info.media_status == PJSUA_CALL_MEDIA_ERROR) {
2256 pj_str_t reason = pj_str("ICE negotiation failed");
2257
2258 PJ_LOG(1,(THIS_FILE,
2259 "Media has reported error, disconnecting call"));
2260
2261 pjsua_call_hangup(call_id, 500, &reason, NULL);
2262
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002263 } else {
2264 PJ_LOG(3,(THIS_FILE,
2265 "Media for call %d is inactive",
2266 call_id));
2267 }
2268}
2269
Benny Prijono0875ae82006-12-26 00:11:48 +00002270/*
2271 * DTMF callback.
2272 */
2273static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2274{
2275 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2276}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002277
2278/*
2279 * Handler registration status has changed.
2280 */
2281static void on_reg_state(pjsua_acc_id acc_id)
2282{
2283 PJ_UNUSED_ARG(acc_id);
2284
2285 // Log already written.
2286}
2287
2288
2289/*
2290 * Handler on buddy state changed.
2291 */
2292static void on_buddy_state(pjsua_buddy_id buddy_id)
2293{
2294 pjsua_buddy_info info;
2295 pjsua_buddy_get_info(buddy_id, &info);
2296
2297 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2298 (int)info.uri.slen,
2299 info.uri.ptr,
2300 (int)info.status_text.slen,
2301 info.status_text.ptr));
2302}
2303
2304
2305/**
2306 * Incoming IM message (i.e. MESSAGE request)!
2307 */
2308static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2309 const pj_str_t *to, const pj_str_t *contact,
2310 const pj_str_t *mime_type, const pj_str_t *text)
2311{
2312 /* Note: call index may be -1 */
2313 PJ_UNUSED_ARG(call_id);
2314 PJ_UNUSED_ARG(to);
2315 PJ_UNUSED_ARG(contact);
2316 PJ_UNUSED_ARG(mime_type);
2317
Benny Prijonof4b538d2007-05-14 16:45:20 +00002318 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002319 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002320 (int)text->slen, text->ptr,
2321 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002322}
2323
2324
2325/**
2326 * Received typing indication
2327 */
2328static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2329 const pj_str_t *to, const pj_str_t *contact,
2330 pj_bool_t is_typing)
2331{
2332 PJ_UNUSED_ARG(call_id);
2333 PJ_UNUSED_ARG(to);
2334 PJ_UNUSED_ARG(contact);
2335
2336 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2337 (int)from->slen, from->ptr,
2338 (is_typing?"is typing..":"has stopped typing")));
2339}
2340
2341
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002342/**
2343 * Call transfer request status.
2344 */
2345static void on_call_transfer_status(pjsua_call_id call_id,
2346 int status_code,
2347 const pj_str_t *status_text,
2348 pj_bool_t final,
2349 pj_bool_t *p_cont)
2350{
2351 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2352 call_id, status_code,
2353 (int)status_text->slen, status_text->ptr,
2354 (final ? "[final]" : "")));
2355
2356 if (status_code/100 == 2) {
2357 PJ_LOG(3,(THIS_FILE,
2358 "Call %d: call transfered successfully, disconnecting call",
2359 call_id));
2360 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2361 *p_cont = PJ_FALSE;
2362 }
2363}
2364
2365
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002366/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002367 * Notification that call is being replaced.
2368 */
2369static void on_call_replaced(pjsua_call_id old_call_id,
2370 pjsua_call_id new_call_id)
2371{
2372 pjsua_call_info old_ci, new_ci;
2373
2374 pjsua_call_get_info(old_call_id, &old_ci);
2375 pjsua_call_get_info(new_call_id, &new_ci);
2376
2377 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2378 "call %d with %.*s",
2379 old_call_id,
2380 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2381 new_call_id,
2382 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2383}
2384
2385
2386/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002387 * NAT type detection callback.
2388 */
2389static void on_nat_detect(const pj_stun_nat_detect_result *res)
2390{
2391 if (res->status != PJ_SUCCESS) {
2392 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2393 } else {
2394 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2395 }
2396}
2397
2398
2399/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002400 * Print buddy list.
2401 */
2402static void print_buddy_list(void)
2403{
2404 pjsua_buddy_id ids[64];
2405 int i;
2406 unsigned count = PJ_ARRAY_SIZE(ids);
2407
2408 puts("Buddy list:");
2409
2410 pjsua_enum_buddies(ids, &count);
2411
2412 if (count == 0)
2413 puts(" -none-");
2414 else {
2415 for (i=0; i<(int)count; ++i) {
2416 pjsua_buddy_info info;
2417
2418 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2419 continue;
2420
Benny Prijono4461c7d2007-08-25 13:36:15 +00002421 printf(" [%2d] <%.*s> %.*s\n",
2422 ids[i]+1,
2423 (int)info.status_text.slen,
2424 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002425 (int)info.uri.slen,
2426 info.uri.ptr);
2427 }
2428 }
2429 puts("");
2430}
2431
2432
2433/*
2434 * Print account status.
2435 */
2436static void print_acc_status(int acc_id)
2437{
2438 char buf[80];
2439 pjsua_acc_info info;
2440
2441 pjsua_acc_get_info(acc_id, &info);
2442
2443 if (!info.has_registration) {
2444 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2445 (int)info.status_text.slen,
2446 info.status_text.ptr);
2447
2448 } else {
2449 pj_ansi_snprintf(buf, sizeof(buf),
2450 "%d/%.*s (expires=%d)",
2451 info.status,
2452 (int)info.status_text.slen,
2453 info.status_text.ptr,
2454 info.expires);
2455
2456 }
2457
2458 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2459 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002460 printf(" Online status: %.*s\n",
2461 (int)info.online_status_text.slen,
2462 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002463}
2464
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002465/* Playfile done notification, set timer to hangup calls */
2466pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2467{
2468 pj_time_val delay;
2469
2470 PJ_UNUSED_ARG(port);
2471 PJ_UNUSED_ARG(usr_data);
2472
2473 /* Just rewind WAV when it is played outside of call */
2474 if (pjsua_call_get_count() == 0) {
2475 pjsua_player_set_pos(app_config.wav_id, 0);
2476 return PJ_SUCCESS;
2477 }
2478
2479 /* Timer is already active */
2480 if (app_config.auto_hangup_timer.id == 1)
2481 return PJ_SUCCESS;
2482
2483 app_config.auto_hangup_timer.id = 1;
2484 delay.sec = 0;
2485 delay.msec = 200; /* Give 200 ms before hangup */
2486 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2487 &app_config.auto_hangup_timer,
2488 &delay);
2489
2490 return PJ_SUCCESS;
2491}
2492
2493/* Auto hangup timer callback */
2494static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2495 struct pj_timer_entry *entry)
2496{
2497 PJ_UNUSED_ARG(timer_heap);
2498 PJ_UNUSED_ARG(entry);
2499
2500 app_config.auto_hangup_timer.id = 0;
2501 pjsua_call_hangup_all();
2502}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002503
2504/*
2505 * Show a bit of help.
2506 */
2507static void keystroke_help(void)
2508{
2509 pjsua_acc_id acc_ids[16];
2510 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2511 int i;
2512
2513 printf(">>>>\n");
2514
2515 pjsua_enum_accs(acc_ids, &count);
2516
2517 printf("Account list:\n");
2518 for (i=0; i<(int)count; ++i)
2519 print_acc_status(acc_ids[i]);
2520
2521 print_buddy_list();
2522
2523 //puts("Commands:");
2524 puts("+=============================================================================+");
2525 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2526 puts("| | | |");
2527 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2528 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002529 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2530 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2531 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2532 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002533 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2534 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002535 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002536 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002537 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2538 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2539 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2540 puts("| | V Adjust audio Volume | f Save config |");
2541 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002542 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002543 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002544 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002545
2546 i = pjsua_call_get_count();
2547 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002548
2549 if (current_call != PJSUA_INVALID_ID) {
2550 pjsua_call_info ci;
2551 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2552 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2553 (int)ci.remote_info.slen, ci.remote_info.ptr,
2554 (int)ci.state_text.slen, ci.state_text.ptr);
2555 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002556}
2557
2558
2559/*
2560 * Input simple string
2561 */
2562static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2563{
2564 char *p;
2565
2566 printf("%s (empty to cancel): ", title); fflush(stdout);
2567 fgets(buf, len, stdin);
2568
2569 /* Remove trailing newlines. */
2570 for (p=buf; ; ++p) {
2571 if (*p=='\r' || *p=='\n') *p='\0';
2572 else if (!*p) break;
2573 }
2574
2575 if (!*buf)
2576 return PJ_FALSE;
2577
2578 return PJ_TRUE;
2579}
2580
2581
2582#define NO_NB -2
2583struct input_result
2584{
2585 int nb_result;
2586 char *uri_result;
2587};
2588
2589
2590/*
2591 * Input URL.
2592 */
2593static void ui_input_url(const char *title, char *buf, int len,
2594 struct input_result *result)
2595{
2596 result->nb_result = NO_NB;
2597 result->uri_result = NULL;
2598
2599 print_buddy_list();
2600
2601 printf("Choices:\n"
2602 " 0 For current dialog.\n"
2603 " -1 All %d buddies in buddy list\n"
2604 " [1 -%2d] Select from buddy list\n"
2605 " URL An URL\n"
2606 " <Enter> Empty input (or 'q') to cancel\n"
2607 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2608 printf("%s: ", title);
2609
2610 fflush(stdout);
2611 fgets(buf, len, stdin);
2612 len = strlen(buf);
2613
2614 /* Left trim */
2615 while (pj_isspace(*buf)) {
2616 ++buf;
2617 --len;
2618 }
2619
2620 /* Remove trailing newlines */
2621 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2622 buf[--len] = '\0';
2623
2624 if (len == 0 || buf[0]=='q')
2625 return;
2626
2627 if (pj_isdigit(*buf) || *buf=='-') {
2628
2629 int i;
2630
2631 if (*buf=='-')
2632 i = 1;
2633 else
2634 i = 0;
2635
2636 for (; i<len; ++i) {
2637 if (!pj_isdigit(buf[i])) {
2638 puts("Invalid input");
2639 return;
2640 }
2641 }
2642
2643 result->nb_result = my_atoi(buf);
2644
2645 if (result->nb_result >= 0 &&
2646 result->nb_result <= (int)pjsua_get_buddy_count())
2647 {
2648 return;
2649 }
2650 if (result->nb_result == -1)
2651 return;
2652
2653 puts("Invalid input");
2654 result->nb_result = NO_NB;
2655 return;
2656
2657 } else {
2658 pj_status_t status;
2659
2660 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2661 pjsua_perror(THIS_FILE, "Invalid URL", status);
2662 return;
2663 }
2664
2665 result->uri_result = buf;
2666 }
2667}
2668
2669/*
2670 * List the ports in conference bridge
2671 */
2672static void conf_list(void)
2673{
2674 unsigned i, count;
2675 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2676
2677 printf("Conference ports:\n");
2678
2679 count = PJ_ARRAY_SIZE(id);
2680 pjsua_enum_conf_ports(id, &count);
2681
2682 for (i=0; i<count; ++i) {
2683 char txlist[PJSUA_MAX_CALLS*4+10];
2684 unsigned j;
2685 pjsua_conf_port_info info;
2686
2687 pjsua_conf_get_port_info(id[i], &info);
2688
2689 txlist[0] = '\0';
2690 for (j=0; j<info.listener_cnt; ++j) {
2691 char s[10];
2692 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2693 pj_ansi_strcat(txlist, s);
2694 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002695 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002696 info.slot_id,
2697 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002698 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002699 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002700 (int)info.name.slen,
2701 info.name.ptr,
2702 txlist);
2703
2704 }
2705 puts("");
2706}
2707
2708
2709/*
Benny Prijono56315612006-07-18 14:39:40 +00002710 * Send arbitrary request to remote host
2711 */
2712static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2713{
2714 pj_str_t str_method;
2715 pjsip_method method;
2716 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002717 pjsip_endpoint *endpt;
2718 pj_status_t status;
2719
2720 endpt = pjsua_get_pjsip_endpt();
2721
2722 str_method = pj_str(cstr_method);
2723 pjsip_method_init_np(&method, &str_method);
2724
Benny Prijonofff245c2007-04-02 11:44:47 +00002725 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002726
Benny Prijonob988d762007-12-05 04:30:04 +00002727 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002728 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002729 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002730 return;
2731 }
2732}
2733
2734
2735/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002736 * Change extended online status.
2737 */
2738static void change_online_status(void)
2739{
2740 char menuin[32];
2741 pj_bool_t online_status;
2742 pjrpid_element elem;
2743 int i, choice;
2744
2745 enum {
2746 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2747 };
2748
2749 struct opt {
2750 int id;
2751 char *name;
2752 } opts[] = {
2753 { AVAILABLE, "Available" },
2754 { BUSY, "Busy"},
2755 { OTP, "On the phone"},
2756 { IDLE, "Idle"},
2757 { AWAY, "Away"},
2758 { BRB, "Be right back"},
2759 { OFFLINE, "Offline"}
2760 };
2761
2762 printf("\n"
2763 "Choices:\n");
2764 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2765 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2766 }
2767
2768 if (!simple_input("Select status", menuin, sizeof(menuin)))
2769 return;
2770
2771 choice = atoi(menuin) - 1;
2772 if (choice < 0 || choice >= OPT_MAX) {
2773 puts("Invalid selection");
2774 return;
2775 }
2776
2777 pj_bzero(&elem, sizeof(elem));
2778 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2779
2780 online_status = PJ_TRUE;
2781
2782 switch (choice) {
2783 case AVAILABLE:
2784 break;
2785 case BUSY:
2786 elem.activity = PJRPID_ACTIVITY_BUSY;
2787 elem.note = pj_str("Busy");
2788 break;
2789 case OTP:
2790 elem.activity = PJRPID_ACTIVITY_BUSY;
2791 elem.note = pj_str("On the phone");
2792 break;
2793 case IDLE:
2794 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2795 elem.note = pj_str("Idle");
2796 break;
2797 case AWAY:
2798 elem.activity = PJRPID_ACTIVITY_AWAY;
2799 elem.note = pj_str("Away");
2800 break;
2801 case BRB:
2802 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2803 elem.note = pj_str("Be right back");
2804 break;
2805 case OFFLINE:
2806 online_status = PJ_FALSE;
2807 break;
2808 }
2809
2810 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2811}
2812
2813
2814/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002815 * Change codec priorities.
2816 */
2817static void manage_codec_prio(void)
2818{
2819 pjsua_codec_info c[32];
2820 unsigned i, count = PJ_ARRAY_SIZE(c);
2821 char input[32];
2822 char *codec, *prio;
2823 pj_str_t id;
2824 int new_prio;
2825 pj_status_t status;
2826
2827 printf("List of codecs:\n");
2828
2829 pjsua_enum_codecs(c, &count);
2830 for (i=0; i<count; ++i) {
2831 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2832 c[i].codec_id.ptr);
2833 }
2834
2835 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00002836 puts("Enter codec id and its new priority "
2837 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002838
Benny Prijono88accae2008-06-26 15:48:14 +00002839 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002840 fgets(input, sizeof(input), stdin);
2841 if (input[0]=='\r' || input[0]=='\n') {
2842 puts("Done");
2843 return;
2844 }
2845
2846 codec = strtok(input, " \t\r\n");
2847 prio = strtok(NULL, " \r\n");
2848
2849 if (!codec || !prio) {
2850 puts("Invalid input");
2851 return;
2852 }
2853
2854 new_prio = atoi(prio);
2855 if (new_prio < 0)
2856 new_prio = 0;
2857 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2858 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2859
2860 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2861 (pj_uint8_t)new_prio);
2862 if (status != PJ_SUCCESS)
2863 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2864}
2865
2866
2867/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002868 * Main "user interface" loop.
2869 */
2870void console_app_main(const pj_str_t *uri_to_call)
2871{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002872 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002873 char buf[128];
2874 char text[128];
2875 int i, count;
2876 char *uri;
2877 pj_str_t tmp;
2878 struct input_result result;
2879 pjsua_call_info call_info;
2880 pjsua_acc_info acc_info;
2881
2882
2883 /* If user specifies URI to call, then call the URI */
2884 if (uri_to_call->slen) {
2885 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2886 }
2887
2888 keystroke_help();
2889
2890 for (;;) {
2891
2892 printf(">>> ");
2893 fflush(stdout);
2894
Benny Prijono990042e2007-01-21 19:36:00 +00002895 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2896 /*
2897 * Be friendly to users who redirect commands into
2898 * program, when file ends, resume with kbd.
2899 * If exit is desired end script with q for quit
2900 */
2901 /* Reopen stdin/stdout/stderr to /dev/console */
2902#if defined(PJ_WIN32) && PJ_WIN32!=0
2903 if (freopen ("CONIN$", "r", stdin) == NULL) {
2904#else
2905 if (1) {
2906#endif
2907 puts("Cannot switch back to console from file redirection");
2908 menuin[0] = 'q';
2909 menuin[1] = '\0';
2910 } else {
2911 puts("Switched back to console from file redirection");
2912 continue;
2913 }
2914 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002915
Benny Prijonoebc32c32008-06-12 13:30:39 +00002916 if (cmd_echo) {
2917 printf("%s", menuin);
2918 }
2919
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002920 switch (menuin[0]) {
2921
2922 case 'm':
2923 /* Make call! : */
2924 printf("(You currently have %d calls)\n",
2925 pjsua_call_get_count());
2926
2927 uri = NULL;
2928 ui_input_url("Make call", buf, sizeof(buf), &result);
2929 if (result.nb_result != NO_NB) {
2930
2931 if (result.nb_result == -1 || result.nb_result == 0) {
2932 puts("You can't do that with make call!");
2933 continue;
2934 } else {
2935 pjsua_buddy_info binfo;
2936 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2937 uri = binfo.uri.ptr;
2938 }
2939
2940 } else if (result.uri_result) {
2941 uri = result.uri_result;
2942 }
2943
2944 tmp = pj_str(uri);
2945 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2946 break;
2947
2948 case 'M':
2949 /* Make multiple calls! : */
2950 printf("(You currently have %d calls)\n",
2951 pjsua_call_get_count());
2952
2953 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2954 continue;
2955
2956 count = my_atoi(menuin);
2957 if (count < 1)
2958 continue;
2959
2960 ui_input_url("Make call", buf, sizeof(buf), &result);
2961 if (result.nb_result != NO_NB) {
2962 pjsua_buddy_info binfo;
2963 if (result.nb_result == -1 || result.nb_result == 0) {
2964 puts("You can't do that with make call!");
2965 continue;
2966 }
2967 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2968 uri = binfo.uri.ptr;
2969 } else {
2970 uri = result.uri_result;
2971 }
2972
2973 for (i=0; i<my_atoi(menuin); ++i) {
2974 pj_status_t status;
2975
2976 tmp = pj_str(uri);
2977 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2978 NULL, NULL);
2979 if (status != PJ_SUCCESS)
2980 break;
2981 }
2982 break;
2983
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002984 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00002985 i = pjsua_detect_nat_type();
2986 if (i != PJ_SUCCESS)
2987 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002988 break;
2989
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002990 case 'i':
2991 /* Send instant messaeg */
2992
2993 /* i is for call index to send message, if any */
2994 i = -1;
2995
2996 /* Make compiler happy. */
2997 uri = NULL;
2998
2999 /* Input destination. */
3000 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3001 if (result.nb_result != NO_NB) {
3002
3003 if (result.nb_result == -1) {
3004 puts("You can't send broadcast IM like that!");
3005 continue;
3006
3007 } else if (result.nb_result == 0) {
3008
3009 i = current_call;
3010
3011 } else {
3012 pjsua_buddy_info binfo;
3013 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3014 uri = binfo.uri.ptr;
3015 }
3016
3017 } else if (result.uri_result) {
3018 uri = result.uri_result;
3019 }
3020
3021
3022 /* Send typing indication. */
3023 if (i != -1)
3024 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3025 else {
3026 pj_str_t tmp_uri = pj_str(uri);
3027 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3028 }
3029
3030 /* Input the IM . */
3031 if (!simple_input("Message", text, sizeof(text))) {
3032 /*
3033 * Cancelled.
3034 * Send typing notification too, saying we're not typing.
3035 */
3036 if (i != -1)
3037 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3038 else {
3039 pj_str_t tmp_uri = pj_str(uri);
3040 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3041 }
3042 continue;
3043 }
3044
3045 tmp = pj_str(text);
3046
3047 /* Send the IM */
3048 if (i != -1)
3049 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3050 else {
3051 pj_str_t tmp_uri = pj_str(uri);
3052 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3053 }
3054
3055 break;
3056
3057 case 'a':
3058
3059 if (current_call != -1) {
3060 pjsua_call_get_info(current_call, &call_info);
3061 } else {
3062 /* Make compiler happy */
3063 call_info.role = PJSIP_ROLE_UAC;
3064 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3065 }
3066
3067 if (current_call == -1 ||
3068 call_info.role != PJSIP_ROLE_UAS ||
3069 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3070 {
3071 puts("No pending incoming call");
3072 fflush(stdout);
3073 continue;
3074
3075 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003076 int st_code;
3077 char contact[120];
3078 pj_str_t hname = { "Contact", 7 };
3079 pj_str_t hvalue;
3080 pjsip_generic_string_hdr hcontact;
3081 pjsua_msg_data msg_data;
3082
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003083 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3084 continue;
3085
Benny Prijono20d36722007-02-22 14:52:24 +00003086 st_code = my_atoi(buf);
3087 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003088 continue;
3089
Benny Prijono20d36722007-02-22 14:52:24 +00003090 pjsua_msg_data_init(&msg_data);
3091
3092 if (st_code/100 == 3) {
3093 if (!simple_input("Enter URL to be put in Contact",
3094 contact, sizeof(contact)))
3095 continue;
3096 hvalue = pj_str(contact);
3097 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3098
3099 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3100 }
3101
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003102 /*
3103 * Must check again!
3104 * Call may have been disconnected while we're waiting for
3105 * keyboard input.
3106 */
3107 if (current_call == -1) {
3108 puts("Call has been disconnected");
3109 fflush(stdout);
3110 continue;
3111 }
3112
Benny Prijono20d36722007-02-22 14:52:24 +00003113 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003114 }
3115
3116 break;
3117
3118
3119 case 'h':
3120
3121 if (current_call == -1) {
3122 puts("No current call");
3123 fflush(stdout);
3124 continue;
3125
3126 } else if (menuin[1] == 'a') {
3127
3128 /* Hangup all calls */
3129 pjsua_call_hangup_all();
3130
3131 } else {
3132
3133 /* Hangup current calls */
3134 pjsua_call_hangup(current_call, 0, NULL, NULL);
3135 }
3136 break;
3137
3138 case ']':
3139 case '[':
3140 /*
3141 * Cycle next/prev dialog.
3142 */
3143 if (menuin[0] == ']') {
3144 find_next_call();
3145
3146 } else {
3147 find_prev_call();
3148 }
3149
3150 if (current_call != -1) {
3151
3152 pjsua_call_get_info(current_call, &call_info);
3153 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3154 (int)call_info.remote_info.slen,
3155 call_info.remote_info.ptr));
3156
3157 } else {
3158 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3159 }
3160 break;
3161
3162
3163 case '>':
3164 case '<':
3165 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3166 break;
3167
3168 i = my_atoi(buf);
3169 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003170 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003171 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3172 } else {
3173 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3174 }
3175 break;
3176
3177
3178 case '+':
3179 if (menuin[1] == 'b') {
3180
3181 pjsua_buddy_config buddy_cfg;
3182 pjsua_buddy_id buddy_id;
3183 pj_status_t status;
3184
3185 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3186 break;
3187
3188 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3189 printf("Invalid SIP URI '%s'\n", buf);
3190 break;
3191 }
3192
Benny Prijonoac623b32006-07-03 15:19:31 +00003193 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003194
3195 buddy_cfg.uri = pj_str(buf);
3196 buddy_cfg.subscribe = PJ_TRUE;
3197
3198 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3199 if (status == PJ_SUCCESS) {
3200 printf("New buddy '%s' added at index %d\n",
3201 buf, buddy_id+1);
3202 }
3203
3204 } else if (menuin[1] == 'a') {
3205
Benny Prijonofb2b3652007-06-28 07:15:03 +00003206 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3207 pjsua_acc_config acc_cfg;
3208 pj_status_t status;
3209
3210 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3211 break;
3212 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3213 break;
3214 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3215 break;
3216 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3217 break;
3218 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3219 break;
3220
3221 pjsua_acc_config_default(&acc_cfg);
3222 acc_cfg.id = pj_str(id);
3223 acc_cfg.reg_uri = pj_str(registrar);
3224 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003225 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003226 acc_cfg.cred_info[0].realm = pj_str(realm);
3227 acc_cfg.cred_info[0].username = pj_str(uname);
3228 acc_cfg.cred_info[0].data_type = 0;
3229 acc_cfg.cred_info[0].data = pj_str(passwd);
3230
3231 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3232 if (status != PJ_SUCCESS) {
3233 pjsua_perror(THIS_FILE, "Error adding new account", status);
3234 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003235
3236 } else {
3237 printf("Invalid input %s\n", menuin);
3238 }
3239 break;
3240
3241 case '-':
3242 if (menuin[1] == 'b') {
3243 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3244 break;
3245
3246 i = my_atoi(buf) - 1;
3247
3248 if (!pjsua_buddy_is_valid(i)) {
3249 printf("Invalid buddy id %d\n", i);
3250 } else {
3251 pjsua_buddy_del(i);
3252 printf("Buddy %d deleted\n", i);
3253 }
3254
3255 } else if (menuin[1] == 'a') {
3256
3257 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3258 break;
3259
3260 i = my_atoi(buf);
3261
3262 if (!pjsua_acc_is_valid(i)) {
3263 printf("Invalid account id %d\n", i);
3264 } else {
3265 pjsua_acc_del(i);
3266 printf("Account %d deleted\n", i);
3267 }
3268
3269 } else {
3270 printf("Invalid input %s\n", menuin);
3271 }
3272 break;
3273
3274 case 'H':
3275 /*
3276 * Hold call.
3277 */
3278 if (current_call != -1) {
3279
3280 pjsua_call_set_hold(current_call, NULL);
3281
3282 } else {
3283 PJ_LOG(3,(THIS_FILE, "No current call"));
3284 }
3285 break;
3286
3287 case 'v':
3288 /*
3289 * Send re-INVITE (to release hold, etc).
3290 */
3291 if (current_call != -1) {
3292
3293 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3294
3295 } else {
3296 PJ_LOG(3,(THIS_FILE, "No current call"));
3297 }
3298 break;
3299
Benny Prijonoc08682e2007-10-04 06:17:58 +00003300 case 'U':
3301 /*
3302 * Send UPDATE
3303 */
3304 if (current_call != -1) {
3305
3306 pjsua_call_update(current_call, 0, NULL);
3307
3308 } else {
3309 PJ_LOG(3,(THIS_FILE, "No current call"));
3310 }
3311 break;
3312
3313 case 'C':
3314 if (menuin[1] == 'p') {
3315 manage_codec_prio();
3316 }
3317 break;
3318
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003319 case 'x':
3320 /*
3321 * Transfer call.
3322 */
3323 if (current_call == -1) {
3324
3325 PJ_LOG(3,(THIS_FILE, "No current call"));
3326
3327 } else {
3328 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003329 pjsua_msg_data msg_data;
3330 pjsip_generic_string_hdr refer_sub;
3331 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3332 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003333 pjsua_call_info ci;
3334
3335 pjsua_call_get_info(current_call, &ci);
3336 printf("Transfering current call [%d] %.*s\n",
3337 current_call,
3338 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003339
3340 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3341
3342 /* Check if call is still there. */
3343
3344 if (call != current_call) {
3345 puts("Call has been disconnected");
3346 continue;
3347 }
3348
Benny Prijonod524e822006-09-22 12:48:18 +00003349 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003350 if (app_config.no_refersub) {
3351 /* Add Refer-Sub: false in outgoing REFER request */
3352 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3353 &STR_FALSE);
3354 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3355 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003356 if (result.nb_result != NO_NB) {
3357 if (result.nb_result == -1 || result.nb_result == 0)
3358 puts("You can't do that with transfer call!");
3359 else {
3360 pjsua_buddy_info binfo;
3361 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003362 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003363 }
3364
3365 } else if (result.uri_result) {
3366 pj_str_t tmp;
3367 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003368 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003369 }
3370 }
3371 break;
3372
Benny Prijonof7b1c392006-11-11 16:46:34 +00003373 case 'X':
3374 /*
3375 * Transfer call with replaces.
3376 */
3377 if (current_call == -1) {
3378
3379 PJ_LOG(3,(THIS_FILE, "No current call"));
3380
3381 } else {
3382 int call = current_call;
3383 int dst_call;
3384 pjsua_msg_data msg_data;
3385 pjsip_generic_string_hdr refer_sub;
3386 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3387 pj_str_t STR_FALSE = { "false", 5 };
3388 pjsua_call_id ids[PJSUA_MAX_CALLS];
3389 pjsua_call_info ci;
3390 unsigned i, count;
3391
3392 count = PJ_ARRAY_SIZE(ids);
3393 pjsua_enum_calls(ids, &count);
3394
3395 if (count <= 1) {
3396 puts("There are no other calls");
3397 continue;
3398 }
3399
3400 pjsua_call_get_info(current_call, &ci);
3401 printf("Transfer call [%d] %.*s to one of the following:\n",
3402 current_call,
3403 (int)ci.remote_info.slen, ci.remote_info.ptr);
3404
3405 for (i=0; i<count; ++i) {
3406 pjsua_call_info call_info;
3407
3408 if (ids[i] == call)
3409 continue;
3410
3411 pjsua_call_get_info(ids[i], &call_info);
3412 printf("%d %.*s [%.*s]\n",
3413 ids[i],
3414 (int)call_info.remote_info.slen,
3415 call_info.remote_info.ptr,
3416 (int)call_info.state_text.slen,
3417 call_info.state_text.ptr);
3418 }
3419
3420 if (!simple_input("Enter call number to be replaced",
3421 buf, sizeof(buf)))
3422 continue;
3423
3424 dst_call = my_atoi(buf);
3425
3426 /* Check if call is still there. */
3427
3428 if (call != current_call) {
3429 puts("Call has been disconnected");
3430 continue;
3431 }
3432
3433 /* Check that destination call is valid. */
3434 if (dst_call == call) {
3435 puts("Destination call number must not be the same "
3436 "as the call being transfered");
3437 continue;
3438 }
3439 if (dst_call >= PJSUA_MAX_CALLS) {
3440 puts("Invalid destination call number");
3441 continue;
3442 }
3443 if (!pjsua_call_is_active(dst_call)) {
3444 puts("Invalid destination call number");
3445 continue;
3446 }
3447
3448 pjsua_msg_data_init(&msg_data);
3449 if (app_config.no_refersub) {
3450 /* Add Refer-Sub: false in outgoing REFER request */
3451 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3452 &STR_FALSE);
3453 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3454 }
3455
3456 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3457 }
3458 break;
3459
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003460 case '#':
3461 /*
3462 * Send DTMF strings.
3463 */
3464 if (current_call == -1) {
3465
3466 PJ_LOG(3,(THIS_FILE, "No current call"));
3467
3468 } else if (!pjsua_call_has_media(current_call)) {
3469
3470 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3471
3472 } else {
3473 pj_str_t digits;
3474 int call = current_call;
3475 pj_status_t status;
3476
3477 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3478 sizeof(buf)))
3479 {
3480 break;
3481 }
3482
3483 if (call != current_call) {
3484 puts("Call has been disconnected");
3485 continue;
3486 }
3487
3488 digits = pj_str(buf);
3489 status = pjsua_call_dial_dtmf(current_call, &digits);
3490 if (status != PJ_SUCCESS) {
3491 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3492 } else {
3493 puts("DTMF digits enqueued for transmission");
3494 }
3495 }
3496 break;
3497
Benny Prijonofeb69f42007-10-05 09:12:26 +00003498 case '*':
3499 /* Send DTMF with INFO */
3500 if (current_call == -1) {
3501
3502 PJ_LOG(3,(THIS_FILE, "No current call"));
3503
3504 } else {
3505 const pj_str_t SIP_INFO = pj_str("INFO");
3506 pj_str_t digits;
3507 int call = current_call;
3508 int i;
3509 pj_status_t status;
3510
3511 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3512 sizeof(buf)))
3513 {
3514 break;
3515 }
3516
3517 if (call != current_call) {
3518 puts("Call has been disconnected");
3519 continue;
3520 }
3521
3522 digits = pj_str(buf);
3523 for (i=0; i<digits.slen; ++i) {
3524 pjsua_msg_data msg_data;
3525 char body[80];
3526
3527 pjsua_msg_data_init(&msg_data);
3528 msg_data.content_type = pj_str("application/dtmf-relay");
3529
3530 pj_ansi_snprintf(body, sizeof(body),
3531 "Signal=%c\r\n"
3532 "Duration=160",
3533 buf[i]);
3534 msg_data.msg_body = pj_str(body);
3535
3536 status = pjsua_call_send_request(current_call, &SIP_INFO,
3537 &msg_data);
3538 if (status != PJ_SUCCESS) {
3539 break;
3540 }
3541 }
3542 }
3543 break;
3544
Benny Prijono56315612006-07-18 14:39:40 +00003545 case 'S':
3546 /*
3547 * Send arbitrary request
3548 */
3549 if (pjsua_acc_get_count() == 0) {
3550 puts("Sorry, need at least one account configured");
3551 break;
3552 }
3553
3554 puts("Send arbitrary request to remote host");
3555
3556 /* Input METHOD */
3557 if (!simple_input("Request method:",text,sizeof(text)))
3558 break;
3559
3560 /* Input destination URI */
3561 uri = NULL;
3562 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3563 if (result.nb_result != NO_NB) {
3564
3565 if (result.nb_result == -1 || result.nb_result == 0) {
3566 puts("Sorry you can't do that!");
3567 continue;
3568 } else {
3569 pjsua_buddy_info binfo;
3570 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3571 uri = binfo.uri.ptr;
3572 }
3573
3574 } else if (result.uri_result) {
3575 uri = result.uri_result;
3576 }
3577
3578 tmp = pj_str(uri);
3579
3580 send_request(text, &tmp);
3581 break;
3582
Benny Prijonoebc32c32008-06-12 13:30:39 +00003583 case 'e':
3584 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3585 pj_str_t tmp;
3586
3587 tmp.ptr = menuin+5;
3588 tmp.slen = pj_ansi_strlen(menuin)-6;
3589
3590 if (tmp.slen < 1) {
3591 puts("Usage: echo [0|1]");
3592 break;
3593 }
3594
Benny Prijonoddd02de2008-06-26 22:20:11 +00003595 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003596 }
3597 break;
3598
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003599 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003600 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3601 pj_str_t tmp;
3602 int delay;
3603
3604 tmp.ptr = menuin+6;
3605 tmp.slen = pj_ansi_strlen(menuin)-7;
3606
3607 if (tmp.slen < 1) {
3608 puts("Usage: sleep MSEC");
3609 break;
3610 }
3611
3612 delay = pj_strtoul(&tmp);
3613 if (delay < 0) delay = 0;
3614 pj_thread_sleep(delay);
3615 break;
3616 }
3617 /* Continue below */
3618
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003619 case 'u':
3620 /*
3621 * Subscribe/unsubscribe presence.
3622 */
3623 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3624 if (result.nb_result != NO_NB) {
3625 if (result.nb_result == -1) {
3626 int i, count;
3627 count = pjsua_get_buddy_count();
3628 for (i=0; i<count; ++i)
3629 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3630 } else if (result.nb_result == 0) {
3631 puts("Sorry, can only subscribe to buddy's presence, "
3632 "not from existing call");
3633 } else {
3634 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3635 }
3636
3637 } else if (result.uri_result) {
3638 puts("Sorry, can only subscribe to buddy's presence, "
3639 "not arbitrary URL (for now)");
3640 }
3641
3642 break;
3643
3644 case 'r':
3645 switch (menuin[1]) {
3646 case 'r':
3647 /*
3648 * Re-Register.
3649 */
3650 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3651 break;
3652 case 'u':
3653 /*
3654 * Unregister
3655 */
3656 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3657 break;
3658 }
3659 break;
3660
3661 case 't':
3662 pjsua_acc_get_info(current_acc, &acc_info);
3663 acc_info.online_status = !acc_info.online_status;
3664 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3665 printf("Setting %s online status to %s\n",
3666 acc_info.acc_uri.ptr,
3667 (acc_info.online_status?"online":"offline"));
3668 break;
3669
Benny Prijono4461c7d2007-08-25 13:36:15 +00003670 case 'T':
3671 change_online_status();
3672 break;
3673
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003674 case 'c':
3675 switch (menuin[1]) {
3676 case 'l':
3677 conf_list();
3678 break;
3679 case 'c':
3680 case 'd':
3681 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003682 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003683 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003684 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003685 const char *src_title, *dst_title;
3686
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003687 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003688
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003689 if (cnt != 3) {
3690 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003691
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003692 src_title = (menuin[1]=='c'?
3693 "Connect src port #":
3694 "Disconnect src port #");
3695 dst_title = (menuin[1]=='c'?
3696 "To dst port #":
3697 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003698
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003699 if (!simple_input(src_title, src_port, sizeof(src_port)))
3700 break;
3701
3702 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3703 break;
3704 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003705
3706 if (menuin[1]=='c') {
3707 status = pjsua_conf_connect(my_atoi(src_port),
3708 my_atoi(dst_port));
3709 } else {
3710 status = pjsua_conf_disconnect(my_atoi(src_port),
3711 my_atoi(dst_port));
3712 }
3713 if (status == PJ_SUCCESS) {
3714 puts("Success");
3715 } else {
3716 puts("ERROR!!");
3717 }
3718 }
3719 break;
3720 }
3721 break;
3722
Benny Prijono6dd967c2006-12-26 02:27:14 +00003723 case 'V':
3724 /* Adjust audio volume */
3725 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3726 if (simple_input(buf,text,sizeof(text))) {
3727 char *err;
3728 app_config.mic_level = (float)strtod(text, &err);
3729 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3730 }
3731 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3732 app_config.speaker_level);
3733 if (simple_input(buf,text,sizeof(text))) {
3734 char *err;
3735 app_config.speaker_level = (float)strtod(text, &err);
3736 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3737 }
3738
3739 break;
3740
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003741 case 'd':
3742 if (menuin[1] == 'c') {
3743 char settings[2000];
3744 int len;
3745
3746 len = write_settings(&app_config, settings, sizeof(settings));
3747 if (len < 1)
3748 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3749 else
3750 PJ_LOG(3,(THIS_FILE,
3751 "Dumping configuration (%d bytes):\n%s\n",
3752 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003753
3754 } else if (menuin[1] == 'q') {
3755
3756 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003757 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003758 } else {
3759 PJ_LOG(3,(THIS_FILE, "No current call"));
3760 }
3761
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003762 } else {
3763 app_dump(menuin[1]=='d');
3764 }
3765 break;
3766
3767
3768 case 'f':
3769 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3770 char settings[2000];
3771 int len;
3772
3773 len = write_settings(&app_config, settings, sizeof(settings));
3774 if (len < 1)
3775 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3776 else {
3777 pj_oshandle_t fd;
3778 pj_status_t status;
3779
3780 status = pj_file_open(app_config.pool, buf,
3781 PJ_O_WRONLY, &fd);
3782 if (status != PJ_SUCCESS) {
3783 pjsua_perror(THIS_FILE, "Unable to open file", status);
3784 } else {
3785 pj_ssize_t size = len;
3786 pj_file_write(fd, settings, &size);
3787 pj_file_close(fd);
3788
3789 printf("Settings successfully written to '%s'\n", buf);
3790 }
3791 }
3792
3793 }
3794 break;
3795
3796
3797 case 'q':
3798 goto on_exit;
3799
3800
3801 default:
3802 if (menuin[0] != '\n' && menuin[0] != '\r') {
3803 printf("Invalid input %s", menuin);
3804 }
3805 keystroke_help();
3806 break;
3807 }
3808 }
3809
3810on_exit:
3811 ;
3812}
3813
3814
3815/*****************************************************************************
3816 * Public API
3817 */
3818
3819pj_status_t app_init(int argc, char *argv[])
3820{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003821 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003822 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003823 unsigned i;
3824 pj_status_t status;
3825
3826 /* Create pjsua */
3827 status = pjsua_create();
3828 if (status != PJ_SUCCESS)
3829 return status;
3830
3831 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00003832 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003833
3834 /* Initialize default config */
3835 default_config(&app_config);
3836
3837 /* Parse the arguments */
3838 status = parse_args(argc, argv, &app_config, &uri_arg);
3839 if (status != PJ_SUCCESS)
3840 return status;
3841
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003842 /* Initialize application callbacks */
3843 app_config.cfg.cb.on_call_state = &on_call_state;
3844 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3845 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003846 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003847 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003848 app_config.cfg.cb.on_reg_state = &on_reg_state;
3849 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3850 app_config.cfg.cb.on_pager = &on_pager;
3851 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003852 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003853 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003854 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003855
3856 /* Initialize pjsua */
3857 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3858 &app_config.media_cfg);
3859 if (status != PJ_SUCCESS)
3860 return status;
3861
Benny Prijonoe909eac2006-07-27 22:04:56 +00003862#ifdef STEREO_DEMO
3863 stereo_demo();
3864#endif
3865
Benny Prijono804ff0a2006-09-14 11:17:48 +00003866 /* Initialize calls data */
3867 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3868 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3869 app_config.call_data[i].timer.cb = &call_timeout_callback;
3870 }
3871
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003872 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003873 for (i=0; i<app_config.wav_count; ++i) {
3874 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003875 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00003876
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003877 if (app_config.auto_play_hangup)
3878 play_options |= PJMEDIA_FILE_NO_LOOP;
3879
3880 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00003881 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003882 if (status != PJ_SUCCESS)
3883 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003884
Benny Prijono72c04d22007-02-17 22:20:58 +00003885 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003886 app_config.wav_id = wav_id;
3887 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003888 if (app_config.auto_play_hangup) {
3889 pjmedia_port *port;
3890
3891 pjsua_player_get_port(app_config.wav_id, &port);
3892 status = pjmedia_wav_player_set_eof_cb(port, NULL,
3893 &on_playfile_done);
3894 if (status != PJ_SUCCESS)
3895 goto on_error;
3896
3897 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
3898 &hangup_timeout_callback);
3899 }
Benny Prijono32e4f492007-01-24 00:44:26 +00003900 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003901 }
3902
Benny Prijono4af234b2007-01-24 02:02:09 +00003903 /* Optionally registers tone players */
3904 for (i=0; i<app_config.tone_count; ++i) {
3905 pjmedia_port *tport;
3906 char name[80];
3907 pj_str_t label;
3908 pj_status_t status;
3909
3910 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3911 app_config.tones[i].freq1,
3912 app_config.tones[i].freq2);
3913 label = pj_str(name);
3914 status = pjmedia_tonegen_create2(app_config.pool, &label,
3915 8000, 1, 160, 16,
3916 PJMEDIA_TONEGEN_LOOP, &tport);
3917 if (status != PJ_SUCCESS) {
3918 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3919 goto on_error;
3920 }
3921
3922 status = pjsua_conf_add_port(app_config.pool, tport,
3923 &app_config.tone_slots[i]);
3924 pj_assert(status == PJ_SUCCESS);
3925
3926 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3927 pj_assert(status == PJ_SUCCESS);
3928 }
3929
Benny Prijono1ebd6142006-10-19 15:48:02 +00003930 /* Optionally create recorder file, if any. */
3931 if (app_config.rec_file.slen) {
3932 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3933 &app_config.rec_id);
3934 if (status != PJ_SUCCESS)
3935 goto on_error;
3936
3937 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3938 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003939
Benny Prijonoe347cb02007-02-14 14:36:13 +00003940 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3941
Benny Prijono91d20f42008-06-14 19:42:37 +00003942 /* Create ringback tones */
3943 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00003944 unsigned i, samples_per_frame;
3945 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00003946 pj_str_t name;
3947
Benny Prijono4f966892008-06-14 22:43:56 +00003948 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
3949 app_config.media_cfg.clock_rate *
3950 app_config.media_cfg.channel_count / 1000;
3951
Benny Prijono91d20f42008-06-14 19:42:37 +00003952 /* Ringback tone (call is ringing) */
3953 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00003954 status = pjmedia_tonegen_create2(app_config.pool, &name,
3955 app_config.media_cfg.clock_rate,
3956 app_config.media_cfg.channel_count,
3957 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003958 16, PJMEDIA_TONEGEN_LOOP,
3959 &app_config.ringback_port);
3960 if (status != PJ_SUCCESS)
3961 goto on_error;
3962
Benny Prijonoe28cec82008-06-14 20:40:53 +00003963 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00003964 for (i=0; i<RINGBACK_CNT; ++i) {
3965 tone[i].freq1 = RINGBACK_FREQ1;
3966 tone[i].freq2 = RINGBACK_FREQ2;
3967 tone[i].on_msec = RINGBACK_ON;
3968 tone[i].off_msec = RINGBACK_OFF;
3969 }
3970 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003971
Benny Prijono4f966892008-06-14 22:43:56 +00003972 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00003973 PJMEDIA_TONEGEN_LOOP);
3974
3975
Benny Prijono91d20f42008-06-14 19:42:37 +00003976 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
3977 &app_config.ringback_slot);
3978 if (status != PJ_SUCCESS)
3979 goto on_error;
3980
3981 /* Ring (to alert incoming call) */
3982 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00003983 status = pjmedia_tonegen_create2(app_config.pool, &name,
3984 app_config.media_cfg.clock_rate,
3985 app_config.media_cfg.channel_count,
3986 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003987 16, PJMEDIA_TONEGEN_LOOP,
3988 &app_config.ring_port);
3989 if (status != PJ_SUCCESS)
3990 goto on_error;
3991
Benny Prijono4f966892008-06-14 22:43:56 +00003992 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00003993 tone[i].freq1 = RING_FREQ1;
3994 tone[i].freq2 = RING_FREQ2;
3995 tone[i].on_msec = RING_ON;
3996 tone[i].off_msec = RING_OFF;
3997 }
Benny Prijono4f966892008-06-14 22:43:56 +00003998 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003999
Benny Prijono4f966892008-06-14 22:43:56 +00004000 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004001 tone, PJMEDIA_TONEGEN_LOOP);
4002
Benny Prijono91d20f42008-06-14 19:42:37 +00004003 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4004 &app_config.ring_slot);
4005 if (status != PJ_SUCCESS)
4006 goto on_error;
4007
4008 }
4009
Benny Prijono87ef89a2007-01-14 00:39:45 +00004010 /* Add UDP transport unless it's disabled. */
4011 if (!app_config.no_udp) {
4012 pjsua_acc_id aid;
4013
4014 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4015 &app_config.udp_cfg,
4016 &transport_id);
4017 if (status != PJ_SUCCESS)
4018 goto on_error;
4019
4020 /* Add local account */
4021 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4022 //pjsua_acc_set_transport(aid, transport_id);
4023 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004024
4025 if (app_config.udp_cfg.port == 0) {
4026 pjsua_transport_info ti;
4027 pj_sockaddr_in *a;
4028
4029 pjsua_transport_get_info(transport_id, &ti);
4030 a = (pj_sockaddr_in*)&ti.local_addr;
4031
4032 tcp_cfg.port = pj_ntohs(a->sin_port);
4033 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004034 }
4035
Benny Prijonoe93e2872006-06-28 16:46:49 +00004036 /* Add TCP transport unless it's disabled */
4037 if (!app_config.no_tcp) {
4038 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004039 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004040 &transport_id);
4041 if (status != PJ_SUCCESS)
4042 goto on_error;
4043
4044 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004045 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004046 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4047
4048 }
4049
Benny Prijonoe93e2872006-06-28 16:46:49 +00004050
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004051#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4052 /* Add TLS transport when application wants one */
4053 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004054
4055 pjsua_acc_id acc_id;
4056
4057 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004058 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004059 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004060 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004061 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004062 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004063 if (status != PJ_SUCCESS)
4064 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004065
4066 /* Add local account */
4067 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4068 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004069 }
4070#endif
4071
Benny Prijonoe93e2872006-06-28 16:46:49 +00004072 if (transport_id == -1) {
4073 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
4074 status = -1;
4075 goto on_error;
4076 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004077
4078
4079 /* Add accounts */
4080 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004081 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004082 if (status != PJ_SUCCESS)
4083 goto on_error;
4084 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4085 }
4086
4087 /* Add buddies */
4088 for (i=0; i<app_config.buddy_cnt; ++i) {
4089 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4090 if (status != PJ_SUCCESS)
4091 goto on_error;
4092 }
4093
4094 /* Optionally set codec orders */
4095 for (i=0; i<app_config.codec_cnt; ++i) {
4096 pjsua_codec_set_priority(&app_config.codec_arg[i],
4097 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4098 }
4099
Benny Prijonofce28542007-12-09 15:41:10 +00004100 /* Optionally disable some codec */
4101 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4102 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4103 }
4104
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004105 /* Add RTP transports */
4106 status = pjsua_media_transports_create(&app_config.rtp_cfg);
4107 if (status != PJ_SUCCESS)
4108 goto on_error;
4109
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004110 /* Set sound device latency */
4111 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4112
Benny Prijono22a300a2006-06-14 20:04:55 +00004113 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004114#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004115 if (app_config.null_audio) {
4116 status = pjsua_set_null_snd_dev();
4117 if (status != PJ_SUCCESS)
4118 return status;
4119 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004120#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004121
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004122 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4123 app_config.playback_dev != PJSUA_INVALID_ID)
4124 {
4125 status = pjsua_set_snd_dev(app_config.capture_dev,
4126 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004127 if (status != PJ_SUCCESS)
4128 goto on_error;
4129 }
4130
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004131 return PJ_SUCCESS;
4132
4133on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004134 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004135 return status;
4136}
4137
4138
Benny Prijonoebc32c32008-06-12 13:30:39 +00004139static int stdout_refresh_proc(void *arg)
4140{
4141 PJ_UNUSED_ARG(arg);
4142
4143 /* Set thread to lowest priority so that it doesn't clobber
4144 * stdout output
4145 */
4146 pj_thread_set_prio(pj_thread_this(),
4147 pj_thread_get_prio_min(pj_thread_this()));
4148
4149 while (!stdout_refresh_quit) {
4150 pj_thread_sleep(stdout_refresh * 1000);
4151 puts(stdout_refresh_text);
4152 fflush(stdout);
4153 }
4154
4155 return 0;
4156}
4157
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004158pj_status_t app_main(void)
4159{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004160 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004161 pj_status_t status;
4162
4163 /* Start pjsua */
4164 status = pjsua_start();
4165 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004166 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004167 return status;
4168 }
4169
Benny Prijonoebc32c32008-06-12 13:30:39 +00004170 /* Start console refresh thread */
4171 if (stdout_refresh > 0) {
4172 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4173 NULL, 0, 0, &stdout_refresh_thread);
4174 }
4175
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004176 console_app_main(&uri_arg);
4177
Benny Prijonoebc32c32008-06-12 13:30:39 +00004178 if (stdout_refresh_thread) {
4179 stdout_refresh_quit = PJ_TRUE;
4180 pj_thread_join(stdout_refresh_thread);
4181 pj_thread_destroy(stdout_refresh_thread);
4182 }
4183
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004184 return PJ_SUCCESS;
4185}
4186
4187pj_status_t app_destroy(void)
4188{
Benny Prijonof762ee72006-12-01 11:14:37 +00004189 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004190 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004191
Benny Prijonoe909eac2006-07-27 22:04:56 +00004192#ifdef STEREO_DEMO
4193 if (app_config.snd) {
4194 pjmedia_snd_port_destroy(app_config.snd);
4195 app_config.snd = NULL;
4196 }
4197#endif
4198
Benny Prijono91d20f42008-06-14 19:42:37 +00004199 /* Close ringback port */
4200 if (app_config.ringback_port &&
4201 app_config.ringback_slot != PJSUA_INVALID_ID)
4202 {
4203 pjsua_conf_remove_port(app_config.ringback_slot);
4204 app_config.ringback_slot = PJSUA_INVALID_ID;
4205 pjmedia_port_destroy(app_config.ringback_port);
4206 app_config.ringback_port = NULL;
4207 }
4208
4209 /* Close ring port */
4210 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4211 pjsua_conf_remove_port(app_config.ring_slot);
4212 app_config.ring_slot = PJSUA_INVALID_ID;
4213 pjmedia_port_destroy(app_config.ring_port);
4214 app_config.ring_port = NULL;
4215 }
4216
Benny Prijono4af234b2007-01-24 02:02:09 +00004217 /* Close tone generators */
4218 for (i=0; i<app_config.tone_count; ++i) {
4219 pjsua_conf_remove_port(app_config.tone_slots[i]);
4220 }
4221
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004222 if (app_config.pool) {
4223 pj_pool_release(app_config.pool);
4224 app_config.pool = NULL;
4225 }
4226
Benny Prijonof762ee72006-12-01 11:14:37 +00004227 status = pjsua_destroy();
4228
4229 pj_bzero(&app_config, sizeof(app_config));
4230
4231 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004232}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004233
4234
4235#ifdef STEREO_DEMO
4236static void stereo_demo()
4237{
4238 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004239 pj_status_t status;
4240
4241 /* Disable existing sound device */
4242 conf = pjsua_set_no_snd_dev();
4243
Benny Prijonoe909eac2006-07-27 22:04:56 +00004244 /* Create stereo-mono splitter/combiner */
4245 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004246 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004247 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004248 2 * conf->info.samples_per_frame,
4249 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004250 0 /* options */,
4251 &splitter);
4252 pj_assert(status == PJ_SUCCESS);
4253
4254 /* Connect channel0 (left channel?) to conference port slot0 */
4255 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4256 0 /*options*/,
4257 conf);
4258 pj_assert(status == PJ_SUCCESS);
4259
4260 /* Create reverse channel for channel1 (right channel?)... */
4261 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4262 splitter,
4263 1 /* ch1 */,
4264 0 /* options */,
4265 &ch1);
4266 pj_assert(status == PJ_SUCCESS);
4267
4268 /* .. and register it to conference bridge (it would be slot1
4269 * if there's no other devices connected to the bridge)
4270 */
4271 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4272 pj_assert(status == PJ_SUCCESS);
4273
4274 /* Create sound device */
4275 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004276 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004277 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004278 2 * conf->info.samples_per_frame,
4279 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004280 0, &app_config.snd);
4281 pj_assert(status == PJ_SUCCESS);
4282
4283
4284 /* Connect the splitter to the sound device */
4285 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4286 pj_assert(status == PJ_SUCCESS);
4287
4288}
4289#endif
4290