blob: de9c736eaa41a34f9f9a142763d51283477745e3 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20
21
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000022#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000023#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000024
Benny Prijonoe909eac2006-07-27 22:04:56 +000025//#define STEREO_DEMO
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026
Benny Prijono4f966892008-06-14 22:43:56 +000027/* Ringtones US UK */
28#define RINGBACK_FREQ1 440 /* 400 */
29#define RINGBACK_FREQ2 480 /* 450 */
30#define RINGBACK_ON 2000 /* 400 */
31#define RINGBACK_OFF 4000 /* 200 */
32#define RINGBACK_CNT 1 /* 2 */
33#define RINGBACK_INTERVAL 4000 /* 2000 */
Benny Prijono91d20f42008-06-14 19:42:37 +000034
Benny Prijono4f966892008-06-14 22:43:56 +000035#define RING_FREQ1 800
36#define RING_FREQ2 640
37#define RING_ON 200
38#define RING_OFF 100
39#define RING_CNT 3
40#define RING_INTERVAL 3000
Benny Prijono91d20f42008-06-14 19:42:37 +000041
42
Benny Prijono804ff0a2006-09-14 11:17:48 +000043/* Call specific data */
44struct call_data
45{
46 pj_timer_entry timer;
Benny Prijono91d20f42008-06-14 19:42:37 +000047 pj_bool_t ringback_on;
48 pj_bool_t ring_on;
Benny Prijono804ff0a2006-09-14 11:17:48 +000049};
50
Benny Prijonoeebe9af2006-06-13 22:57:13 +000051
52/* Pjsua application data */
53static struct app_config
54{
55 pjsua_config cfg;
56 pjsua_logging_config log_cfg;
57 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000058 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000059 pj_bool_t no_tcp;
60 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000061 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000062 pjsua_transport_config udp_cfg;
63 pjsua_transport_config rtp_cfg;
64
65 unsigned acc_cnt;
66 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
67
68 unsigned buddy_cnt;
69 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
70
Benny Prijono804ff0a2006-09-14 11:17:48 +000071 struct call_data call_data[PJSUA_MAX_CALLS];
72
Benny Prijonoeebe9af2006-06-13 22:57:13 +000073 pj_pool_t *pool;
74 /* Compatibility with older pjsua */
75
76 unsigned codec_cnt;
77 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000078 unsigned codec_dis_cnt;
79 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000080 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000081 unsigned wav_count;
82 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000083 unsigned tone_count;
84 pjmedia_tone_desc tones[32];
85 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000086 pjsua_player_id wav_id;
87 pjsua_conf_port_id wav_port;
88 pj_bool_t auto_play;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +000089 pj_bool_t auto_play_hangup;
90 pj_timer_entry auto_hangup_timer;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000091 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000092 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000093 pj_str_t rec_file;
94 pj_bool_t auto_rec;
95 pjsua_recorder_id rec_id;
96 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000097 unsigned auto_answer;
98 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +000099
100#ifdef STEREO_DEMO
101 pjmedia_snd_port *snd;
102#endif
103
Benny Prijono6dd967c2006-12-26 02:27:14 +0000104 float mic_level,
105 speaker_level;
106
Benny Prijono4e5d5512007-03-06 18:11:30 +0000107 int capture_dev, playback_dev;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000108 unsigned capture_lat, playback_lat;
Benny Prijono91d20f42008-06-14 19:42:37 +0000109
110 pj_bool_t no_tones;
111 int ringback_slot;
112 int ringback_cnt;
113 pjmedia_port *ringback_port;
114 int ring_slot;
115 int ring_cnt;
116 pjmedia_port *ring_port;
117
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000118} app_config;
119
120
Benny Prijono21b9ad92006-08-15 13:11:22 +0000121//static pjsua_acc_id current_acc;
122#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +0000123static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoebc32c32008-06-12 13:30:39 +0000124static pj_bool_t cmd_echo;
125static int stdout_refresh = -1;
126static const char *stdout_refresh_text = "STDOUT_REFRESH";
127static pj_bool_t stdout_refresh_quit = PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000128static pj_str_t uri_arg;
129
Benny Prijono6eddd872008-03-21 13:46:08 +0000130static char some_buf[2048];
131
Benny Prijono594e4c52006-09-14 18:51:01 +0000132#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +0000133static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +0000134#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000135pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000136
Benny Prijono91d20f42008-06-14 19:42:37 +0000137static void ringback_start(pjsua_call_id call_id);
138static void ring_start(pjsua_call_id call_id);
139static void ring_stop(pjsua_call_id call_id);
140
141
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000142/*****************************************************************************
143 * Configuration manipulation
144 */
145
146/* Show usage */
147static void usage(void)
148{
149 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000150 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000151 puts ("");
152 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000153 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000154 puts (" --help Display this help screen");
155 puts (" --version Display version info");
156 puts ("");
157 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000158 puts (" --log-file=fname Log to filename (default stderr)");
159 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
160 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
161 puts ("");
162 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000163 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000164#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000165 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
166 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000167#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000168 puts (" --registrar=url Set the URL of registrar server");
169 puts (" --id=url Set the URL of local ID (used in From header)");
170 puts (" --contact=url Optionally override the Contact information");
171 puts (" --proxy=url Optional URL of proxy server to visit");
172 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000173 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174 puts (" --realm=string Set realm");
175 puts (" --username=string Set authentication username");
176 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000177 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000178 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000179 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
180 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000181 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 puts ("");
183 puts ("SIP Account Control:");
184 puts (" --next-account Add more account");
185 puts ("");
186 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000187 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
188 puts (" TCP and UDP transports on the specified port, unless");
189 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000190 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
191 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000192 puts (" --no-tcp Disable TCP transport.");
193 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000194 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
195 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000196 puts (" --outbound=url Set the URL of global outbound proxy server");
197 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000198 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000199 puts ("");
200 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000201 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000202 puts (" --tls-ca-file Specify TLS CA file (default=none)");
203 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
204 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
205 puts (" --tls-password Specify TLS password to private key file (default=none)");
206 puts (" --tls-verify-server Verify server's certificate (default=no)");
207 puts (" --tls-verify-client Verify client's certificate (default=no)");
208 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
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
1765
1766/*****************************************************************************
1767 * Console application
1768 */
1769
Benny Prijono91d20f42008-06-14 19:42:37 +00001770static void ringback_start(pjsua_call_id call_id)
1771{
1772 if (app_config.no_tones)
1773 return;
1774
1775 if (app_config.call_data[call_id].ringback_on)
1776 return;
1777
1778 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1779
1780 if (++app_config.ringback_cnt==1 &&
1781 app_config.ringback_slot!=PJSUA_INVALID_ID)
1782 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001783 pjsua_conf_connect(app_config.ringback_slot, 0);
1784 }
1785}
1786
1787static void ring_stop(pjsua_call_id call_id)
1788{
1789 if (app_config.no_tones)
1790 return;
1791
1792 if (app_config.call_data[call_id].ringback_on) {
1793 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1794
1795 pj_assert(app_config.ringback_cnt>0);
1796 if (--app_config.ringback_cnt == 0 &&
1797 app_config.ringback_slot!=PJSUA_INVALID_ID)
1798 {
1799 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001800 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001801 }
1802 }
1803
1804 if (app_config.call_data[call_id].ring_on) {
1805 app_config.call_data[call_id].ring_on = PJ_FALSE;
1806
1807 pj_assert(app_config.ring_cnt>0);
1808 if (--app_config.ring_cnt == 0 &&
1809 app_config.ring_slot!=PJSUA_INVALID_ID)
1810 {
1811 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001812 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001813 }
1814 }
1815}
1816
1817static void ring_start(pjsua_call_id call_id)
1818{
1819 if (app_config.no_tones)
1820 return;
1821
1822 if (app_config.call_data[call_id].ring_on)
1823 return;
1824
1825 app_config.call_data[call_id].ring_on = PJ_TRUE;
1826
1827 if (++app_config.ring_cnt==1 &&
1828 app_config.ring_slot!=PJSUA_INVALID_ID)
1829 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001830 pjsua_conf_connect(app_config.ring_slot, 0);
1831 }
1832}
1833
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001834/*
1835 * Find next call when current call is disconnected or when user
1836 * press ']'
1837 */
1838static pj_bool_t find_next_call(void)
1839{
1840 int i, max;
1841
1842 max = pjsua_call_get_max_count();
1843 for (i=current_call+1; i<max; ++i) {
1844 if (pjsua_call_is_active(i)) {
1845 current_call = i;
1846 return PJ_TRUE;
1847 }
1848 }
1849
1850 for (i=0; i<current_call; ++i) {
1851 if (pjsua_call_is_active(i)) {
1852 current_call = i;
1853 return PJ_TRUE;
1854 }
1855 }
1856
1857 current_call = PJSUA_INVALID_ID;
1858 return PJ_FALSE;
1859}
1860
1861
1862/*
1863 * Find previous call when user press '['
1864 */
1865static pj_bool_t find_prev_call(void)
1866{
1867 int i, max;
1868
1869 max = pjsua_call_get_max_count();
1870 for (i=current_call-1; i>=0; --i) {
1871 if (pjsua_call_is_active(i)) {
1872 current_call = i;
1873 return PJ_TRUE;
1874 }
1875 }
1876
1877 for (i=max-1; i>current_call; --i) {
1878 if (pjsua_call_is_active(i)) {
1879 current_call = i;
1880 return PJ_TRUE;
1881 }
1882 }
1883
1884 current_call = PJSUA_INVALID_ID;
1885 return PJ_FALSE;
1886}
1887
1888
Benny Prijono804ff0a2006-09-14 11:17:48 +00001889/* Callback from timer when the maximum call duration has been
1890 * exceeded.
1891 */
1892static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1893 struct pj_timer_entry *entry)
1894{
1895 pjsua_call_id call_id = entry->id;
1896 pjsua_msg_data msg_data;
1897 pjsip_generic_string_hdr warn;
1898 pj_str_t hname = pj_str("Warning");
1899 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1900
1901 PJ_UNUSED_ARG(timer_heap);
1902
Benny Prijono148c9dd2006-09-19 13:37:53 +00001903 if (call_id == PJSUA_INVALID_ID) {
1904 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1905 return;
1906 }
1907
Benny Prijono804ff0a2006-09-14 11:17:48 +00001908 /* Add warning header */
1909 pjsua_msg_data_init(&msg_data);
1910 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1911 pj_list_push_back(&msg_data.hdr_list, &warn);
1912
1913 /* Call duration has been exceeded; disconnect the call */
1914 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1915 "for call %d, disconnecting the call",
1916 app_config.duration, call_id));
1917 entry->id = PJSUA_INVALID_ID;
1918 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1919}
1920
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001921
1922/*
1923 * Handler when invite state has changed.
1924 */
1925static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1926{
1927 pjsua_call_info call_info;
1928
1929 PJ_UNUSED_ARG(e);
1930
1931 pjsua_call_get_info(call_id, &call_info);
1932
1933 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1934
Benny Prijono91d20f42008-06-14 19:42:37 +00001935 /* Stop all ringback for this call */
1936 ring_stop(call_id);
1937
Benny Prijono804ff0a2006-09-14 11:17:48 +00001938 /* Cancel duration timer, if any */
1939 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1940 struct call_data *cd = &app_config.call_data[call_id];
1941 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1942
1943 cd->timer.id = PJSUA_INVALID_ID;
1944 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1945 }
1946
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00001947 /* Rewind play file when hangup automatically,
1948 * since file is not looped
1949 */
1950 if (app_config.auto_play_hangup)
1951 pjsua_player_set_pos(app_config.wav_id, 0);
1952
1953
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001954 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1955 call_id,
1956 call_info.last_status,
1957 call_info.last_status_text.ptr));
1958
1959 if (call_id == current_call) {
1960 find_next_call();
1961 }
1962
Benny Prijono4be63b52006-11-25 14:50:25 +00001963 /* Dump media state upon disconnected */
1964 if (1) {
Benny Prijono6eddd872008-03-21 13:46:08 +00001965 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1966 sizeof(some_buf), " ");
Benny Prijono4be63b52006-11-25 14:50:25 +00001967 PJ_LOG(5,(THIS_FILE,
1968 "Call %d disconnected, dumping media stats\n%s",
Benny Prijono6eddd872008-03-21 13:46:08 +00001969 call_id, some_buf));
Benny Prijono4be63b52006-11-25 14:50:25 +00001970 }
1971
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001972 } else {
1973
Benny Prijono804ff0a2006-09-14 11:17:48 +00001974 if (app_config.duration!=NO_LIMIT &&
1975 call_info.state == PJSIP_INV_STATE_CONFIRMED)
1976 {
1977 /* Schedule timer to hangup call after the specified duration */
1978 struct call_data *cd = &app_config.call_data[call_id];
1979 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1980 pj_time_val delay;
1981
1982 cd->timer.id = call_id;
1983 delay.sec = app_config.duration;
1984 delay.msec = 0;
1985 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
1986 }
1987
Benny Prijono4be63b52006-11-25 14:50:25 +00001988 if (call_info.state == PJSIP_INV_STATE_EARLY) {
1989 int code;
1990 pj_str_t reason;
1991 pjsip_msg *msg;
1992
1993 /* This can only occur because of TX or RX message */
1994 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
1995
1996 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1997 msg = e->body.tsx_state.src.rdata->msg_info.msg;
1998 } else {
1999 msg = e->body.tsx_state.src.tdata->msg;
2000 }
2001
2002 code = msg->line.status.code;
2003 reason = msg->line.status.reason;
2004
Benny Prijono91d20f42008-06-14 19:42:37 +00002005 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2006 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2007 msg->body == NULL &&
2008 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2009 {
2010 ringback_start(call_id);
2011 }
2012
Benny Prijono4be63b52006-11-25 14:50:25 +00002013 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2014 call_id, call_info.state_text.ptr,
2015 code, (int)reason.slen, reason.ptr));
2016 } else {
2017 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2018 call_id,
2019 call_info.state_text.ptr));
2020 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002021
2022 if (current_call==PJSUA_INVALID_ID)
2023 current_call = call_id;
2024
2025 }
2026}
2027
2028
2029/**
2030 * Handler when there is incoming call.
2031 */
2032static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2033 pjsip_rx_data *rdata)
2034{
2035 pjsua_call_info call_info;
2036
2037 PJ_UNUSED_ARG(acc_id);
2038 PJ_UNUSED_ARG(rdata);
2039
2040 pjsua_call_get_info(call_id, &call_info);
2041
Benny Prijono91d20f42008-06-14 19:42:37 +00002042 /* Start ringback */
2043 ring_start(call_id);
2044
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002045 if (app_config.auto_answer > 0) {
2046 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2047 }
2048
2049 if (app_config.auto_answer < 200) {
2050 PJ_LOG(3,(THIS_FILE,
2051 "Incoming call for account %d!\n"
2052 "From: %s\n"
2053 "To: %s\n"
2054 "Press a to answer or h to reject call",
2055 acc_id,
2056 call_info.remote_info.ptr,
2057 call_info.local_info.ptr));
2058 }
2059}
2060
2061
2062/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002063 * Handler when a transaction within a call has changed state.
2064 */
2065static void on_call_tsx_state(pjsua_call_id call_id,
2066 pjsip_transaction *tsx,
2067 pjsip_event *e)
2068{
2069 const pjsip_method info_method =
2070 {
2071 PJSIP_OTHER_METHOD,
2072 { "INFO", 4 }
2073 };
2074
2075 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2076 /*
2077 * Handle INFO method.
2078 */
2079 if (tsx->role == PJSIP_ROLE_UAC &&
2080 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002081 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2082 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002083 {
2084 /* Status of outgoing INFO request */
2085 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2086 PJ_LOG(4,(THIS_FILE,
2087 "Call %d: DTMF sent successfully with INFO",
2088 call_id));
2089 } else if (tsx->status_code >= 300) {
2090 PJ_LOG(4,(THIS_FILE,
2091 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2092 call_id,
2093 tsx->status_code,
2094 (int)tsx->status_text.slen,
2095 tsx->status_text.ptr));
2096 }
2097 } else if (tsx->role == PJSIP_ROLE_UAS &&
2098 tsx->state == PJSIP_TSX_STATE_TRYING)
2099 {
2100 /* Answer incoming INFO with 200/OK */
2101 pjsip_rx_data *rdata;
2102 pjsip_tx_data *tdata;
2103 pj_status_t status;
2104
2105 rdata = e->body.tsx_state.src.rdata;
2106
2107 if (rdata->msg_info.msg->body) {
2108 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2109 200, NULL, &tdata);
2110 if (status == PJ_SUCCESS)
2111 status = pjsip_tsx_send_msg(tsx, tdata);
2112
2113 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2114 call_id,
2115 (int)rdata->msg_info.msg->body->len,
2116 rdata->msg_info.msg->body->data));
2117 } else {
2118 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2119 400, NULL, &tdata);
2120 if (status == PJ_SUCCESS)
2121 status = pjsip_tsx_send_msg(tsx, tdata);
2122 }
2123 }
2124 }
2125}
2126
2127
2128/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002129 * Callback on media state changed event.
2130 * The action may connect the call to sound device, to file, or
2131 * to loop the call.
2132 */
2133static void on_call_media_state(pjsua_call_id call_id)
2134{
2135 pjsua_call_info call_info;
2136
2137 pjsua_call_get_info(call_id, &call_info);
2138
Benny Prijono91d20f42008-06-14 19:42:37 +00002139 /* Stop ringback */
2140 ring_stop(call_id);
2141
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002142 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
2143 pj_bool_t connect_sound = PJ_TRUE;
2144
2145 /* Loopback sound, if desired */
2146 if (app_config.auto_loop) {
2147 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2148 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00002149
2150 /* Automatically record conversation, if desired */
2151 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2152 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2153 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002154 }
2155
2156 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002157 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2158 app_config.wav_port != PJSUA_INVALID_ID)
2159 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002160 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2161 connect_sound = PJ_FALSE;
2162 }
2163
Benny Prijono7ca96da2006-08-07 12:11:40 +00002164 /* Put call in conference with other calls, if desired */
2165 if (app_config.auto_conf) {
2166 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002167 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002168 unsigned i;
2169
2170 /* Get all calls, and establish media connection between
2171 * this call and other calls.
2172 */
2173 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002174
Benny Prijono7ca96da2006-08-07 12:11:40 +00002175 for (i=0; i<call_cnt; ++i) {
2176 if (call_ids[i] == call_id)
2177 continue;
2178
2179 if (!pjsua_call_has_media(call_ids[i]))
2180 continue;
2181
2182 pjsua_conf_connect(call_info.conf_slot,
2183 pjsua_call_get_conf_port(call_ids[i]));
2184 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2185 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002186
2187 /* Automatically record conversation, if desired */
2188 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2189 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2190 app_config.rec_port);
2191 }
2192
Benny Prijono7ca96da2006-08-07 12:11:40 +00002193 }
2194
2195 /* Also connect call to local sound device */
2196 connect_sound = PJ_TRUE;
2197 }
2198
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002199 /* Otherwise connect to sound device */
2200 if (connect_sound) {
2201 pjsua_conf_connect(call_info.conf_slot, 0);
2202 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002203
2204 /* Automatically record conversation, if desired */
2205 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2206 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2207 pjsua_conf_connect(0, app_config.rec_port);
2208 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002209 }
2210
2211 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
2212
2213 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
2214 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2215 call_id));
2216 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
2217 PJ_LOG(3,(THIS_FILE,
2218 "Media for call %d is suspended (hold) by remote",
2219 call_id));
Benny Prijono096c56c2007-09-15 08:30:16 +00002220 } else if (call_info.media_status == PJSUA_CALL_MEDIA_ERROR) {
2221 pj_str_t reason = pj_str("ICE negotiation failed");
2222
2223 PJ_LOG(1,(THIS_FILE,
2224 "Media has reported error, disconnecting call"));
2225
2226 pjsua_call_hangup(call_id, 500, &reason, NULL);
2227
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002228 } else {
2229 PJ_LOG(3,(THIS_FILE,
2230 "Media for call %d is inactive",
2231 call_id));
2232 }
2233}
2234
Benny Prijono0875ae82006-12-26 00:11:48 +00002235/*
2236 * DTMF callback.
2237 */
2238static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2239{
2240 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2241}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002242
2243/*
2244 * Handler registration status has changed.
2245 */
2246static void on_reg_state(pjsua_acc_id acc_id)
2247{
2248 PJ_UNUSED_ARG(acc_id);
2249
2250 // Log already written.
2251}
2252
2253
2254/*
2255 * Handler on buddy state changed.
2256 */
2257static void on_buddy_state(pjsua_buddy_id buddy_id)
2258{
2259 pjsua_buddy_info info;
2260 pjsua_buddy_get_info(buddy_id, &info);
2261
2262 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2263 (int)info.uri.slen,
2264 info.uri.ptr,
2265 (int)info.status_text.slen,
2266 info.status_text.ptr));
2267}
2268
2269
2270/**
2271 * Incoming IM message (i.e. MESSAGE request)!
2272 */
2273static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2274 const pj_str_t *to, const pj_str_t *contact,
2275 const pj_str_t *mime_type, const pj_str_t *text)
2276{
2277 /* Note: call index may be -1 */
2278 PJ_UNUSED_ARG(call_id);
2279 PJ_UNUSED_ARG(to);
2280 PJ_UNUSED_ARG(contact);
2281 PJ_UNUSED_ARG(mime_type);
2282
Benny Prijonof4b538d2007-05-14 16:45:20 +00002283 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002284 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002285 (int)text->slen, text->ptr,
2286 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002287}
2288
2289
2290/**
2291 * Received typing indication
2292 */
2293static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2294 const pj_str_t *to, const pj_str_t *contact,
2295 pj_bool_t is_typing)
2296{
2297 PJ_UNUSED_ARG(call_id);
2298 PJ_UNUSED_ARG(to);
2299 PJ_UNUSED_ARG(contact);
2300
2301 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2302 (int)from->slen, from->ptr,
2303 (is_typing?"is typing..":"has stopped typing")));
2304}
2305
2306
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002307/**
2308 * Call transfer request status.
2309 */
2310static void on_call_transfer_status(pjsua_call_id call_id,
2311 int status_code,
2312 const pj_str_t *status_text,
2313 pj_bool_t final,
2314 pj_bool_t *p_cont)
2315{
2316 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2317 call_id, status_code,
2318 (int)status_text->slen, status_text->ptr,
2319 (final ? "[final]" : "")));
2320
2321 if (status_code/100 == 2) {
2322 PJ_LOG(3,(THIS_FILE,
2323 "Call %d: call transfered successfully, disconnecting call",
2324 call_id));
2325 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2326 *p_cont = PJ_FALSE;
2327 }
2328}
2329
2330
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002331/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002332 * Notification that call is being replaced.
2333 */
2334static void on_call_replaced(pjsua_call_id old_call_id,
2335 pjsua_call_id new_call_id)
2336{
2337 pjsua_call_info old_ci, new_ci;
2338
2339 pjsua_call_get_info(old_call_id, &old_ci);
2340 pjsua_call_get_info(new_call_id, &new_ci);
2341
2342 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2343 "call %d with %.*s",
2344 old_call_id,
2345 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2346 new_call_id,
2347 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2348}
2349
2350
2351/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002352 * NAT type detection callback.
2353 */
2354static void on_nat_detect(const pj_stun_nat_detect_result *res)
2355{
2356 if (res->status != PJ_SUCCESS) {
2357 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2358 } else {
2359 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2360 }
2361}
2362
2363
2364/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002365 * Print buddy list.
2366 */
2367static void print_buddy_list(void)
2368{
2369 pjsua_buddy_id ids[64];
2370 int i;
2371 unsigned count = PJ_ARRAY_SIZE(ids);
2372
2373 puts("Buddy list:");
2374
2375 pjsua_enum_buddies(ids, &count);
2376
2377 if (count == 0)
2378 puts(" -none-");
2379 else {
2380 for (i=0; i<(int)count; ++i) {
2381 pjsua_buddy_info info;
2382
2383 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2384 continue;
2385
Benny Prijono4461c7d2007-08-25 13:36:15 +00002386 printf(" [%2d] <%.*s> %.*s\n",
2387 ids[i]+1,
2388 (int)info.status_text.slen,
2389 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002390 (int)info.uri.slen,
2391 info.uri.ptr);
2392 }
2393 }
2394 puts("");
2395}
2396
2397
2398/*
2399 * Print account status.
2400 */
2401static void print_acc_status(int acc_id)
2402{
2403 char buf[80];
2404 pjsua_acc_info info;
2405
2406 pjsua_acc_get_info(acc_id, &info);
2407
2408 if (!info.has_registration) {
2409 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2410 (int)info.status_text.slen,
2411 info.status_text.ptr);
2412
2413 } else {
2414 pj_ansi_snprintf(buf, sizeof(buf),
2415 "%d/%.*s (expires=%d)",
2416 info.status,
2417 (int)info.status_text.slen,
2418 info.status_text.ptr,
2419 info.expires);
2420
2421 }
2422
2423 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2424 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002425 printf(" Online status: %.*s\n",
2426 (int)info.online_status_text.slen,
2427 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002428}
2429
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002430/* Playfile done notification, set timer to hangup calls */
2431pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2432{
2433 pj_time_val delay;
2434
2435 PJ_UNUSED_ARG(port);
2436 PJ_UNUSED_ARG(usr_data);
2437
2438 /* Just rewind WAV when it is played outside of call */
2439 if (pjsua_call_get_count() == 0) {
2440 pjsua_player_set_pos(app_config.wav_id, 0);
2441 return PJ_SUCCESS;
2442 }
2443
2444 /* Timer is already active */
2445 if (app_config.auto_hangup_timer.id == 1)
2446 return PJ_SUCCESS;
2447
2448 app_config.auto_hangup_timer.id = 1;
2449 delay.sec = 0;
2450 delay.msec = 200; /* Give 200 ms before hangup */
2451 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2452 &app_config.auto_hangup_timer,
2453 &delay);
2454
2455 return PJ_SUCCESS;
2456}
2457
2458/* Auto hangup timer callback */
2459static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2460 struct pj_timer_entry *entry)
2461{
2462 PJ_UNUSED_ARG(timer_heap);
2463 PJ_UNUSED_ARG(entry);
2464
2465 app_config.auto_hangup_timer.id = 0;
2466 pjsua_call_hangup_all();
2467}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002468
2469/*
2470 * Show a bit of help.
2471 */
2472static void keystroke_help(void)
2473{
2474 pjsua_acc_id acc_ids[16];
2475 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2476 int i;
2477
2478 printf(">>>>\n");
2479
2480 pjsua_enum_accs(acc_ids, &count);
2481
2482 printf("Account list:\n");
2483 for (i=0; i<(int)count; ++i)
2484 print_acc_status(acc_ids[i]);
2485
2486 print_buddy_list();
2487
2488 //puts("Commands:");
2489 puts("+=============================================================================+");
2490 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2491 puts("| | | |");
2492 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2493 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002494 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2495 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2496 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2497 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002498 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2499 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002500 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002501 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002502 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2503 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2504 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2505 puts("| | V Adjust audio Volume | f Save config |");
2506 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002507 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002508 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002509 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002510
2511 i = pjsua_call_get_count();
2512 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002513
2514 if (current_call != PJSUA_INVALID_ID) {
2515 pjsua_call_info ci;
2516 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2517 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2518 (int)ci.remote_info.slen, ci.remote_info.ptr,
2519 (int)ci.state_text.slen, ci.state_text.ptr);
2520 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002521}
2522
2523
2524/*
2525 * Input simple string
2526 */
2527static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2528{
2529 char *p;
2530
2531 printf("%s (empty to cancel): ", title); fflush(stdout);
2532 fgets(buf, len, stdin);
2533
2534 /* Remove trailing newlines. */
2535 for (p=buf; ; ++p) {
2536 if (*p=='\r' || *p=='\n') *p='\0';
2537 else if (!*p) break;
2538 }
2539
2540 if (!*buf)
2541 return PJ_FALSE;
2542
2543 return PJ_TRUE;
2544}
2545
2546
2547#define NO_NB -2
2548struct input_result
2549{
2550 int nb_result;
2551 char *uri_result;
2552};
2553
2554
2555/*
2556 * Input URL.
2557 */
2558static void ui_input_url(const char *title, char *buf, int len,
2559 struct input_result *result)
2560{
2561 result->nb_result = NO_NB;
2562 result->uri_result = NULL;
2563
2564 print_buddy_list();
2565
2566 printf("Choices:\n"
2567 " 0 For current dialog.\n"
2568 " -1 All %d buddies in buddy list\n"
2569 " [1 -%2d] Select from buddy list\n"
2570 " URL An URL\n"
2571 " <Enter> Empty input (or 'q') to cancel\n"
2572 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2573 printf("%s: ", title);
2574
2575 fflush(stdout);
2576 fgets(buf, len, stdin);
2577 len = strlen(buf);
2578
2579 /* Left trim */
2580 while (pj_isspace(*buf)) {
2581 ++buf;
2582 --len;
2583 }
2584
2585 /* Remove trailing newlines */
2586 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2587 buf[--len] = '\0';
2588
2589 if (len == 0 || buf[0]=='q')
2590 return;
2591
2592 if (pj_isdigit(*buf) || *buf=='-') {
2593
2594 int i;
2595
2596 if (*buf=='-')
2597 i = 1;
2598 else
2599 i = 0;
2600
2601 for (; i<len; ++i) {
2602 if (!pj_isdigit(buf[i])) {
2603 puts("Invalid input");
2604 return;
2605 }
2606 }
2607
2608 result->nb_result = my_atoi(buf);
2609
2610 if (result->nb_result >= 0 &&
2611 result->nb_result <= (int)pjsua_get_buddy_count())
2612 {
2613 return;
2614 }
2615 if (result->nb_result == -1)
2616 return;
2617
2618 puts("Invalid input");
2619 result->nb_result = NO_NB;
2620 return;
2621
2622 } else {
2623 pj_status_t status;
2624
2625 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2626 pjsua_perror(THIS_FILE, "Invalid URL", status);
2627 return;
2628 }
2629
2630 result->uri_result = buf;
2631 }
2632}
2633
2634/*
2635 * List the ports in conference bridge
2636 */
2637static void conf_list(void)
2638{
2639 unsigned i, count;
2640 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2641
2642 printf("Conference ports:\n");
2643
2644 count = PJ_ARRAY_SIZE(id);
2645 pjsua_enum_conf_ports(id, &count);
2646
2647 for (i=0; i<count; ++i) {
2648 char txlist[PJSUA_MAX_CALLS*4+10];
2649 unsigned j;
2650 pjsua_conf_port_info info;
2651
2652 pjsua_conf_get_port_info(id[i], &info);
2653
2654 txlist[0] = '\0';
2655 for (j=0; j<info.listener_cnt; ++j) {
2656 char s[10];
2657 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2658 pj_ansi_strcat(txlist, s);
2659 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002660 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002661 info.slot_id,
2662 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002663 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002664 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002665 (int)info.name.slen,
2666 info.name.ptr,
2667 txlist);
2668
2669 }
2670 puts("");
2671}
2672
2673
2674/*
Benny Prijono56315612006-07-18 14:39:40 +00002675 * Send arbitrary request to remote host
2676 */
2677static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2678{
2679 pj_str_t str_method;
2680 pjsip_method method;
2681 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002682 pjsip_endpoint *endpt;
2683 pj_status_t status;
2684
2685 endpt = pjsua_get_pjsip_endpt();
2686
2687 str_method = pj_str(cstr_method);
2688 pjsip_method_init_np(&method, &str_method);
2689
Benny Prijonofff245c2007-04-02 11:44:47 +00002690 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002691
Benny Prijonob988d762007-12-05 04:30:04 +00002692 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002693 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002694 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002695 return;
2696 }
2697}
2698
2699
2700/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002701 * Change extended online status.
2702 */
2703static void change_online_status(void)
2704{
2705 char menuin[32];
2706 pj_bool_t online_status;
2707 pjrpid_element elem;
2708 int i, choice;
2709
2710 enum {
2711 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2712 };
2713
2714 struct opt {
2715 int id;
2716 char *name;
2717 } opts[] = {
2718 { AVAILABLE, "Available" },
2719 { BUSY, "Busy"},
2720 { OTP, "On the phone"},
2721 { IDLE, "Idle"},
2722 { AWAY, "Away"},
2723 { BRB, "Be right back"},
2724 { OFFLINE, "Offline"}
2725 };
2726
2727 printf("\n"
2728 "Choices:\n");
2729 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2730 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2731 }
2732
2733 if (!simple_input("Select status", menuin, sizeof(menuin)))
2734 return;
2735
2736 choice = atoi(menuin) - 1;
2737 if (choice < 0 || choice >= OPT_MAX) {
2738 puts("Invalid selection");
2739 return;
2740 }
2741
2742 pj_bzero(&elem, sizeof(elem));
2743 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2744
2745 online_status = PJ_TRUE;
2746
2747 switch (choice) {
2748 case AVAILABLE:
2749 break;
2750 case BUSY:
2751 elem.activity = PJRPID_ACTIVITY_BUSY;
2752 elem.note = pj_str("Busy");
2753 break;
2754 case OTP:
2755 elem.activity = PJRPID_ACTIVITY_BUSY;
2756 elem.note = pj_str("On the phone");
2757 break;
2758 case IDLE:
2759 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2760 elem.note = pj_str("Idle");
2761 break;
2762 case AWAY:
2763 elem.activity = PJRPID_ACTIVITY_AWAY;
2764 elem.note = pj_str("Away");
2765 break;
2766 case BRB:
2767 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2768 elem.note = pj_str("Be right back");
2769 break;
2770 case OFFLINE:
2771 online_status = PJ_FALSE;
2772 break;
2773 }
2774
2775 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2776}
2777
2778
2779/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002780 * Change codec priorities.
2781 */
2782static void manage_codec_prio(void)
2783{
2784 pjsua_codec_info c[32];
2785 unsigned i, count = PJ_ARRAY_SIZE(c);
2786 char input[32];
2787 char *codec, *prio;
2788 pj_str_t id;
2789 int new_prio;
2790 pj_status_t status;
2791
2792 printf("List of codecs:\n");
2793
2794 pjsua_enum_codecs(c, &count);
2795 for (i=0; i<count; ++i) {
2796 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2797 c[i].codec_id.ptr);
2798 }
2799
2800 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00002801 puts("Enter codec id and its new priority "
2802 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002803
Benny Prijono88accae2008-06-26 15:48:14 +00002804 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002805 fgets(input, sizeof(input), stdin);
2806 if (input[0]=='\r' || input[0]=='\n') {
2807 puts("Done");
2808 return;
2809 }
2810
2811 codec = strtok(input, " \t\r\n");
2812 prio = strtok(NULL, " \r\n");
2813
2814 if (!codec || !prio) {
2815 puts("Invalid input");
2816 return;
2817 }
2818
2819 new_prio = atoi(prio);
2820 if (new_prio < 0)
2821 new_prio = 0;
2822 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2823 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2824
2825 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2826 (pj_uint8_t)new_prio);
2827 if (status != PJ_SUCCESS)
2828 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2829}
2830
2831
2832/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002833 * Main "user interface" loop.
2834 */
2835void console_app_main(const pj_str_t *uri_to_call)
2836{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002837 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002838 char buf[128];
2839 char text[128];
2840 int i, count;
2841 char *uri;
2842 pj_str_t tmp;
2843 struct input_result result;
2844 pjsua_call_info call_info;
2845 pjsua_acc_info acc_info;
2846
2847
2848 /* If user specifies URI to call, then call the URI */
2849 if (uri_to_call->slen) {
2850 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2851 }
2852
2853 keystroke_help();
2854
2855 for (;;) {
2856
2857 printf(">>> ");
2858 fflush(stdout);
2859
Benny Prijono990042e2007-01-21 19:36:00 +00002860 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2861 /*
2862 * Be friendly to users who redirect commands into
2863 * program, when file ends, resume with kbd.
2864 * If exit is desired end script with q for quit
2865 */
2866 /* Reopen stdin/stdout/stderr to /dev/console */
2867#if defined(PJ_WIN32) && PJ_WIN32!=0
2868 if (freopen ("CONIN$", "r", stdin) == NULL) {
2869#else
2870 if (1) {
2871#endif
2872 puts("Cannot switch back to console from file redirection");
2873 menuin[0] = 'q';
2874 menuin[1] = '\0';
2875 } else {
2876 puts("Switched back to console from file redirection");
2877 continue;
2878 }
2879 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002880
Benny Prijonoebc32c32008-06-12 13:30:39 +00002881 if (cmd_echo) {
2882 printf("%s", menuin);
2883 }
2884
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002885 switch (menuin[0]) {
2886
2887 case 'm':
2888 /* Make call! : */
2889 printf("(You currently have %d calls)\n",
2890 pjsua_call_get_count());
2891
2892 uri = NULL;
2893 ui_input_url("Make call", buf, sizeof(buf), &result);
2894 if (result.nb_result != NO_NB) {
2895
2896 if (result.nb_result == -1 || result.nb_result == 0) {
2897 puts("You can't do that with make call!");
2898 continue;
2899 } else {
2900 pjsua_buddy_info binfo;
2901 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2902 uri = binfo.uri.ptr;
2903 }
2904
2905 } else if (result.uri_result) {
2906 uri = result.uri_result;
2907 }
2908
2909 tmp = pj_str(uri);
2910 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2911 break;
2912
2913 case 'M':
2914 /* Make multiple calls! : */
2915 printf("(You currently have %d calls)\n",
2916 pjsua_call_get_count());
2917
2918 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2919 continue;
2920
2921 count = my_atoi(menuin);
2922 if (count < 1)
2923 continue;
2924
2925 ui_input_url("Make call", buf, sizeof(buf), &result);
2926 if (result.nb_result != NO_NB) {
2927 pjsua_buddy_info binfo;
2928 if (result.nb_result == -1 || result.nb_result == 0) {
2929 puts("You can't do that with make call!");
2930 continue;
2931 }
2932 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2933 uri = binfo.uri.ptr;
2934 } else {
2935 uri = result.uri_result;
2936 }
2937
2938 for (i=0; i<my_atoi(menuin); ++i) {
2939 pj_status_t status;
2940
2941 tmp = pj_str(uri);
2942 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2943 NULL, NULL);
2944 if (status != PJ_SUCCESS)
2945 break;
2946 }
2947 break;
2948
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002949 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00002950 i = pjsua_detect_nat_type();
2951 if (i != PJ_SUCCESS)
2952 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002953 break;
2954
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002955 case 'i':
2956 /* Send instant messaeg */
2957
2958 /* i is for call index to send message, if any */
2959 i = -1;
2960
2961 /* Make compiler happy. */
2962 uri = NULL;
2963
2964 /* Input destination. */
2965 ui_input_url("Send IM to", buf, sizeof(buf), &result);
2966 if (result.nb_result != NO_NB) {
2967
2968 if (result.nb_result == -1) {
2969 puts("You can't send broadcast IM like that!");
2970 continue;
2971
2972 } else if (result.nb_result == 0) {
2973
2974 i = current_call;
2975
2976 } else {
2977 pjsua_buddy_info binfo;
2978 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2979 uri = binfo.uri.ptr;
2980 }
2981
2982 } else if (result.uri_result) {
2983 uri = result.uri_result;
2984 }
2985
2986
2987 /* Send typing indication. */
2988 if (i != -1)
2989 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
2990 else {
2991 pj_str_t tmp_uri = pj_str(uri);
2992 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
2993 }
2994
2995 /* Input the IM . */
2996 if (!simple_input("Message", text, sizeof(text))) {
2997 /*
2998 * Cancelled.
2999 * Send typing notification too, saying we're not typing.
3000 */
3001 if (i != -1)
3002 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3003 else {
3004 pj_str_t tmp_uri = pj_str(uri);
3005 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3006 }
3007 continue;
3008 }
3009
3010 tmp = pj_str(text);
3011
3012 /* Send the IM */
3013 if (i != -1)
3014 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3015 else {
3016 pj_str_t tmp_uri = pj_str(uri);
3017 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3018 }
3019
3020 break;
3021
3022 case 'a':
3023
3024 if (current_call != -1) {
3025 pjsua_call_get_info(current_call, &call_info);
3026 } else {
3027 /* Make compiler happy */
3028 call_info.role = PJSIP_ROLE_UAC;
3029 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3030 }
3031
3032 if (current_call == -1 ||
3033 call_info.role != PJSIP_ROLE_UAS ||
3034 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3035 {
3036 puts("No pending incoming call");
3037 fflush(stdout);
3038 continue;
3039
3040 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003041 int st_code;
3042 char contact[120];
3043 pj_str_t hname = { "Contact", 7 };
3044 pj_str_t hvalue;
3045 pjsip_generic_string_hdr hcontact;
3046 pjsua_msg_data msg_data;
3047
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003048 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3049 continue;
3050
Benny Prijono20d36722007-02-22 14:52:24 +00003051 st_code = my_atoi(buf);
3052 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003053 continue;
3054
Benny Prijono20d36722007-02-22 14:52:24 +00003055 pjsua_msg_data_init(&msg_data);
3056
3057 if (st_code/100 == 3) {
3058 if (!simple_input("Enter URL to be put in Contact",
3059 contact, sizeof(contact)))
3060 continue;
3061 hvalue = pj_str(contact);
3062 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3063
3064 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3065 }
3066
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003067 /*
3068 * Must check again!
3069 * Call may have been disconnected while we're waiting for
3070 * keyboard input.
3071 */
3072 if (current_call == -1) {
3073 puts("Call has been disconnected");
3074 fflush(stdout);
3075 continue;
3076 }
3077
Benny Prijono20d36722007-02-22 14:52:24 +00003078 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003079 }
3080
3081 break;
3082
3083
3084 case 'h':
3085
3086 if (current_call == -1) {
3087 puts("No current call");
3088 fflush(stdout);
3089 continue;
3090
3091 } else if (menuin[1] == 'a') {
3092
3093 /* Hangup all calls */
3094 pjsua_call_hangup_all();
3095
3096 } else {
3097
3098 /* Hangup current calls */
3099 pjsua_call_hangup(current_call, 0, NULL, NULL);
3100 }
3101 break;
3102
3103 case ']':
3104 case '[':
3105 /*
3106 * Cycle next/prev dialog.
3107 */
3108 if (menuin[0] == ']') {
3109 find_next_call();
3110
3111 } else {
3112 find_prev_call();
3113 }
3114
3115 if (current_call != -1) {
3116
3117 pjsua_call_get_info(current_call, &call_info);
3118 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3119 (int)call_info.remote_info.slen,
3120 call_info.remote_info.ptr));
3121
3122 } else {
3123 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3124 }
3125 break;
3126
3127
3128 case '>':
3129 case '<':
3130 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3131 break;
3132
3133 i = my_atoi(buf);
3134 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003135 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003136 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3137 } else {
3138 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3139 }
3140 break;
3141
3142
3143 case '+':
3144 if (menuin[1] == 'b') {
3145
3146 pjsua_buddy_config buddy_cfg;
3147 pjsua_buddy_id buddy_id;
3148 pj_status_t status;
3149
3150 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3151 break;
3152
3153 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3154 printf("Invalid SIP URI '%s'\n", buf);
3155 break;
3156 }
3157
Benny Prijonoac623b32006-07-03 15:19:31 +00003158 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003159
3160 buddy_cfg.uri = pj_str(buf);
3161 buddy_cfg.subscribe = PJ_TRUE;
3162
3163 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3164 if (status == PJ_SUCCESS) {
3165 printf("New buddy '%s' added at index %d\n",
3166 buf, buddy_id+1);
3167 }
3168
3169 } else if (menuin[1] == 'a') {
3170
Benny Prijonofb2b3652007-06-28 07:15:03 +00003171 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3172 pjsua_acc_config acc_cfg;
3173 pj_status_t status;
3174
3175 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3176 break;
3177 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3178 break;
3179 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3180 break;
3181 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3182 break;
3183 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3184 break;
3185
3186 pjsua_acc_config_default(&acc_cfg);
3187 acc_cfg.id = pj_str(id);
3188 acc_cfg.reg_uri = pj_str(registrar);
3189 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003190 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003191 acc_cfg.cred_info[0].realm = pj_str(realm);
3192 acc_cfg.cred_info[0].username = pj_str(uname);
3193 acc_cfg.cred_info[0].data_type = 0;
3194 acc_cfg.cred_info[0].data = pj_str(passwd);
3195
3196 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3197 if (status != PJ_SUCCESS) {
3198 pjsua_perror(THIS_FILE, "Error adding new account", status);
3199 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003200
3201 } else {
3202 printf("Invalid input %s\n", menuin);
3203 }
3204 break;
3205
3206 case '-':
3207 if (menuin[1] == 'b') {
3208 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3209 break;
3210
3211 i = my_atoi(buf) - 1;
3212
3213 if (!pjsua_buddy_is_valid(i)) {
3214 printf("Invalid buddy id %d\n", i);
3215 } else {
3216 pjsua_buddy_del(i);
3217 printf("Buddy %d deleted\n", i);
3218 }
3219
3220 } else if (menuin[1] == 'a') {
3221
3222 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3223 break;
3224
3225 i = my_atoi(buf);
3226
3227 if (!pjsua_acc_is_valid(i)) {
3228 printf("Invalid account id %d\n", i);
3229 } else {
3230 pjsua_acc_del(i);
3231 printf("Account %d deleted\n", i);
3232 }
3233
3234 } else {
3235 printf("Invalid input %s\n", menuin);
3236 }
3237 break;
3238
3239 case 'H':
3240 /*
3241 * Hold call.
3242 */
3243 if (current_call != -1) {
3244
3245 pjsua_call_set_hold(current_call, NULL);
3246
3247 } else {
3248 PJ_LOG(3,(THIS_FILE, "No current call"));
3249 }
3250 break;
3251
3252 case 'v':
3253 /*
3254 * Send re-INVITE (to release hold, etc).
3255 */
3256 if (current_call != -1) {
3257
3258 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3259
3260 } else {
3261 PJ_LOG(3,(THIS_FILE, "No current call"));
3262 }
3263 break;
3264
Benny Prijonoc08682e2007-10-04 06:17:58 +00003265 case 'U':
3266 /*
3267 * Send UPDATE
3268 */
3269 if (current_call != -1) {
3270
3271 pjsua_call_update(current_call, 0, NULL);
3272
3273 } else {
3274 PJ_LOG(3,(THIS_FILE, "No current call"));
3275 }
3276 break;
3277
3278 case 'C':
3279 if (menuin[1] == 'p') {
3280 manage_codec_prio();
3281 }
3282 break;
3283
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003284 case 'x':
3285 /*
3286 * Transfer call.
3287 */
3288 if (current_call == -1) {
3289
3290 PJ_LOG(3,(THIS_FILE, "No current call"));
3291
3292 } else {
3293 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003294 pjsua_msg_data msg_data;
3295 pjsip_generic_string_hdr refer_sub;
3296 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3297 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003298 pjsua_call_info ci;
3299
3300 pjsua_call_get_info(current_call, &ci);
3301 printf("Transfering current call [%d] %.*s\n",
3302 current_call,
3303 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003304
3305 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3306
3307 /* Check if call is still there. */
3308
3309 if (call != current_call) {
3310 puts("Call has been disconnected");
3311 continue;
3312 }
3313
Benny Prijonod524e822006-09-22 12:48:18 +00003314 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003315 if (app_config.no_refersub) {
3316 /* Add Refer-Sub: false in outgoing REFER request */
3317 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3318 &STR_FALSE);
3319 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3320 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003321 if (result.nb_result != NO_NB) {
3322 if (result.nb_result == -1 || result.nb_result == 0)
3323 puts("You can't do that with transfer call!");
3324 else {
3325 pjsua_buddy_info binfo;
3326 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003327 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003328 }
3329
3330 } else if (result.uri_result) {
3331 pj_str_t tmp;
3332 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003333 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003334 }
3335 }
3336 break;
3337
Benny Prijonof7b1c392006-11-11 16:46:34 +00003338 case 'X':
3339 /*
3340 * Transfer call with replaces.
3341 */
3342 if (current_call == -1) {
3343
3344 PJ_LOG(3,(THIS_FILE, "No current call"));
3345
3346 } else {
3347 int call = current_call;
3348 int dst_call;
3349 pjsua_msg_data msg_data;
3350 pjsip_generic_string_hdr refer_sub;
3351 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3352 pj_str_t STR_FALSE = { "false", 5 };
3353 pjsua_call_id ids[PJSUA_MAX_CALLS];
3354 pjsua_call_info ci;
3355 unsigned i, count;
3356
3357 count = PJ_ARRAY_SIZE(ids);
3358 pjsua_enum_calls(ids, &count);
3359
3360 if (count <= 1) {
3361 puts("There are no other calls");
3362 continue;
3363 }
3364
3365 pjsua_call_get_info(current_call, &ci);
3366 printf("Transfer call [%d] %.*s to one of the following:\n",
3367 current_call,
3368 (int)ci.remote_info.slen, ci.remote_info.ptr);
3369
3370 for (i=0; i<count; ++i) {
3371 pjsua_call_info call_info;
3372
3373 if (ids[i] == call)
3374 continue;
3375
3376 pjsua_call_get_info(ids[i], &call_info);
3377 printf("%d %.*s [%.*s]\n",
3378 ids[i],
3379 (int)call_info.remote_info.slen,
3380 call_info.remote_info.ptr,
3381 (int)call_info.state_text.slen,
3382 call_info.state_text.ptr);
3383 }
3384
3385 if (!simple_input("Enter call number to be replaced",
3386 buf, sizeof(buf)))
3387 continue;
3388
3389 dst_call = my_atoi(buf);
3390
3391 /* Check if call is still there. */
3392
3393 if (call != current_call) {
3394 puts("Call has been disconnected");
3395 continue;
3396 }
3397
3398 /* Check that destination call is valid. */
3399 if (dst_call == call) {
3400 puts("Destination call number must not be the same "
3401 "as the call being transfered");
3402 continue;
3403 }
3404 if (dst_call >= PJSUA_MAX_CALLS) {
3405 puts("Invalid destination call number");
3406 continue;
3407 }
3408 if (!pjsua_call_is_active(dst_call)) {
3409 puts("Invalid destination call number");
3410 continue;
3411 }
3412
3413 pjsua_msg_data_init(&msg_data);
3414 if (app_config.no_refersub) {
3415 /* Add Refer-Sub: false in outgoing REFER request */
3416 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3417 &STR_FALSE);
3418 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3419 }
3420
3421 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3422 }
3423 break;
3424
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003425 case '#':
3426 /*
3427 * Send DTMF strings.
3428 */
3429 if (current_call == -1) {
3430
3431 PJ_LOG(3,(THIS_FILE, "No current call"));
3432
3433 } else if (!pjsua_call_has_media(current_call)) {
3434
3435 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3436
3437 } else {
3438 pj_str_t digits;
3439 int call = current_call;
3440 pj_status_t status;
3441
3442 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3443 sizeof(buf)))
3444 {
3445 break;
3446 }
3447
3448 if (call != current_call) {
3449 puts("Call has been disconnected");
3450 continue;
3451 }
3452
3453 digits = pj_str(buf);
3454 status = pjsua_call_dial_dtmf(current_call, &digits);
3455 if (status != PJ_SUCCESS) {
3456 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3457 } else {
3458 puts("DTMF digits enqueued for transmission");
3459 }
3460 }
3461 break;
3462
Benny Prijonofeb69f42007-10-05 09:12:26 +00003463 case '*':
3464 /* Send DTMF with INFO */
3465 if (current_call == -1) {
3466
3467 PJ_LOG(3,(THIS_FILE, "No current call"));
3468
3469 } else {
3470 const pj_str_t SIP_INFO = pj_str("INFO");
3471 pj_str_t digits;
3472 int call = current_call;
3473 int i;
3474 pj_status_t status;
3475
3476 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3477 sizeof(buf)))
3478 {
3479 break;
3480 }
3481
3482 if (call != current_call) {
3483 puts("Call has been disconnected");
3484 continue;
3485 }
3486
3487 digits = pj_str(buf);
3488 for (i=0; i<digits.slen; ++i) {
3489 pjsua_msg_data msg_data;
3490 char body[80];
3491
3492 pjsua_msg_data_init(&msg_data);
3493 msg_data.content_type = pj_str("application/dtmf-relay");
3494
3495 pj_ansi_snprintf(body, sizeof(body),
3496 "Signal=%c\r\n"
3497 "Duration=160",
3498 buf[i]);
3499 msg_data.msg_body = pj_str(body);
3500
3501 status = pjsua_call_send_request(current_call, &SIP_INFO,
3502 &msg_data);
3503 if (status != PJ_SUCCESS) {
3504 break;
3505 }
3506 }
3507 }
3508 break;
3509
Benny Prijono56315612006-07-18 14:39:40 +00003510 case 'S':
3511 /*
3512 * Send arbitrary request
3513 */
3514 if (pjsua_acc_get_count() == 0) {
3515 puts("Sorry, need at least one account configured");
3516 break;
3517 }
3518
3519 puts("Send arbitrary request to remote host");
3520
3521 /* Input METHOD */
3522 if (!simple_input("Request method:",text,sizeof(text)))
3523 break;
3524
3525 /* Input destination URI */
3526 uri = NULL;
3527 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3528 if (result.nb_result != NO_NB) {
3529
3530 if (result.nb_result == -1 || result.nb_result == 0) {
3531 puts("Sorry you can't do that!");
3532 continue;
3533 } else {
3534 pjsua_buddy_info binfo;
3535 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3536 uri = binfo.uri.ptr;
3537 }
3538
3539 } else if (result.uri_result) {
3540 uri = result.uri_result;
3541 }
3542
3543 tmp = pj_str(uri);
3544
3545 send_request(text, &tmp);
3546 break;
3547
Benny Prijonoebc32c32008-06-12 13:30:39 +00003548 case 'e':
3549 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3550 pj_str_t tmp;
3551
3552 tmp.ptr = menuin+5;
3553 tmp.slen = pj_ansi_strlen(menuin)-6;
3554
3555 if (tmp.slen < 1) {
3556 puts("Usage: echo [0|1]");
3557 break;
3558 }
3559
Benny Prijonoddd02de2008-06-26 22:20:11 +00003560 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003561 }
3562 break;
3563
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003564 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003565 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3566 pj_str_t tmp;
3567 int delay;
3568
3569 tmp.ptr = menuin+6;
3570 tmp.slen = pj_ansi_strlen(menuin)-7;
3571
3572 if (tmp.slen < 1) {
3573 puts("Usage: sleep MSEC");
3574 break;
3575 }
3576
3577 delay = pj_strtoul(&tmp);
3578 if (delay < 0) delay = 0;
3579 pj_thread_sleep(delay);
3580 break;
3581 }
3582 /* Continue below */
3583
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003584 case 'u':
3585 /*
3586 * Subscribe/unsubscribe presence.
3587 */
3588 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3589 if (result.nb_result != NO_NB) {
3590 if (result.nb_result == -1) {
3591 int i, count;
3592 count = pjsua_get_buddy_count();
3593 for (i=0; i<count; ++i)
3594 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3595 } else if (result.nb_result == 0) {
3596 puts("Sorry, can only subscribe to buddy's presence, "
3597 "not from existing call");
3598 } else {
3599 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3600 }
3601
3602 } else if (result.uri_result) {
3603 puts("Sorry, can only subscribe to buddy's presence, "
3604 "not arbitrary URL (for now)");
3605 }
3606
3607 break;
3608
3609 case 'r':
3610 switch (menuin[1]) {
3611 case 'r':
3612 /*
3613 * Re-Register.
3614 */
3615 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3616 break;
3617 case 'u':
3618 /*
3619 * Unregister
3620 */
3621 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3622 break;
3623 }
3624 break;
3625
3626 case 't':
3627 pjsua_acc_get_info(current_acc, &acc_info);
3628 acc_info.online_status = !acc_info.online_status;
3629 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3630 printf("Setting %s online status to %s\n",
3631 acc_info.acc_uri.ptr,
3632 (acc_info.online_status?"online":"offline"));
3633 break;
3634
Benny Prijono4461c7d2007-08-25 13:36:15 +00003635 case 'T':
3636 change_online_status();
3637 break;
3638
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003639 case 'c':
3640 switch (menuin[1]) {
3641 case 'l':
3642 conf_list();
3643 break;
3644 case 'c':
3645 case 'd':
3646 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003647 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003648 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003649 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003650 const char *src_title, *dst_title;
3651
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003652 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003653
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003654 if (cnt != 3) {
3655 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003656
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003657 src_title = (menuin[1]=='c'?
3658 "Connect src port #":
3659 "Disconnect src port #");
3660 dst_title = (menuin[1]=='c'?
3661 "To dst port #":
3662 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003663
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003664 if (!simple_input(src_title, src_port, sizeof(src_port)))
3665 break;
3666
3667 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3668 break;
3669 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003670
3671 if (menuin[1]=='c') {
3672 status = pjsua_conf_connect(my_atoi(src_port),
3673 my_atoi(dst_port));
3674 } else {
3675 status = pjsua_conf_disconnect(my_atoi(src_port),
3676 my_atoi(dst_port));
3677 }
3678 if (status == PJ_SUCCESS) {
3679 puts("Success");
3680 } else {
3681 puts("ERROR!!");
3682 }
3683 }
3684 break;
3685 }
3686 break;
3687
Benny Prijono6dd967c2006-12-26 02:27:14 +00003688 case 'V':
3689 /* Adjust audio volume */
3690 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3691 if (simple_input(buf,text,sizeof(text))) {
3692 char *err;
3693 app_config.mic_level = (float)strtod(text, &err);
3694 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3695 }
3696 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3697 app_config.speaker_level);
3698 if (simple_input(buf,text,sizeof(text))) {
3699 char *err;
3700 app_config.speaker_level = (float)strtod(text, &err);
3701 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3702 }
3703
3704 break;
3705
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003706 case 'd':
3707 if (menuin[1] == 'c') {
3708 char settings[2000];
3709 int len;
3710
3711 len = write_settings(&app_config, settings, sizeof(settings));
3712 if (len < 1)
3713 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3714 else
3715 PJ_LOG(3,(THIS_FILE,
3716 "Dumping configuration (%d bytes):\n%s\n",
3717 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003718
3719 } else if (menuin[1] == 'q') {
3720
3721 if (current_call != PJSUA_INVALID_ID) {
Benny Prijono6eddd872008-03-21 13:46:08 +00003722 pjsua_call_dump(current_call, PJ_TRUE, some_buf,
3723 sizeof(some_buf), " ");
3724 PJ_LOG(3,(THIS_FILE, "\n%s", some_buf));
Benny Prijono819506c2006-06-22 22:29:51 +00003725 } else {
3726 PJ_LOG(3,(THIS_FILE, "No current call"));
3727 }
3728
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003729 } else {
3730 app_dump(menuin[1]=='d');
3731 }
3732 break;
3733
3734
3735 case 'f':
3736 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3737 char settings[2000];
3738 int len;
3739
3740 len = write_settings(&app_config, settings, sizeof(settings));
3741 if (len < 1)
3742 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3743 else {
3744 pj_oshandle_t fd;
3745 pj_status_t status;
3746
3747 status = pj_file_open(app_config.pool, buf,
3748 PJ_O_WRONLY, &fd);
3749 if (status != PJ_SUCCESS) {
3750 pjsua_perror(THIS_FILE, "Unable to open file", status);
3751 } else {
3752 pj_ssize_t size = len;
3753 pj_file_write(fd, settings, &size);
3754 pj_file_close(fd);
3755
3756 printf("Settings successfully written to '%s'\n", buf);
3757 }
3758 }
3759
3760 }
3761 break;
3762
3763
3764 case 'q':
3765 goto on_exit;
3766
3767
3768 default:
3769 if (menuin[0] != '\n' && menuin[0] != '\r') {
3770 printf("Invalid input %s", menuin);
3771 }
3772 keystroke_help();
3773 break;
3774 }
3775 }
3776
3777on_exit:
3778 ;
3779}
3780
3781
3782/*****************************************************************************
3783 * Public API
3784 */
3785
3786pj_status_t app_init(int argc, char *argv[])
3787{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003788 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003789 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003790 unsigned i;
3791 pj_status_t status;
3792
3793 /* Create pjsua */
3794 status = pjsua_create();
3795 if (status != PJ_SUCCESS)
3796 return status;
3797
3798 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00003799 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003800
3801 /* Initialize default config */
3802 default_config(&app_config);
3803
3804 /* Parse the arguments */
3805 status = parse_args(argc, argv, &app_config, &uri_arg);
3806 if (status != PJ_SUCCESS)
3807 return status;
3808
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003809 /* Initialize application callbacks */
3810 app_config.cfg.cb.on_call_state = &on_call_state;
3811 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3812 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003813 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003814 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003815 app_config.cfg.cb.on_reg_state = &on_reg_state;
3816 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3817 app_config.cfg.cb.on_pager = &on_pager;
3818 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003819 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003820 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003821 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003822
3823 /* Initialize pjsua */
3824 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3825 &app_config.media_cfg);
3826 if (status != PJ_SUCCESS)
3827 return status;
3828
Benny Prijonoe909eac2006-07-27 22:04:56 +00003829#ifdef STEREO_DEMO
3830 stereo_demo();
3831#endif
3832
Benny Prijono804ff0a2006-09-14 11:17:48 +00003833 /* Initialize calls data */
3834 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3835 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3836 app_config.call_data[i].timer.cb = &call_timeout_callback;
3837 }
3838
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003839 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003840 for (i=0; i<app_config.wav_count; ++i) {
3841 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003842 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00003843
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003844 if (app_config.auto_play_hangup)
3845 play_options |= PJMEDIA_FILE_NO_LOOP;
3846
3847 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00003848 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003849 if (status != PJ_SUCCESS)
3850 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003851
Benny Prijono72c04d22007-02-17 22:20:58 +00003852 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003853 app_config.wav_id = wav_id;
3854 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003855 if (app_config.auto_play_hangup) {
3856 pjmedia_port *port;
3857
3858 pjsua_player_get_port(app_config.wav_id, &port);
3859 status = pjmedia_wav_player_set_eof_cb(port, NULL,
3860 &on_playfile_done);
3861 if (status != PJ_SUCCESS)
3862 goto on_error;
3863
3864 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
3865 &hangup_timeout_callback);
3866 }
Benny Prijono32e4f492007-01-24 00:44:26 +00003867 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003868 }
3869
Benny Prijono4af234b2007-01-24 02:02:09 +00003870 /* Optionally registers tone players */
3871 for (i=0; i<app_config.tone_count; ++i) {
3872 pjmedia_port *tport;
3873 char name[80];
3874 pj_str_t label;
3875 pj_status_t status;
3876
3877 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3878 app_config.tones[i].freq1,
3879 app_config.tones[i].freq2);
3880 label = pj_str(name);
3881 status = pjmedia_tonegen_create2(app_config.pool, &label,
3882 8000, 1, 160, 16,
3883 PJMEDIA_TONEGEN_LOOP, &tport);
3884 if (status != PJ_SUCCESS) {
3885 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3886 goto on_error;
3887 }
3888
3889 status = pjsua_conf_add_port(app_config.pool, tport,
3890 &app_config.tone_slots[i]);
3891 pj_assert(status == PJ_SUCCESS);
3892
3893 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3894 pj_assert(status == PJ_SUCCESS);
3895 }
3896
Benny Prijono1ebd6142006-10-19 15:48:02 +00003897 /* Optionally create recorder file, if any. */
3898 if (app_config.rec_file.slen) {
3899 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3900 &app_config.rec_id);
3901 if (status != PJ_SUCCESS)
3902 goto on_error;
3903
3904 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3905 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003906
Benny Prijonoe347cb02007-02-14 14:36:13 +00003907 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3908
Benny Prijono91d20f42008-06-14 19:42:37 +00003909 /* Create ringback tones */
3910 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00003911 unsigned i, samples_per_frame;
3912 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00003913 pj_str_t name;
3914
Benny Prijono4f966892008-06-14 22:43:56 +00003915 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
3916 app_config.media_cfg.clock_rate *
3917 app_config.media_cfg.channel_count / 1000;
3918
Benny Prijono91d20f42008-06-14 19:42:37 +00003919 /* Ringback tone (call is ringing) */
3920 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00003921 status = pjmedia_tonegen_create2(app_config.pool, &name,
3922 app_config.media_cfg.clock_rate,
3923 app_config.media_cfg.channel_count,
3924 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003925 16, PJMEDIA_TONEGEN_LOOP,
3926 &app_config.ringback_port);
3927 if (status != PJ_SUCCESS)
3928 goto on_error;
3929
Benny Prijonoe28cec82008-06-14 20:40:53 +00003930 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00003931 for (i=0; i<RINGBACK_CNT; ++i) {
3932 tone[i].freq1 = RINGBACK_FREQ1;
3933 tone[i].freq2 = RINGBACK_FREQ2;
3934 tone[i].on_msec = RINGBACK_ON;
3935 tone[i].off_msec = RINGBACK_OFF;
3936 }
3937 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003938
Benny Prijono4f966892008-06-14 22:43:56 +00003939 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00003940 PJMEDIA_TONEGEN_LOOP);
3941
3942
Benny Prijono91d20f42008-06-14 19:42:37 +00003943 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
3944 &app_config.ringback_slot);
3945 if (status != PJ_SUCCESS)
3946 goto on_error;
3947
3948 /* Ring (to alert incoming call) */
3949 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00003950 status = pjmedia_tonegen_create2(app_config.pool, &name,
3951 app_config.media_cfg.clock_rate,
3952 app_config.media_cfg.channel_count,
3953 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003954 16, PJMEDIA_TONEGEN_LOOP,
3955 &app_config.ring_port);
3956 if (status != PJ_SUCCESS)
3957 goto on_error;
3958
Benny Prijono4f966892008-06-14 22:43:56 +00003959 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00003960 tone[i].freq1 = RING_FREQ1;
3961 tone[i].freq2 = RING_FREQ2;
3962 tone[i].on_msec = RING_ON;
3963 tone[i].off_msec = RING_OFF;
3964 }
Benny Prijono4f966892008-06-14 22:43:56 +00003965 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00003966
Benny Prijono4f966892008-06-14 22:43:56 +00003967 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00003968 tone, PJMEDIA_TONEGEN_LOOP);
3969
Benny Prijono91d20f42008-06-14 19:42:37 +00003970 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
3971 &app_config.ring_slot);
3972 if (status != PJ_SUCCESS)
3973 goto on_error;
3974
3975 }
3976
Benny Prijono87ef89a2007-01-14 00:39:45 +00003977 /* Add UDP transport unless it's disabled. */
3978 if (!app_config.no_udp) {
3979 pjsua_acc_id aid;
3980
3981 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
3982 &app_config.udp_cfg,
3983 &transport_id);
3984 if (status != PJ_SUCCESS)
3985 goto on_error;
3986
3987 /* Add local account */
3988 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
3989 //pjsua_acc_set_transport(aid, transport_id);
3990 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00003991
3992 if (app_config.udp_cfg.port == 0) {
3993 pjsua_transport_info ti;
3994 pj_sockaddr_in *a;
3995
3996 pjsua_transport_get_info(transport_id, &ti);
3997 a = (pj_sockaddr_in*)&ti.local_addr;
3998
3999 tcp_cfg.port = pj_ntohs(a->sin_port);
4000 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004001 }
4002
Benny Prijonoe93e2872006-06-28 16:46:49 +00004003 /* Add TCP transport unless it's disabled */
4004 if (!app_config.no_tcp) {
4005 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004006 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004007 &transport_id);
4008 if (status != PJ_SUCCESS)
4009 goto on_error;
4010
4011 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004012 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004013 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4014
4015 }
4016
Benny Prijonoe93e2872006-06-28 16:46:49 +00004017
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004018#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4019 /* Add TLS transport when application wants one */
4020 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004021
4022 pjsua_acc_id acc_id;
4023
4024 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004025 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004026 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004027 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004028 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004029 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004030 if (status != PJ_SUCCESS)
4031 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004032
4033 /* Add local account */
4034 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4035 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004036 }
4037#endif
4038
Benny Prijonoe93e2872006-06-28 16:46:49 +00004039 if (transport_id == -1) {
4040 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
4041 status = -1;
4042 goto on_error;
4043 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004044
4045
4046 /* Add accounts */
4047 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004048 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004049 if (status != PJ_SUCCESS)
4050 goto on_error;
4051 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4052 }
4053
4054 /* Add buddies */
4055 for (i=0; i<app_config.buddy_cnt; ++i) {
4056 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4057 if (status != PJ_SUCCESS)
4058 goto on_error;
4059 }
4060
4061 /* Optionally set codec orders */
4062 for (i=0; i<app_config.codec_cnt; ++i) {
4063 pjsua_codec_set_priority(&app_config.codec_arg[i],
4064 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4065 }
4066
Benny Prijonofce28542007-12-09 15:41:10 +00004067 /* Optionally disable some codec */
4068 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4069 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4070 }
4071
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004072 /* Add RTP transports */
4073 status = pjsua_media_transports_create(&app_config.rtp_cfg);
4074 if (status != PJ_SUCCESS)
4075 goto on_error;
4076
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004077 /* Set sound device latency */
4078 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4079
Benny Prijono22a300a2006-06-14 20:04:55 +00004080 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004081#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004082 if (app_config.null_audio) {
4083 status = pjsua_set_null_snd_dev();
4084 if (status != PJ_SUCCESS)
4085 return status;
4086 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004087#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004088
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004089 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4090 app_config.playback_dev != PJSUA_INVALID_ID)
4091 {
4092 status = pjsua_set_snd_dev(app_config.capture_dev,
4093 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004094 if (status != PJ_SUCCESS)
4095 goto on_error;
4096 }
4097
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004098 return PJ_SUCCESS;
4099
4100on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004101 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004102 return status;
4103}
4104
4105
Benny Prijonoebc32c32008-06-12 13:30:39 +00004106static int stdout_refresh_proc(void *arg)
4107{
4108 PJ_UNUSED_ARG(arg);
4109
4110 /* Set thread to lowest priority so that it doesn't clobber
4111 * stdout output
4112 */
4113 pj_thread_set_prio(pj_thread_this(),
4114 pj_thread_get_prio_min(pj_thread_this()));
4115
4116 while (!stdout_refresh_quit) {
4117 pj_thread_sleep(stdout_refresh * 1000);
4118 puts(stdout_refresh_text);
4119 fflush(stdout);
4120 }
4121
4122 return 0;
4123}
4124
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004125pj_status_t app_main(void)
4126{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004127 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004128 pj_status_t status;
4129
4130 /* Start pjsua */
4131 status = pjsua_start();
4132 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004133 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004134 return status;
4135 }
4136
Benny Prijonoebc32c32008-06-12 13:30:39 +00004137 /* Start console refresh thread */
4138 if (stdout_refresh > 0) {
4139 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4140 NULL, 0, 0, &stdout_refresh_thread);
4141 }
4142
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004143 console_app_main(&uri_arg);
4144
Benny Prijonoebc32c32008-06-12 13:30:39 +00004145 if (stdout_refresh_thread) {
4146 stdout_refresh_quit = PJ_TRUE;
4147 pj_thread_join(stdout_refresh_thread);
4148 pj_thread_destroy(stdout_refresh_thread);
4149 }
4150
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004151 return PJ_SUCCESS;
4152}
4153
4154pj_status_t app_destroy(void)
4155{
Benny Prijonof762ee72006-12-01 11:14:37 +00004156 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004157 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004158
Benny Prijonoe909eac2006-07-27 22:04:56 +00004159#ifdef STEREO_DEMO
4160 if (app_config.snd) {
4161 pjmedia_snd_port_destroy(app_config.snd);
4162 app_config.snd = NULL;
4163 }
4164#endif
4165
Benny Prijono91d20f42008-06-14 19:42:37 +00004166 /* Close ringback port */
4167 if (app_config.ringback_port &&
4168 app_config.ringback_slot != PJSUA_INVALID_ID)
4169 {
4170 pjsua_conf_remove_port(app_config.ringback_slot);
4171 app_config.ringback_slot = PJSUA_INVALID_ID;
4172 pjmedia_port_destroy(app_config.ringback_port);
4173 app_config.ringback_port = NULL;
4174 }
4175
4176 /* Close ring port */
4177 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4178 pjsua_conf_remove_port(app_config.ring_slot);
4179 app_config.ring_slot = PJSUA_INVALID_ID;
4180 pjmedia_port_destroy(app_config.ring_port);
4181 app_config.ring_port = NULL;
4182 }
4183
Benny Prijono4af234b2007-01-24 02:02:09 +00004184 /* Close tone generators */
4185 for (i=0; i<app_config.tone_count; ++i) {
4186 pjsua_conf_remove_port(app_config.tone_slots[i]);
4187 }
4188
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004189 if (app_config.pool) {
4190 pj_pool_release(app_config.pool);
4191 app_config.pool = NULL;
4192 }
4193
Benny Prijonof762ee72006-12-01 11:14:37 +00004194 status = pjsua_destroy();
4195
4196 pj_bzero(&app_config, sizeof(app_config));
4197
4198 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004199}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004200
4201
4202#ifdef STEREO_DEMO
4203static void stereo_demo()
4204{
4205 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004206 pj_status_t status;
4207
4208 /* Disable existing sound device */
4209 conf = pjsua_set_no_snd_dev();
4210
Benny Prijonoe909eac2006-07-27 22:04:56 +00004211 /* Create stereo-mono splitter/combiner */
4212 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004213 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004214 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004215 2 * conf->info.samples_per_frame,
4216 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004217 0 /* options */,
4218 &splitter);
4219 pj_assert(status == PJ_SUCCESS);
4220
4221 /* Connect channel0 (left channel?) to conference port slot0 */
4222 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4223 0 /*options*/,
4224 conf);
4225 pj_assert(status == PJ_SUCCESS);
4226
4227 /* Create reverse channel for channel1 (right channel?)... */
4228 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4229 splitter,
4230 1 /* ch1 */,
4231 0 /* options */,
4232 &ch1);
4233 pj_assert(status == PJ_SUCCESS);
4234
4235 /* .. and register it to conference bridge (it would be slot1
4236 * if there's no other devices connected to the bridge)
4237 */
4238 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4239 pj_assert(status == PJ_SUCCESS);
4240
4241 /* Create sound device */
4242 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004243 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004244 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004245 2 * conf->info.samples_per_frame,
4246 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004247 0, &app_config.snd);
4248 pj_assert(status == PJ_SUCCESS);
4249
4250
4251 /* Connect the splitter to the sound device */
4252 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4253 pj_assert(status == PJ_SUCCESS);
4254
4255}
4256#endif
4257