blob: bbe40e955fd81ba1a251da979c82fdddbc5c4fd0 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20
21
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000022#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000023#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000024
Benny Prijonoe909eac2006-07-27 22:04:56 +000025//#define STEREO_DEMO
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026
Benny Prijono4f966892008-06-14 22:43:56 +000027/* Ringtones US UK */
28#define RINGBACK_FREQ1 440 /* 400 */
29#define RINGBACK_FREQ2 480 /* 450 */
30#define RINGBACK_ON 2000 /* 400 */
31#define RINGBACK_OFF 4000 /* 200 */
32#define RINGBACK_CNT 1 /* 2 */
33#define RINGBACK_INTERVAL 4000 /* 2000 */
Benny Prijono91d20f42008-06-14 19:42:37 +000034
Benny Prijono4f966892008-06-14 22:43:56 +000035#define RING_FREQ1 800
36#define RING_FREQ2 640
37#define RING_ON 200
38#define RING_OFF 100
39#define RING_CNT 3
40#define RING_INTERVAL 3000
Benny Prijono91d20f42008-06-14 19:42:37 +000041
42
Benny Prijono804ff0a2006-09-14 11:17:48 +000043/* Call specific data */
44struct call_data
45{
46 pj_timer_entry timer;
Benny Prijono91d20f42008-06-14 19:42:37 +000047 pj_bool_t ringback_on;
48 pj_bool_t ring_on;
Benny Prijono804ff0a2006-09-14 11:17:48 +000049};
50
Benny Prijonoeebe9af2006-06-13 22:57:13 +000051
52/* Pjsua application data */
53static struct app_config
54{
55 pjsua_config cfg;
56 pjsua_logging_config log_cfg;
57 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000058 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000059 pj_bool_t no_tcp;
60 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000061 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000062 pjsua_transport_config udp_cfg;
63 pjsua_transport_config rtp_cfg;
64
65 unsigned acc_cnt;
66 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
67
68 unsigned buddy_cnt;
69 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
70
Benny Prijono804ff0a2006-09-14 11:17:48 +000071 struct call_data call_data[PJSUA_MAX_CALLS];
72
Benny Prijonoeebe9af2006-06-13 22:57:13 +000073 pj_pool_t *pool;
74 /* Compatibility with older pjsua */
75
76 unsigned codec_cnt;
77 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000078 unsigned codec_dis_cnt;
79 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000080 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000081 unsigned wav_count;
82 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000083 unsigned tone_count;
84 pjmedia_tone_desc tones[32];
85 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000086 pjsua_player_id wav_id;
87 pjsua_conf_port_id wav_port;
88 pj_bool_t auto_play;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +000089 pj_bool_t auto_play_hangup;
90 pj_timer_entry auto_hangup_timer;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000091 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000092 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000093 pj_str_t rec_file;
94 pj_bool_t auto_rec;
95 pjsua_recorder_id rec_id;
96 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000097 unsigned auto_answer;
98 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +000099
100#ifdef STEREO_DEMO
101 pjmedia_snd_port *snd;
102#endif
103
Benny Prijono6dd967c2006-12-26 02:27:14 +0000104 float mic_level,
105 speaker_level;
106
Benny Prijono4e5d5512007-03-06 18:11:30 +0000107 int capture_dev, playback_dev;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000108 unsigned capture_lat, playback_lat;
Benny Prijono91d20f42008-06-14 19:42:37 +0000109
110 pj_bool_t no_tones;
111 int ringback_slot;
112 int ringback_cnt;
113 pjmedia_port *ringback_port;
114 int ring_slot;
115 int ring_cnt;
116 pjmedia_port *ring_port;
117
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000118} app_config;
119
120
Benny Prijono21b9ad92006-08-15 13:11:22 +0000121//static pjsua_acc_id current_acc;
122#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +0000123static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoebc32c32008-06-12 13:30:39 +0000124static pj_bool_t cmd_echo;
125static int stdout_refresh = -1;
126static const char *stdout_refresh_text = "STDOUT_REFRESH";
127static pj_bool_t stdout_refresh_quit = PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000128static pj_str_t uri_arg;
129
Nanang Izzuddin660eee82008-07-17 14:54:03 +0000130static char some_buf[1024 * 3];
Benny Prijono6eddd872008-03-21 13:46:08 +0000131
Benny Prijono594e4c52006-09-14 18:51:01 +0000132#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +0000133static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +0000134#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000135pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000136
Benny Prijono91d20f42008-06-14 19:42:37 +0000137static void ringback_start(pjsua_call_id call_id);
138static void ring_start(pjsua_call_id call_id);
139static void ring_stop(pjsua_call_id call_id);
140
141
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000142/*****************************************************************************
143 * Configuration manipulation
144 */
145
146/* Show usage */
147static void usage(void)
148{
149 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000150 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000151 puts ("");
152 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000153 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000154 puts (" --help Display this help screen");
155 puts (" --version Display version info");
156 puts ("");
157 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000158 puts (" --log-file=fname Log to filename (default stderr)");
159 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
160 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000161 puts (" --color Use colorful logging (default yes on Win32)");
162 puts (" --no-color Disable colorful logging");
Benny Prijonob7944862008-07-19 20:53:49 +0000163 puts (" --light-bg Use dark colors for light background (default is dark bg)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000164
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000165 puts ("");
166 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000167 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000168#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000169 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
170 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000171#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000172 puts (" --registrar=url Set the URL of registrar server");
173 puts (" --id=url Set the URL of local ID (used in From header)");
174 puts (" --contact=url Optionally override the Contact information");
175 puts (" --proxy=url Optional URL of proxy server to visit");
176 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000177 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178 puts (" --realm=string Set realm");
179 puts (" --username=string Set authentication username");
180 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000181 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000182 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000183 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
184 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000185 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000186 puts ("");
187 puts ("SIP Account Control:");
188 puts (" --next-account Add more account");
189 puts ("");
190 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000191 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
192 puts (" TCP and UDP transports on the specified port, unless");
193 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000194 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
195 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000196 puts (" --no-tcp Disable TCP transport.");
197 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000198 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
199 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000200 puts (" --outbound=url Set the URL of global outbound proxy server");
201 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000202 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000203 puts ("");
204 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000205 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000206 puts (" --tls-ca-file Specify TLS CA file (default=none)");
207 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
208 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
209 puts (" --tls-password Specify TLS password to private key file (default=none)");
210 puts (" --tls-verify-server Verify server's certificate (default=no)");
211 puts (" --tls-verify-client Verify client's certificate (default=no)");
212 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
Benny Prijonoe10db842008-07-01 15:31:59 +0000213 puts (" --tls-srv-name Specify TLS server name for multi-hosting server (optional)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000214
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000215 puts ("");
216 puts ("Media Options:");
217 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000218 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000219 puts (" --clock-rate=N Override conference bridge clock rate");
220 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000221 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000222 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000223 puts (" --play-file=file Register WAV file in conference bridge.");
224 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000225 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
226 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
227 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000228 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000229 puts (" --auto-play Automatically play the file (to incoming calls only)");
230 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000231 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000232 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
233 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000234 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000235 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000236 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000237 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono00cae612006-07-31 15:19:36 +0000238 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000239 puts (" --capture-dev=id Audio capture device ID (default=-1)");
240 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000241 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000242 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000243 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
244 puts (" Specify N=-1 (default) to disable this feature.");
245 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000246 puts (" --no-tones Disable audible tones");
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000247 puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000248
Benny Prijonof76e1392008-06-06 14:51:48 +0000249 puts ("");
250 puts ("Media Transport Options:");
251 puts (" --use-ice Enable ICE (default:no)");
252 puts (" --ice-no-host Disable ICE host candidates");
253 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
254 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
255 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
256 puts (" --use-turn Enable TURN relay with ICE (default:no)");
257 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
258 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
259 puts (" --turn-user TURN username");
260 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000261
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000262 puts ("");
263 puts ("Buddy List (can be more than one):");
264 puts (" --add-buddy url Add the specified URL to the buddy list.");
265 puts ("");
266 puts ("User Agent options:");
267 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
268 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000269 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000271 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000272 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000273
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000274 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000275 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
276 puts ("");
277
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000278 fflush(stdout);
279}
280
281
282/* Set default config. */
283static void default_config(struct app_config *cfg)
284{
Benny Prijono56315612006-07-18 14:39:40 +0000285 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000286 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000287
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000288 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000289 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000290 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
291
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000292 pjsua_logging_config_default(&cfg->log_cfg);
293 pjsua_media_config_default(&cfg->media_cfg);
294 pjsua_transport_config_default(&cfg->udp_cfg);
295 cfg->udp_cfg.port = 5060;
296 pjsua_transport_config_default(&cfg->rtp_cfg);
297 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000298 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000299 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000300 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000301 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000302 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000303 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000304 cfg->capture_dev = PJSUA_INVALID_ID;
305 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000306 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
307 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000308 cfg->ringback_slot = PJSUA_INVALID_ID;
309 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000310
311 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
312 pjsua_acc_config_default(&cfg->acc_cfg[i]);
313
314 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
315 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000316}
317
318
319/*
320 * Read command arguments from config file.
321 */
322static int read_config_file(pj_pool_t *pool, const char *filename,
323 int *app_argc, char ***app_argv)
324{
325 int i;
326 FILE *fhnd;
327 char line[200];
328 int argc = 0;
329 char **argv;
330 enum { MAX_ARGS = 64 };
331
332 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
333 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
334 argv[argc++] = *app_argv[0];
335
336 /* Open config file. */
337 fhnd = fopen(filename, "rt");
338 if (!fhnd) {
339 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
340 fflush(stdout);
341 return -1;
342 }
343
344 /* Scan tokens in the file. */
345 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000346 char *token;
347 char *p;
348 const char *whitespace = " \t\r\n";
349 char cDelimiter;
350 int len, token_len;
351
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000352 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000353
354 // Trim ending newlines
355 len = strlen(line);
356 if (line[len-1]=='\n')
357 line[--len] = '\0';
358 if (line[len-1]=='\r')
359 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000360
Benny Prijonobf5b4692007-06-28 03:20:17 +0000361 if (len==0) continue;
362
363 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
364 // first, scan whitespaces
365 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
366
367 if (*p == '\0') // are we done yet?
368 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000369
Benny Prijonobf5b4692007-06-28 03:20:17 +0000370 if (*p == '"' || *p == '\'') { // is token a quoted string
371 cDelimiter = *p++; // save quote delimiter
372 token = p;
373
374 while (*p != '\0' && *p != cDelimiter) p++;
375
376 if (*p == '\0') // found end of the line, but,
377 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378
Benny Prijonobf5b4692007-06-28 03:20:17 +0000379 } else { // token's not a quoted string
380 token = p;
381
382 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
383
384 cDelimiter = *p;
385 }
386
387 *p = '\0';
388 token_len = p-token;
389
390 if (token_len > 0) {
391 if (*token == '#')
392 break; // ignore remainder of line
393
394 argv[argc] = pj_pool_alloc(pool, token_len + 1);
395 pj_memcpy(argv[argc], token, token_len + 1);
396 ++argc;
397 }
398
399 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000400 }
401 }
402
403 /* Copy arguments from command line */
404 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
405 argv[argc++] = (*app_argv)[i];
406
407 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
408 PJ_LOG(1,(THIS_FILE,
409 "Too many arguments specified in cmd line/config file"));
410 fflush(stdout);
411 fclose(fhnd);
412 return -1;
413 }
414
415 fclose(fhnd);
416
417 /* Assign the new command line back to the original command line. */
418 *app_argc = argc;
419 *app_argv = argv;
420 return 0;
421
422}
423
424static int my_atoi(const char *cs)
425{
426 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000427
428 pj_cstr(&s, cs);
429 if (cs[0] == '-') {
430 s.ptr++, s.slen--;
431 return 0 - (int)pj_strtoul(&s);
432 } else if (cs[0] == '+') {
433 s.ptr++, s.slen--;
434 return pj_strtoul(&s);
435 } else {
436 return pj_strtoul(&s);
437 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000438}
439
440
441/* Parse arguments. */
442static pj_status_t parse_args(int argc, char *argv[],
443 struct app_config *cfg,
444 pj_str_t *uri_to_call)
445{
446 int c;
447 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000448 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonob7944862008-07-19 20:53:49 +0000449 OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000450 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000451 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
452 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000453 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000454 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000455 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000456 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000457 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
458 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijonob7944862008-07-19 20:53:49 +0000459 OPT_USE_TURN, OPT_ICE_NO_HOST, OPT_TURN_SRV, OPT_TURN_TCP,
Benny Prijonof76e1392008-06-06 14:51:48 +0000460 OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000461 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
462 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000463 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijonod79f25c2006-08-02 19:41:37 +0000464 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000465 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000466 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000467 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000468 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
469 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000470 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000471 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000472 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000473 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000474#ifdef _IONBF
475 OPT_STDOUT_NO_BUF,
476#endif
Benny Prijonofce28542007-12-09 15:41:10 +0000477 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000478 };
479 struct pj_getopt_option long_options[] = {
480 { "config-file",1, 0, OPT_CONFIG_FILE},
481 { "log-file", 1, 0, OPT_LOG_FILE},
482 { "log-level", 1, 0, OPT_LOG_LEVEL},
483 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
Benny Prijonod6e362a2008-07-19 17:53:47 +0000484 { "color", 0, 0, OPT_COLOR},
485 { "no-color", 0, 0, OPT_NO_COLOR},
Benny Prijonob7944862008-07-19 20:53:49 +0000486 { "light-bg", 0, 0, OPT_LIGHT_BG},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487 { "help", 0, 0, OPT_HELP},
488 { "version", 0, 0, OPT_VERSION},
489 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000490 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000491 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492 { "null-audio", 0, 0, OPT_NULL_AUDIO},
493 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000494 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000495 { "no-tcp", 0, 0, OPT_NO_TCP},
496 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000497 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 { "proxy", 1, 0, OPT_PROXY},
499 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
500 { "registrar", 1, 0, OPT_REGISTRAR},
501 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000502 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000503 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000504 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 { "id", 1, 0, OPT_ID},
506 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000507 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
508 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 { "realm", 1, 0, OPT_REALM},
510 { "username", 1, 0, OPT_USERNAME},
511 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000512 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000513 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000514 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000515 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
516 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
517 { "no-presence", 0, 0, OPT_NO_PRESENCE},
518 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000520 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000521 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000522 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
523 { "auto-conf", 0, 0, OPT_AUTO_CONF},
524 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000525 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000526 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000527 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000528
Benny Prijonoc97608e2007-03-23 16:34:20 +0000529 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000530 { "use-turn", 0, 0, OPT_USE_TURN},
531 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
532 { "turn-srv", 1, 0, OPT_TURN_SRV},
533 { "turn-tcp", 0, 0, OPT_TURN_TCP},
534 { "turn-user", 1, 0, OPT_TURN_USER},
535 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
536
Benny Prijonod8179652008-01-23 20:39:07 +0000537#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
538 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000539 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000540#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000541 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000542 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000543 { "complexity", 1, 0, OPT_COMPLEXITY},
544 { "quality", 1, 0, OPT_QUALITY},
545 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000546 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000547 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono00cae612006-07-31 15:19:36 +0000548 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
549 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
550 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000551 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000552 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000553 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000554 { "duration", 1, 0, OPT_DURATION},
555 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000556 { "use-tls", 0, 0, OPT_USE_TLS},
557 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000558 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
559 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000560 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000561 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
562 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
563 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000564 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000565 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
566 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000567 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
568 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000569 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
570 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000571#ifdef _IONBF
572 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
573#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000574 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000575 { "no-tones", 0, 0, OPT_NO_TONES},
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000576 { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000577 { NULL, 0, 0, 0}
578 };
579 pj_status_t status;
580 pjsua_acc_config *cur_acc;
581 char *config_file = NULL;
582 unsigned i;
583
584 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000585 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000586 while ((c=pj_getopt_long(argc, argv, "", long_options,
587 &option_index)) != -1)
588 {
589 switch (c) {
590 case OPT_CONFIG_FILE:
591 config_file = pj_optarg;
592 break;
593 }
594 if (config_file)
595 break;
596 }
597
598 if (config_file) {
599 status = read_config_file(app_config.pool, config_file, &argc, &argv);
600 if (status != 0)
601 return status;
602 }
603
604 cfg->acc_cnt = 0;
605 cur_acc = &cfg->acc_cfg[0];
606
607
608 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
609 * read from config file.
610 */
611 pj_optind = 0;
612 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000613 pj_str_t tmp;
614 long lval;
615
616 switch (c) {
617
Benny Prijono6f137482006-06-15 11:31:36 +0000618 case OPT_CONFIG_FILE:
619 /* Ignore as this has been processed before */
620 break;
621
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000622 case OPT_LOG_FILE:
623 cfg->log_cfg.log_filename = pj_str(pj_optarg);
624 break;
625
626 case OPT_LOG_LEVEL:
627 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
628 if (c < 0 || c > 6) {
629 PJ_LOG(1,(THIS_FILE,
630 "Error: expecting integer value 0-6 "
631 "for --log-level"));
632 return PJ_EINVAL;
633 }
634 cfg->log_cfg.level = c;
635 pj_log_set_level( c );
636 break;
637
638 case OPT_APP_LOG_LEVEL:
639 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
640 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
641 PJ_LOG(1,(THIS_FILE,
642 "Error: expecting integer value 0-6 "
643 "for --app-log-level"));
644 return PJ_EINVAL;
645 }
646 break;
647
Benny Prijonod6e362a2008-07-19 17:53:47 +0000648 case OPT_COLOR:
649 cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
650 break;
651
652 case OPT_NO_COLOR:
653 cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
654 break;
655
Benny Prijonob7944862008-07-19 20:53:49 +0000656 case OPT_LIGHT_BG:
657 pj_log_set_color(1, PJ_TERM_COLOR_R);
658 pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
659 pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
660 pj_log_set_color(4, 0);
661 pj_log_set_color(5, 0);
662 pj_log_set_color(77, 0);
663 break;
664
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000665 case OPT_HELP:
666 usage();
667 return PJ_EINVAL;
668
669 case OPT_VERSION: /* version */
670 pj_dump_config();
671 return PJ_EINVAL;
672
673 case OPT_NULL_AUDIO:
674 cfg->null_audio = PJ_TRUE;
675 break;
676
677 case OPT_CLOCK_RATE:
678 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000679 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000680 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000681 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000682 return PJ_EINVAL;
683 }
684 cfg->media_cfg.clock_rate = lval;
685 break;
686
Benny Prijonof3758ee2008-02-26 15:32:16 +0000687 case OPT_SND_CLOCK_RATE:
688 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000689 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000690 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000691 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000692 return PJ_EINVAL;
693 }
694 cfg->media_cfg.snd_clock_rate = lval;
695 break;
696
Benny Prijono7d60d052008-03-29 12:24:20 +0000697 case OPT_STEREO:
698 cfg->media_cfg.channel_count = 2;
699 break;
700
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000701 case OPT_LOCAL_PORT: /* local-port */
702 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000703 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000704 PJ_LOG(1,(THIS_FILE,
705 "Error: expecting integer value for "
706 "--local-port"));
707 return PJ_EINVAL;
708 }
709 cfg->udp_cfg.port = (pj_uint16_t)lval;
710 break;
711
Benny Prijono0a5cad82006-09-26 13:21:02 +0000712 case OPT_IP_ADDR: /* ip-addr */
713 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
714 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
715 break;
716
Benny Prijonoe93e2872006-06-28 16:46:49 +0000717 case OPT_NO_UDP: /* no-udp */
718 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000719 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
720 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000721 }
722
723 cfg->no_udp = PJ_TRUE;
724 break;
725
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000726 case OPT_NOREFERSUB: /* norefersub */
727 cfg->no_refersub = PJ_TRUE;
728 break;
729
Benny Prijonoe93e2872006-06-28 16:46:49 +0000730 case OPT_NO_TCP: /* no-tcp */
731 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000732 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
733 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000734 }
735
736 cfg->no_tcp = PJ_TRUE;
737 break;
738
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000739 case OPT_PROXY: /* proxy */
740 if (pjsua_verify_sip_url(pj_optarg) != 0) {
741 PJ_LOG(1,(THIS_FILE,
742 "Error: invalid SIP URL '%s' "
743 "in proxy argument", pj_optarg));
744 return PJ_EINVAL;
745 }
746 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
747 break;
748
749 case OPT_OUTBOUND_PROXY: /* outbound proxy */
750 if (pjsua_verify_sip_url(pj_optarg) != 0) {
751 PJ_LOG(1,(THIS_FILE,
752 "Error: invalid SIP URL '%s' "
753 "in outbound proxy argument", pj_optarg));
754 return PJ_EINVAL;
755 }
756 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
757 break;
758
759 case OPT_REGISTRAR: /* registrar */
760 if (pjsua_verify_sip_url(pj_optarg) != 0) {
761 PJ_LOG(1,(THIS_FILE,
762 "Error: invalid SIP URL '%s' in "
763 "registrar argument", pj_optarg));
764 return PJ_EINVAL;
765 }
766 cur_acc->reg_uri = pj_str(pj_optarg);
767 break;
768
769 case OPT_REG_TIMEOUT: /* reg-timeout */
770 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
771 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
772 PJ_LOG(1,(THIS_FILE,
773 "Error: invalid value for --reg-timeout "
774 "(expecting 1-3600)"));
775 return PJ_EINVAL;
776 }
777 break;
778
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000779 case OPT_PUBLISH: /* publish */
780 cur_acc->publish_enabled = PJ_TRUE;
781 break;
782
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000783 case OPT_100REL: /** 100rel */
784 cur_acc->require_100rel = PJ_TRUE;
785 cfg->cfg.require_100rel = PJ_TRUE;
786 break;
787
Benny Prijono48ab2b72007-11-08 09:24:30 +0000788 case OPT_USE_IMS: /* Activate IMS settings */
789 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000790 break;
791
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000792 case OPT_ID: /* id */
793 if (pjsua_verify_sip_url(pj_optarg) != 0) {
794 PJ_LOG(1,(THIS_FILE,
795 "Error: invalid SIP URL '%s' "
796 "in local id argument", pj_optarg));
797 return PJ_EINVAL;
798 }
799 cur_acc->id = pj_str(pj_optarg);
800 break;
801
802 case OPT_CONTACT: /* contact */
803 if (pjsua_verify_sip_url(pj_optarg) != 0) {
804 PJ_LOG(1,(THIS_FILE,
805 "Error: invalid SIP URL '%s' "
806 "in contact argument", pj_optarg));
807 return PJ_EINVAL;
808 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000809 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000810 break;
811
Benny Prijonofce28542007-12-09 15:41:10 +0000812 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000813 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000814 break;
815
816 case OPT_USE_COMPACT_FORM:
817 /* enable compact form - from Ticket #342 */
818 {
819 extern pj_bool_t pjsip_use_compact_form;
820 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
821 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
822
823 pjsip_use_compact_form = PJ_TRUE;
824 /* do not transmit Allow header */
825 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
826 /* Do not include rtpmap for static payload types (<96) */
827 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
828 }
829 break;
830
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000831 case OPT_NEXT_ACCOUNT: /* Add more account. */
832 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000833 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000834 break;
835
836 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000837 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000838 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000839 break;
840
841 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000842 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000843 break;
844
845 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000846 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
847 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000848#if PJSIP_HAS_DIGEST_AKA_AUTH
849 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
850 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
851 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
852#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000853 break;
854
855 case OPT_NEXT_CRED: /* next credential */
856 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000857 break;
858
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000859 case OPT_NAMESERVER: /* nameserver */
860 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
861 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
862 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
863 return PJ_ETOOMANY;
864 }
865 break;
866
Benny Prijonoebbf6892007-03-24 17:37:25 +0000867 case OPT_STUN_DOMAIN: /* STUN domain */
868 cfg->cfg.stun_domain = pj_str(pj_optarg);
869 break;
870
Benny Prijonoc97608e2007-03-23 16:34:20 +0000871 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000872 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000873 break;
874
875 case OPT_ADD_BUDDY: /* Add to buddy list. */
876 if (pjsua_verify_sip_url(pj_optarg) != 0) {
877 PJ_LOG(1,(THIS_FILE,
878 "Error: invalid URL '%s' in "
879 "--add-buddy option", pj_optarg));
880 return -1;
881 }
882 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
883 PJ_LOG(1,(THIS_FILE,
884 "Error: too many buddies in buddy list."));
885 return -1;
886 }
887 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
888 cfg->buddy_cnt++;
889 break;
890
891 case OPT_AUTO_PLAY:
892 cfg->auto_play = 1;
893 break;
894
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000895 case OPT_AUTO_PLAY_HANGUP:
896 cfg->auto_play_hangup = 1;
897 break;
898
Benny Prijono1ebd6142006-10-19 15:48:02 +0000899 case OPT_AUTO_REC:
900 cfg->auto_rec = 1;
901 break;
902
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000903 case OPT_AUTO_LOOP:
904 cfg->auto_loop = 1;
905 break;
906
Benny Prijono7ca96da2006-08-07 12:11:40 +0000907 case OPT_AUTO_CONF:
908 cfg->auto_conf = 1;
909 break;
910
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000911 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000912 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000913 break;
914
Benny Prijono4af234b2007-01-24 02:02:09 +0000915 case OPT_PLAY_TONE:
916 {
917 int f1, f2, on, off;
918 int n;
919
920 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
921 if (n != 4) {
922 puts("Expecting f1,f2,on,off in --play-tone");
923 return -1;
924 }
925
926 cfg->tones[cfg->tone_count].freq1 = (short)f1;
927 cfg->tones[cfg->tone_count].freq2 = (short)f2;
928 cfg->tones[cfg->tone_count].on_msec = (short)on;
929 cfg->tones[cfg->tone_count].off_msec = (short)off;
930 ++cfg->tone_count;
931 }
932 break;
933
Benny Prijono1ebd6142006-10-19 15:48:02 +0000934 case OPT_REC_FILE:
935 cfg->rec_file = pj_str(pj_optarg);
936 break;
937
Benny Prijonoc97608e2007-03-23 16:34:20 +0000938 case OPT_USE_ICE:
939 cfg->media_cfg.enable_ice = PJ_TRUE;
940 break;
941
Benny Prijonof76e1392008-06-06 14:51:48 +0000942 case OPT_USE_TURN:
943 cfg->media_cfg.enable_turn = PJ_TRUE;
944 break;
945
946 case OPT_ICE_NO_HOST:
947 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
948 break;
949
950 case OPT_TURN_SRV:
951 cfg->media_cfg.turn_server = pj_str(pj_optarg);
952 break;
953
954 case OPT_TURN_TCP:
955 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
956 break;
957
958 case OPT_TURN_USER:
959 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
960 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
961 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
962 break;
963
964 case OPT_TURN_PASSWD:
965 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
966 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
967 break;
968
Benny Prijonod8179652008-01-23 20:39:07 +0000969#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
970 case OPT_USE_SRTP:
971 app_config.cfg.use_srtp = my_atoi(pj_optarg);
972 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
973 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
974 return -1;
975 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000976 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +0000977 break;
Benny Prijonof6508982008-01-25 09:02:33 +0000978 case OPT_SRTP_SECURE:
979 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
980 if (!pj_isdigit(*pj_optarg) ||
981 app_config.cfg.srtp_secure_signaling > 2)
982 {
983 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
984 return -1;
985 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000986 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +0000987 break;
Benny Prijonod8179652008-01-23 20:39:07 +0000988#endif
989
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000990 case OPT_RTP_PORT:
991 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +0000992 if (cfg->rtp_cfg.port == 0) {
993 enum { START_PORT=4000 };
994 unsigned range;
995
996 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
997 cfg->rtp_cfg.port = START_PORT +
998 ((pj_rand() % range) & 0xFFFE);
999 }
1000
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001001 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
1002 PJ_LOG(1,(THIS_FILE,
1003 "Error: rtp-port argument value "
1004 "(expecting 1-65535"));
1005 return -1;
1006 }
1007 break;
1008
Benny Prijonofce28542007-12-09 15:41:10 +00001009 case OPT_DIS_CODEC:
1010 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
1011 break;
1012
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001013 case OPT_ADD_CODEC:
1014 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
1015 break;
1016
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001017 /* These options were no longer valid after new pjsua */
1018 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001019 case OPT_COMPLEXITY:
1020 cfg->complexity = my_atoi(pj_optarg);
1021 if (cfg->complexity < 0 || cfg->complexity > 10) {
1022 PJ_LOG(1,(THIS_FILE,
1023 "Error: invalid --complexity (expecting 0-10"));
1024 return -1;
1025 }
1026 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001027 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001028
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001029 case OPT_DURATION:
1030 cfg->duration = my_atoi(pj_optarg);
1031 break;
1032
Benny Prijonof521eb02006-08-06 23:07:25 +00001033 case OPT_THREAD_CNT:
1034 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1035 if (cfg->cfg.thread_cnt > 128) {
1036 PJ_LOG(1,(THIS_FILE,
1037 "Error: invalid --thread-cnt option"));
1038 return -1;
1039 }
1040 break;
1041
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001042 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001043 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1044 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 PJ_LOG(1,(THIS_FILE,
1046 "Error: invalid --ptime option"));
1047 return -1;
1048 }
1049 break;
1050
Benny Prijono0a12f002006-07-26 17:05:39 +00001051 case OPT_NO_VAD:
1052 cfg->media_cfg.no_vad = PJ_TRUE;
1053 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001054
Benny Prijonod79f25c2006-08-02 19:41:37 +00001055 case OPT_EC_TAIL:
1056 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1057 if (cfg->media_cfg.ec_tail_len > 1000) {
1058 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1059 "is too big"));
1060 return -1;
1061 }
1062 break;
1063
Benny Prijono0498d902006-06-19 14:49:14 +00001064 case OPT_QUALITY:
1065 cfg->media_cfg.quality = my_atoi(pj_optarg);
1066 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1067 PJ_LOG(1,(THIS_FILE,
1068 "Error: invalid --quality (expecting 0-10"));
1069 return -1;
1070 }
1071 break;
1072
Benny Prijono00cae612006-07-31 15:19:36 +00001073 case OPT_ILBC_MODE:
1074 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1075 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1076 PJ_LOG(1,(THIS_FILE,
1077 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1078 return -1;
1079 }
1080 break;
1081
1082 case OPT_RX_DROP_PCT:
1083 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1084 if (cfg->media_cfg.rx_drop_pct > 100) {
1085 PJ_LOG(1,(THIS_FILE,
1086 "Error: invalid --rx-drop-pct (expecting <= 100"));
1087 return -1;
1088 }
1089 break;
1090
1091 case OPT_TX_DROP_PCT:
1092 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1093 if (cfg->media_cfg.tx_drop_pct > 100) {
1094 PJ_LOG(1,(THIS_FILE,
1095 "Error: invalid --tx-drop-pct (expecting <= 100"));
1096 return -1;
1097 }
1098 break;
1099
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001100 case OPT_AUTO_ANSWER:
1101 cfg->auto_answer = my_atoi(pj_optarg);
1102 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1103 PJ_LOG(1,(THIS_FILE,
1104 "Error: invalid code in --auto-answer "
1105 "(expecting 100-699"));
1106 return -1;
1107 }
1108 break;
1109
1110 case OPT_MAX_CALLS:
1111 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001112 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001113 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1114 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001115 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001116 return -1;
1117 }
1118 break;
1119
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001120 case OPT_USE_TLS:
1121 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001122#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1123 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1124 return -1;
1125#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001126 break;
1127
1128 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001129 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1130#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1131 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1132 return -1;
1133#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001134 break;
1135
Benny Prijonof3bbc132006-12-25 06:43:59 +00001136 case OPT_TLS_CERT_FILE:
1137 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1138#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1139 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1140 return -1;
1141#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001142 break;
1143
Benny Prijonof3bbc132006-12-25 06:43:59 +00001144 case OPT_TLS_PRIV_FILE:
1145 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1146 break;
1147
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001148 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001149 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1150#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1151 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1152 return -1;
1153#endif
1154 break;
1155
1156 case OPT_TLS_VERIFY_SERVER:
1157 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1158 break;
1159
1160 case OPT_TLS_VERIFY_CLIENT:
1161 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1162 break;
1163
1164 case OPT_TLS_NEG_TIMEOUT:
1165 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001166 break;
1167
Benny Prijonoe10db842008-07-01 15:31:59 +00001168 case OPT_TLS_SRV_NAME:
1169 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1170 break;
1171
Benny Prijono4e5d5512007-03-06 18:11:30 +00001172 case OPT_CAPTURE_DEV:
1173 cfg->capture_dev = atoi(pj_optarg);
1174 break;
1175
1176 case OPT_PLAYBACK_DEV:
1177 cfg->playback_dev = atoi(pj_optarg);
1178 break;
1179
Benny Prijonoebc32c32008-06-12 13:30:39 +00001180 case OPT_STDOUT_REFRESH:
1181 stdout_refresh = atoi(pj_optarg);
1182 break;
1183
1184 case OPT_STDOUT_REFRESH_TEXT:
1185 stdout_refresh_text = pj_optarg;
1186 break;
1187
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001188#ifdef _IONBF
1189 case OPT_STDOUT_NO_BUF:
1190 setvbuf(stdout, NULL, _IONBF, 0);
1191 break;
1192#endif
1193
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001194 case OPT_CAPTURE_LAT:
1195 cfg->capture_lat = atoi(pj_optarg);
1196 break;
1197
1198 case OPT_PLAYBACK_LAT:
1199 cfg->playback_lat = atoi(pj_optarg);
1200 break;
1201
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001202 case OPT_SND_AUTO_CLOSE:
1203 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1204 break;
1205
Benny Prijono91d20f42008-06-14 19:42:37 +00001206 case OPT_NO_TONES:
1207 cfg->no_tones = PJ_TRUE;
1208 break;
1209
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001210 case OPT_JB_MAX_SIZE:
1211 cfg->media_cfg.jb_max = atoi(pj_optarg);
1212 break;
1213
Benny Prijono22a300a2006-06-14 20:04:55 +00001214 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001215 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001216 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001217 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001218 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001219 }
1220 }
1221
1222 if (pj_optind != argc) {
1223 pj_str_t uri_arg;
1224
1225 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1226 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1227 return -1;
1228 }
1229 uri_arg = pj_str(argv[pj_optind]);
1230 if (uri_to_call)
1231 *uri_to_call = uri_arg;
1232 pj_optind++;
1233
1234 /* Add URI to call to buddy list if it's not already there */
1235 for (i=0; i<cfg->buddy_cnt; ++i) {
1236 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1237 break;
1238 }
1239 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1240 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1241 }
1242
1243 } else {
1244 if (uri_to_call)
1245 uri_to_call->slen = 0;
1246 }
1247
1248 if (pj_optind != argc) {
1249 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1250 return PJ_EINVAL;
1251 }
1252
Benny Prijono56315612006-07-18 14:39:40 +00001253 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1254 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001255
1256 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001257 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1258
1259 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001260 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001261 acfg->cred_count++;
1262 }
1263
1264 /* When IMS mode is enabled for the account, verify that settings
1265 * are okay.
1266 */
1267 /* For now we check if IMS mode is activated by looking if
1268 * initial_auth is set.
1269 */
1270 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1271 /* Realm must point to the real domain */
1272 if (*acfg->cred_info[0].realm.ptr=='*') {
1273 PJ_LOG(1,(THIS_FILE,
1274 "Error: cannot use '*' as realm with IMS"));
1275 return PJ_EINVAL;
1276 }
1277
1278 /* Username for authentication must be in a@b format */
1279 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1280 PJ_LOG(1,(THIS_FILE,
1281 "Error: Username for authentication must "
1282 "be in user@domain format with IMS"));
1283 return PJ_EINVAL;
1284 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001285 }
1286 }
1287
1288
1289 return PJ_SUCCESS;
1290}
1291
1292
1293/*
1294 * Save account settings
1295 */
1296static void write_account_settings(int acc_index, pj_str_t *result)
1297{
1298 unsigned i;
1299 char line[128];
1300 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1301
1302
1303 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1304 pj_strcat2(result, line);
1305
1306
1307 /* Identity */
1308 if (acc_cfg->id.slen) {
1309 pj_ansi_sprintf(line, "--id %.*s\n",
1310 (int)acc_cfg->id.slen,
1311 acc_cfg->id.ptr);
1312 pj_strcat2(result, line);
1313 }
1314
1315 /* Registrar server */
1316 if (acc_cfg->reg_uri.slen) {
1317 pj_ansi_sprintf(line, "--registrar %.*s\n",
1318 (int)acc_cfg->reg_uri.slen,
1319 acc_cfg->reg_uri.ptr);
1320 pj_strcat2(result, line);
1321
1322 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1323 acc_cfg->reg_timeout);
1324 pj_strcat2(result, line);
1325 }
1326
1327 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001328 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001329 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001330 (int)acc_cfg->force_contact.slen,
1331 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001332 pj_strcat2(result, line);
1333 }
1334
Benny Prijonofce28542007-12-09 15:41:10 +00001335 /* */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001336 if (acc_cfg->allow_contact_rewrite==0)
Benny Prijonofce28542007-12-09 15:41:10 +00001337 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001338 pj_ansi_sprintf(line, "--contact-rewrite %i\n",
1339 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001340 pj_strcat2(result, line);
1341 }
1342
Benny Prijonod8179652008-01-23 20:39:07 +00001343#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1344 /* SRTP */
1345 if (acc_cfg->use_srtp) {
1346 pj_ansi_sprintf(line, "--use-srtp %i\n",
1347 (int)acc_cfg->use_srtp);
1348 pj_strcat2(result, line);
1349 }
1350#endif
1351
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001352 /* Proxy */
1353 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1354 pj_ansi_sprintf(line, "--proxy %.*s\n",
1355 (int)acc_cfg->proxy[i].slen,
1356 acc_cfg->proxy[i].ptr);
1357 pj_strcat2(result, line);
1358 }
1359
1360 /* Credentials */
1361 for (i=0; i<acc_cfg->cred_count; ++i) {
1362 if (acc_cfg->cred_info[i].realm.slen) {
1363 pj_ansi_sprintf(line, "--realm %.*s\n",
1364 (int)acc_cfg->cred_info[i].realm.slen,
1365 acc_cfg->cred_info[i].realm.ptr);
1366 pj_strcat2(result, line);
1367 }
1368
1369 if (acc_cfg->cred_info[i].username.slen) {
1370 pj_ansi_sprintf(line, "--username %.*s\n",
1371 (int)acc_cfg->cred_info[i].username.slen,
1372 acc_cfg->cred_info[i].username.ptr);
1373 pj_strcat2(result, line);
1374 }
1375
1376 if (acc_cfg->cred_info[i].data.slen) {
1377 pj_ansi_sprintf(line, "--password %.*s\n",
1378 (int)acc_cfg->cred_info[i].data.slen,
1379 acc_cfg->cred_info[i].data.ptr);
1380 pj_strcat2(result, line);
1381 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001382
1383 if (i != acc_cfg->cred_count - 1)
1384 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001385 }
1386
1387}
1388
1389
1390/*
1391 * Write settings.
1392 */
1393static int write_settings(const struct app_config *config,
1394 char *buf, pj_size_t max)
1395{
1396 unsigned acc_index;
1397 unsigned i;
1398 pj_str_t cfg;
1399 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001400 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001401
1402 PJ_UNUSED_ARG(max);
1403
1404 cfg.ptr = buf;
1405 cfg.slen = 0;
1406
1407 /* Logging. */
1408 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1409 pj_ansi_sprintf(line, "--log-level %d\n",
1410 config->log_cfg.level);
1411 pj_strcat2(&cfg, line);
1412
1413 pj_ansi_sprintf(line, "--app-log-level %d\n",
1414 config->log_cfg.console_level);
1415 pj_strcat2(&cfg, line);
1416
1417 if (config->log_cfg.log_filename.slen) {
1418 pj_ansi_sprintf(line, "--log-file %.*s\n",
1419 (int)config->log_cfg.log_filename.slen,
1420 config->log_cfg.log_filename.ptr);
1421 pj_strcat2(&cfg, line);
1422 }
1423
1424
1425 /* Save account settings. */
1426 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1427
1428 write_account_settings(acc_index, &cfg);
1429
1430 if (acc_index < config->acc_cnt-1)
1431 pj_strcat2(&cfg, "--next-account\n");
1432 }
1433
1434
1435 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1436
1437 /* Outbound proxy */
1438 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1439 pj_ansi_sprintf(line, "--outbound %.*s\n",
1440 (int)config->cfg.outbound_proxy[i].slen,
1441 config->cfg.outbound_proxy[i].ptr);
1442 pj_strcat2(&cfg, line);
1443 }
1444
1445
1446 /* UDP Transport. */
1447 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1448 pj_strcat2(&cfg, line);
1449
Benny Prijono0a5cad82006-09-26 13:21:02 +00001450 /* IP address, if any. */
1451 if (config->udp_cfg.public_addr.slen) {
1452 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1453 (int)config->udp_cfg.public_addr.slen,
1454 config->udp_cfg.public_addr.ptr);
1455 pj_strcat2(&cfg, line);
1456 }
1457
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001458 /* No TCP ? */
1459 if (config->no_tcp) {
1460 pj_strcat2(&cfg, "--no-tcp\n");
1461 }
1462
1463 /* No UDP ? */
1464 if (config->no_udp) {
1465 pj_strcat2(&cfg, "--no-udp\n");
1466 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001467
1468 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001469 if (config->cfg.stun_domain.slen) {
1470 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1471 (int)config->cfg.stun_domain.slen,
1472 config->cfg.stun_domain.ptr);
1473 pj_strcat2(&cfg, line);
1474 }
1475 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001476 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001477 (int)config->cfg.stun_host.slen,
1478 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001479 pj_strcat2(&cfg, line);
1480 }
1481
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001482 /* TLS */
1483 if (config->use_tls)
1484 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001485 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001486 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001487 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1488 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001489 pj_strcat2(&cfg, line);
1490 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001491 if (config->udp_cfg.tls_setting.cert_file.slen) {
1492 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1493 (int)config->udp_cfg.tls_setting.cert_file.slen,
1494 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001495 pj_strcat2(&cfg, line);
1496 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001497 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1498 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1499 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1500 config->udp_cfg.tls_setting.privkey_file.ptr);
1501 pj_strcat2(&cfg, line);
1502 }
1503
1504 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001505 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001506 (int)config->udp_cfg.tls_setting.password.slen,
1507 config->udp_cfg.tls_setting.password.ptr);
1508 pj_strcat2(&cfg, line);
1509 }
1510
Benny Prijonoe10db842008-07-01 15:31:59 +00001511 if (config->udp_cfg.tls_setting.server_name.slen) {
1512 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1513 (int)config->udp_cfg.tls_setting.server_name.slen,
1514 config->udp_cfg.tls_setting.server_name.ptr);
1515 pj_strcat2(&cfg, line);
1516 }
1517
Benny Prijonof3bbc132006-12-25 06:43:59 +00001518 if (config->udp_cfg.tls_setting.verify_server)
1519 pj_strcat2(&cfg, "--tls-verify-server\n");
1520
1521 if (config->udp_cfg.tls_setting.verify_client)
1522 pj_strcat2(&cfg, "--tls-verify-client\n");
1523
1524 if (config->udp_cfg.tls_setting.timeout.sec) {
1525 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001526 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001527 pj_strcat2(&cfg, line);
1528 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001529
1530 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1531
Benny Prijonof6508982008-01-25 09:02:33 +00001532 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001533#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001534 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1535 pj_ansi_sprintf(line, "--use-srtp %d\n",
1536 app_config.cfg.use_srtp);
1537 pj_strcat2(&cfg, line);
1538 }
1539 if (app_config.cfg.srtp_secure_signaling !=
1540 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1541 {
1542 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1543 app_config.cfg.srtp_secure_signaling);
1544 pj_strcat2(&cfg, line);
1545 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001546#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001547
Benny Prijonof76e1392008-06-06 14:51:48 +00001548 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001549 if (config->media_cfg.enable_ice)
1550 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001551
1552 if (config->media_cfg.enable_turn)
1553 pj_strcat2(&cfg, "--use-turn\n");
1554
1555 if (config->media_cfg.ice_no_host_cands)
1556 pj_strcat2(&cfg, "--ice-no-host\n");
1557
1558 if (config->media_cfg.turn_server.slen) {
1559 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1560 (int)config->media_cfg.turn_server.slen,
1561 config->media_cfg.turn_server.ptr);
1562 pj_strcat2(&cfg, line);
1563 }
1564
1565 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1566 pj_strcat2(&cfg, "--turn-tcp\n");
1567
1568 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1569 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1570 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1571 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1572 pj_strcat2(&cfg, line);
1573 }
1574
1575 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1576 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1577 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1578 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1579 pj_strcat2(&cfg, line);
1580 }
1581
1582 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001583 if (config->null_audio)
1584 pj_strcat2(&cfg, "--null-audio\n");
1585 if (config->auto_play)
1586 pj_strcat2(&cfg, "--auto-play\n");
1587 if (config->auto_loop)
1588 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001589 if (config->auto_conf)
1590 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001591 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001592 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001593 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001594 pj_strcat2(&cfg, line);
1595 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001596 for (i=0; i<config->tone_count; ++i) {
1597 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1598 config->tones[i].freq1, config->tones[i].freq2,
1599 config->tones[i].on_msec, config->tones[i].off_msec);
1600 pj_strcat2(&cfg, line);
1601 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001602 if (config->rec_file.slen) {
1603 pj_ansi_sprintf(line, "--rec-file %s\n",
1604 config->rec_file.ptr);
1605 pj_strcat2(&cfg, line);
1606 }
1607 if (config->auto_rec)
1608 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001609 if (config->capture_dev != PJSUA_INVALID_ID) {
1610 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1611 pj_strcat2(&cfg, line);
1612 }
1613 if (config->playback_dev != PJSUA_INVALID_ID) {
1614 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1615 pj_strcat2(&cfg, line);
1616 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001617 if (config->media_cfg.snd_auto_close_time != -1) {
1618 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1619 config->media_cfg.snd_auto_close_time);
1620 pj_strcat2(&cfg, line);
1621 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001622 if (config->no_tones) {
1623 pj_strcat2(&cfg, "--no-tones\n");
1624 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001625 if (config->media_cfg.jb_max != -1) {
1626 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1627 config->media_cfg.jb_max);
1628 pj_strcat2(&cfg, line);
1629 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001630
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001631 /* Sound device latency */
1632 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1633 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1634 pj_strcat2(&cfg, line);
1635 }
1636 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1637 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1638 pj_strcat2(&cfg, line);
1639 }
1640
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001641 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001642 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001643 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001644 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001645 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001646 } else {
1647 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1648 config->media_cfg.clock_rate);
1649 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001650 }
Benny Prijono70972992006-08-05 11:13:58 +00001651
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001652 if (config->media_cfg.snd_clock_rate &&
1653 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1654 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001655 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1656 config->media_cfg.snd_clock_rate);
1657 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001658 }
1659
Benny Prijono7d60d052008-03-29 12:24:20 +00001660 /* Stereo mode. */
1661 if (config->media_cfg.channel_count == 2) {
1662 pj_ansi_sprintf(line, "--stereo\n");
1663 pj_strcat2(&cfg, line);
1664 }
1665
Benny Prijono70972992006-08-05 11:13:58 +00001666 /* quality */
1667 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001668 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001669 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001670 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001671 } else {
1672 pj_ansi_sprintf(line, "#using default --quality %d\n",
1673 config->media_cfg.quality);
1674 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001675 }
Benny Prijono0498d902006-06-19 14:49:14 +00001676
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001677
1678 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001679 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001680 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001681 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001682 pj_strcat2(&cfg, line);
1683 }
1684
Benny Prijono70972992006-08-05 11:13:58 +00001685 /* no-vad */
1686 if (config->media_cfg.no_vad) {
1687 pj_strcat2(&cfg, "--no-vad\n");
1688 }
1689
1690 /* ec-tail */
1691 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1692 pj_ansi_sprintf(line, "--ec-tail %d\n",
1693 config->media_cfg.ec_tail_len);
1694 pj_strcat2(&cfg, line);
1695 } else {
1696 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1697 config->media_cfg.ec_tail_len);
1698 pj_strcat2(&cfg, line);
1699 }
1700
1701
1702 /* ilbc-mode */
1703 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1704 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1705 config->media_cfg.ilbc_mode);
1706 pj_strcat2(&cfg, line);
1707 } else {
1708 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1709 config->media_cfg.ilbc_mode);
1710 pj_strcat2(&cfg, line);
1711 }
1712
1713 /* RTP drop */
1714 if (config->media_cfg.tx_drop_pct) {
1715 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1716 config->media_cfg.tx_drop_pct);
1717 pj_strcat2(&cfg, line);
1718
1719 }
1720 if (config->media_cfg.rx_drop_pct) {
1721 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1722 config->media_cfg.rx_drop_pct);
1723 pj_strcat2(&cfg, line);
1724
1725 }
1726
1727
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001728 /* Start RTP port. */
1729 pj_ansi_sprintf(line, "--rtp-port %d\n",
1730 config->rtp_cfg.port);
1731 pj_strcat2(&cfg, line);
1732
1733 /* Add codec. */
1734 for (i=0; i<config->codec_cnt; ++i) {
1735 pj_ansi_sprintf(line, "--add-codec %s\n",
1736 config->codec_arg[i].ptr);
1737 pj_strcat2(&cfg, line);
1738 }
Benny Prijonofce28542007-12-09 15:41:10 +00001739 /* Disable codec */
1740 for (i=0; i<config->codec_dis_cnt; ++i) {
1741 pj_ansi_sprintf(line, "--dis-codec %s\n",
1742 config->codec_dis[i].ptr);
1743 pj_strcat2(&cfg, line);
1744 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001745
1746 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1747
1748 /* Auto-answer. */
1749 if (config->auto_answer != 0) {
1750 pj_ansi_sprintf(line, "--auto-answer %d\n",
1751 config->auto_answer);
1752 pj_strcat2(&cfg, line);
1753 }
1754
1755 /* Max calls. */
1756 pj_ansi_sprintf(line, "--max-calls %d\n",
1757 config->cfg.max_calls);
1758 pj_strcat2(&cfg, line);
1759
1760 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001761 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001762 pj_ansi_sprintf(line, "--duration %d\n",
1763 config->duration);
1764 pj_strcat2(&cfg, line);
1765 }
1766
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001767 /* norefersub ? */
1768 if (config->no_refersub) {
1769 pj_strcat2(&cfg, "--norefersub\n");
1770 }
1771
Benny Prijonofce28542007-12-09 15:41:10 +00001772 if (pjsip_use_compact_form)
1773 {
1774 pj_strcat2(&cfg, "--use-compact-form\n");
1775 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001776
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001777 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1778
1779 /* Add buddies. */
1780 for (i=0; i<config->buddy_cnt; ++i) {
1781 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1782 (int)config->buddy_cfg[i].uri.slen,
1783 config->buddy_cfg[i].uri.ptr);
1784 pj_strcat2(&cfg, line);
1785 }
1786
1787
1788 *(cfg.ptr + cfg.slen) = '\0';
1789 return cfg.slen;
1790}
1791
1792
1793/*
1794 * Dump application states.
1795 */
1796static void app_dump(pj_bool_t detail)
1797{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001798 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001799}
1800
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001801/*
1802 * Print log of call states. Since call states may be too long for logger,
1803 * printing it is a bit tricky, it should be printed part by part as long
1804 * as the logger can accept.
1805 */
1806static void log_call_dump(int call_id) {
1807 unsigned call_dump_len;
1808 unsigned part_len;
1809 unsigned part_idx;
1810 unsigned log_decor;
1811
1812 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1813 sizeof(some_buf), " ");
1814 call_dump_len = strlen(some_buf);
1815
1816 log_decor = pj_log_get_decor();
1817 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1818 PJ_LOG(3,(THIS_FILE, "\n"));
1819 pj_log_set_decor(0);
1820
1821 part_idx = 0;
1822 part_len = PJ_LOG_MAX_SIZE-80;
1823 while (part_idx < call_dump_len) {
1824 char p_orig, *p;
1825
1826 p = &some_buf[part_idx];
1827 if (part_idx + part_len > call_dump_len)
1828 part_len = call_dump_len - part_idx;
1829 p_orig = p[part_len];
1830 p[part_len] = '\0';
1831 PJ_LOG(3,(THIS_FILE, "%s", p));
1832 p[part_len] = p_orig;
1833 part_idx += part_len;
1834 }
1835 pj_log_set_decor(log_decor);
1836}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001837
1838/*****************************************************************************
1839 * Console application
1840 */
1841
Benny Prijono91d20f42008-06-14 19:42:37 +00001842static void ringback_start(pjsua_call_id call_id)
1843{
1844 if (app_config.no_tones)
1845 return;
1846
1847 if (app_config.call_data[call_id].ringback_on)
1848 return;
1849
1850 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1851
1852 if (++app_config.ringback_cnt==1 &&
1853 app_config.ringback_slot!=PJSUA_INVALID_ID)
1854 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001855 pjsua_conf_connect(app_config.ringback_slot, 0);
1856 }
1857}
1858
1859static void ring_stop(pjsua_call_id call_id)
1860{
1861 if (app_config.no_tones)
1862 return;
1863
1864 if (app_config.call_data[call_id].ringback_on) {
1865 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1866
1867 pj_assert(app_config.ringback_cnt>0);
1868 if (--app_config.ringback_cnt == 0 &&
1869 app_config.ringback_slot!=PJSUA_INVALID_ID)
1870 {
1871 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001872 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001873 }
1874 }
1875
1876 if (app_config.call_data[call_id].ring_on) {
1877 app_config.call_data[call_id].ring_on = PJ_FALSE;
1878
1879 pj_assert(app_config.ring_cnt>0);
1880 if (--app_config.ring_cnt == 0 &&
1881 app_config.ring_slot!=PJSUA_INVALID_ID)
1882 {
1883 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001884 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001885 }
1886 }
1887}
1888
1889static void ring_start(pjsua_call_id call_id)
1890{
1891 if (app_config.no_tones)
1892 return;
1893
1894 if (app_config.call_data[call_id].ring_on)
1895 return;
1896
1897 app_config.call_data[call_id].ring_on = PJ_TRUE;
1898
1899 if (++app_config.ring_cnt==1 &&
1900 app_config.ring_slot!=PJSUA_INVALID_ID)
1901 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001902 pjsua_conf_connect(app_config.ring_slot, 0);
1903 }
1904}
1905
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001906/*
1907 * Find next call when current call is disconnected or when user
1908 * press ']'
1909 */
1910static pj_bool_t find_next_call(void)
1911{
1912 int i, max;
1913
1914 max = pjsua_call_get_max_count();
1915 for (i=current_call+1; i<max; ++i) {
1916 if (pjsua_call_is_active(i)) {
1917 current_call = i;
1918 return PJ_TRUE;
1919 }
1920 }
1921
1922 for (i=0; i<current_call; ++i) {
1923 if (pjsua_call_is_active(i)) {
1924 current_call = i;
1925 return PJ_TRUE;
1926 }
1927 }
1928
1929 current_call = PJSUA_INVALID_ID;
1930 return PJ_FALSE;
1931}
1932
1933
1934/*
1935 * Find previous call when user press '['
1936 */
1937static pj_bool_t find_prev_call(void)
1938{
1939 int i, max;
1940
1941 max = pjsua_call_get_max_count();
1942 for (i=current_call-1; i>=0; --i) {
1943 if (pjsua_call_is_active(i)) {
1944 current_call = i;
1945 return PJ_TRUE;
1946 }
1947 }
1948
1949 for (i=max-1; i>current_call; --i) {
1950 if (pjsua_call_is_active(i)) {
1951 current_call = i;
1952 return PJ_TRUE;
1953 }
1954 }
1955
1956 current_call = PJSUA_INVALID_ID;
1957 return PJ_FALSE;
1958}
1959
1960
Benny Prijono804ff0a2006-09-14 11:17:48 +00001961/* Callback from timer when the maximum call duration has been
1962 * exceeded.
1963 */
1964static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1965 struct pj_timer_entry *entry)
1966{
1967 pjsua_call_id call_id = entry->id;
1968 pjsua_msg_data msg_data;
1969 pjsip_generic_string_hdr warn;
1970 pj_str_t hname = pj_str("Warning");
1971 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1972
1973 PJ_UNUSED_ARG(timer_heap);
1974
Benny Prijono148c9dd2006-09-19 13:37:53 +00001975 if (call_id == PJSUA_INVALID_ID) {
1976 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1977 return;
1978 }
1979
Benny Prijono804ff0a2006-09-14 11:17:48 +00001980 /* Add warning header */
1981 pjsua_msg_data_init(&msg_data);
1982 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1983 pj_list_push_back(&msg_data.hdr_list, &warn);
1984
1985 /* Call duration has been exceeded; disconnect the call */
1986 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1987 "for call %d, disconnecting the call",
1988 app_config.duration, call_id));
1989 entry->id = PJSUA_INVALID_ID;
1990 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1991}
1992
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001993
1994/*
1995 * Handler when invite state has changed.
1996 */
1997static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1998{
1999 pjsua_call_info call_info;
2000
2001 PJ_UNUSED_ARG(e);
2002
2003 pjsua_call_get_info(call_id, &call_info);
2004
2005 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2006
Benny Prijono91d20f42008-06-14 19:42:37 +00002007 /* Stop all ringback for this call */
2008 ring_stop(call_id);
2009
Benny Prijono804ff0a2006-09-14 11:17:48 +00002010 /* Cancel duration timer, if any */
2011 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2012 struct call_data *cd = &app_config.call_data[call_id];
2013 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2014
2015 cd->timer.id = PJSUA_INVALID_ID;
2016 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2017 }
2018
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002019 /* Rewind play file when hangup automatically,
2020 * since file is not looped
2021 */
2022 if (app_config.auto_play_hangup)
2023 pjsua_player_set_pos(app_config.wav_id, 0);
2024
2025
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002026 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2027 call_id,
2028 call_info.last_status,
2029 call_info.last_status_text.ptr));
2030
2031 if (call_id == current_call) {
2032 find_next_call();
2033 }
2034
Benny Prijono4be63b52006-11-25 14:50:25 +00002035 /* Dump media state upon disconnected */
2036 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002037 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002038 "Call %d disconnected, dumping media stats..",
2039 call_id));
2040 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002041 }
2042
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002043 } else {
2044
Benny Prijono804ff0a2006-09-14 11:17:48 +00002045 if (app_config.duration!=NO_LIMIT &&
2046 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2047 {
2048 /* Schedule timer to hangup call after the specified duration */
2049 struct call_data *cd = &app_config.call_data[call_id];
2050 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2051 pj_time_val delay;
2052
2053 cd->timer.id = call_id;
2054 delay.sec = app_config.duration;
2055 delay.msec = 0;
2056 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2057 }
2058
Benny Prijono4be63b52006-11-25 14:50:25 +00002059 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2060 int code;
2061 pj_str_t reason;
2062 pjsip_msg *msg;
2063
2064 /* This can only occur because of TX or RX message */
2065 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2066
2067 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2068 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2069 } else {
2070 msg = e->body.tsx_state.src.tdata->msg;
2071 }
2072
2073 code = msg->line.status.code;
2074 reason = msg->line.status.reason;
2075
Benny Prijono91d20f42008-06-14 19:42:37 +00002076 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2077 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2078 msg->body == NULL &&
2079 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2080 {
2081 ringback_start(call_id);
2082 }
2083
Benny Prijono4be63b52006-11-25 14:50:25 +00002084 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2085 call_id, call_info.state_text.ptr,
2086 code, (int)reason.slen, reason.ptr));
2087 } else {
2088 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2089 call_id,
2090 call_info.state_text.ptr));
2091 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002092
2093 if (current_call==PJSUA_INVALID_ID)
2094 current_call = call_id;
2095
2096 }
2097}
2098
2099
2100/**
2101 * Handler when there is incoming call.
2102 */
2103static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2104 pjsip_rx_data *rdata)
2105{
2106 pjsua_call_info call_info;
2107
2108 PJ_UNUSED_ARG(acc_id);
2109 PJ_UNUSED_ARG(rdata);
2110
2111 pjsua_call_get_info(call_id, &call_info);
2112
Benny Prijono91d20f42008-06-14 19:42:37 +00002113 /* Start ringback */
2114 ring_start(call_id);
2115
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002116 if (app_config.auto_answer > 0) {
2117 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2118 }
2119
2120 if (app_config.auto_answer < 200) {
2121 PJ_LOG(3,(THIS_FILE,
2122 "Incoming call for account %d!\n"
2123 "From: %s\n"
2124 "To: %s\n"
2125 "Press a to answer or h to reject call",
2126 acc_id,
2127 call_info.remote_info.ptr,
2128 call_info.local_info.ptr));
2129 }
2130}
2131
2132
2133/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002134 * Handler when a transaction within a call has changed state.
2135 */
2136static void on_call_tsx_state(pjsua_call_id call_id,
2137 pjsip_transaction *tsx,
2138 pjsip_event *e)
2139{
2140 const pjsip_method info_method =
2141 {
2142 PJSIP_OTHER_METHOD,
2143 { "INFO", 4 }
2144 };
2145
2146 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2147 /*
2148 * Handle INFO method.
2149 */
2150 if (tsx->role == PJSIP_ROLE_UAC &&
2151 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002152 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2153 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002154 {
2155 /* Status of outgoing INFO request */
2156 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2157 PJ_LOG(4,(THIS_FILE,
2158 "Call %d: DTMF sent successfully with INFO",
2159 call_id));
2160 } else if (tsx->status_code >= 300) {
2161 PJ_LOG(4,(THIS_FILE,
2162 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2163 call_id,
2164 tsx->status_code,
2165 (int)tsx->status_text.slen,
2166 tsx->status_text.ptr));
2167 }
2168 } else if (tsx->role == PJSIP_ROLE_UAS &&
2169 tsx->state == PJSIP_TSX_STATE_TRYING)
2170 {
2171 /* Answer incoming INFO with 200/OK */
2172 pjsip_rx_data *rdata;
2173 pjsip_tx_data *tdata;
2174 pj_status_t status;
2175
2176 rdata = e->body.tsx_state.src.rdata;
2177
2178 if (rdata->msg_info.msg->body) {
2179 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2180 200, NULL, &tdata);
2181 if (status == PJ_SUCCESS)
2182 status = pjsip_tsx_send_msg(tsx, tdata);
2183
2184 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2185 call_id,
2186 (int)rdata->msg_info.msg->body->len,
2187 rdata->msg_info.msg->body->data));
2188 } else {
2189 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2190 400, NULL, &tdata);
2191 if (status == PJ_SUCCESS)
2192 status = pjsip_tsx_send_msg(tsx, tdata);
2193 }
2194 }
2195 }
2196}
2197
2198
2199/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002200 * Callback on media state changed event.
2201 * The action may connect the call to sound device, to file, or
2202 * to loop the call.
2203 */
2204static void on_call_media_state(pjsua_call_id call_id)
2205{
2206 pjsua_call_info call_info;
2207
2208 pjsua_call_get_info(call_id, &call_info);
2209
Benny Prijono91d20f42008-06-14 19:42:37 +00002210 /* Stop ringback */
2211 ring_stop(call_id);
2212
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002213 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
2214 pj_bool_t connect_sound = PJ_TRUE;
2215
2216 /* Loopback sound, if desired */
2217 if (app_config.auto_loop) {
2218 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2219 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00002220
2221 /* Automatically record conversation, if desired */
2222 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2223 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2224 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002225 }
2226
2227 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002228 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2229 app_config.wav_port != PJSUA_INVALID_ID)
2230 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002231 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2232 connect_sound = PJ_FALSE;
2233 }
2234
Benny Prijono7ca96da2006-08-07 12:11:40 +00002235 /* Put call in conference with other calls, if desired */
2236 if (app_config.auto_conf) {
2237 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002238 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002239 unsigned i;
2240
2241 /* Get all calls, and establish media connection between
2242 * this call and other calls.
2243 */
2244 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002245
Benny Prijono7ca96da2006-08-07 12:11:40 +00002246 for (i=0; i<call_cnt; ++i) {
2247 if (call_ids[i] == call_id)
2248 continue;
2249
2250 if (!pjsua_call_has_media(call_ids[i]))
2251 continue;
2252
2253 pjsua_conf_connect(call_info.conf_slot,
2254 pjsua_call_get_conf_port(call_ids[i]));
2255 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2256 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002257
2258 /* Automatically record conversation, if desired */
2259 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2260 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2261 app_config.rec_port);
2262 }
2263
Benny Prijono7ca96da2006-08-07 12:11:40 +00002264 }
2265
2266 /* Also connect call to local sound device */
2267 connect_sound = PJ_TRUE;
2268 }
2269
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002270 /* Otherwise connect to sound device */
2271 if (connect_sound) {
2272 pjsua_conf_connect(call_info.conf_slot, 0);
2273 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002274
2275 /* Automatically record conversation, if desired */
2276 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2277 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2278 pjsua_conf_connect(0, app_config.rec_port);
2279 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002280 }
2281
2282 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
2283
2284 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
2285 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2286 call_id));
2287 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
2288 PJ_LOG(3,(THIS_FILE,
2289 "Media for call %d is suspended (hold) by remote",
2290 call_id));
Benny Prijono096c56c2007-09-15 08:30:16 +00002291 } else if (call_info.media_status == PJSUA_CALL_MEDIA_ERROR) {
2292 pj_str_t reason = pj_str("ICE negotiation failed");
2293
2294 PJ_LOG(1,(THIS_FILE,
2295 "Media has reported error, disconnecting call"));
2296
2297 pjsua_call_hangup(call_id, 500, &reason, NULL);
2298
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002299 } else {
2300 PJ_LOG(3,(THIS_FILE,
2301 "Media for call %d is inactive",
2302 call_id));
2303 }
2304}
2305
Benny Prijono0875ae82006-12-26 00:11:48 +00002306/*
2307 * DTMF callback.
2308 */
2309static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2310{
2311 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2312}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002313
2314/*
2315 * Handler registration status has changed.
2316 */
2317static void on_reg_state(pjsua_acc_id acc_id)
2318{
2319 PJ_UNUSED_ARG(acc_id);
2320
2321 // Log already written.
2322}
2323
2324
2325/*
2326 * Handler on buddy state changed.
2327 */
2328static void on_buddy_state(pjsua_buddy_id buddy_id)
2329{
2330 pjsua_buddy_info info;
2331 pjsua_buddy_get_info(buddy_id, &info);
2332
2333 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2334 (int)info.uri.slen,
2335 info.uri.ptr,
2336 (int)info.status_text.slen,
2337 info.status_text.ptr));
2338}
2339
2340
2341/**
2342 * Incoming IM message (i.e. MESSAGE request)!
2343 */
2344static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2345 const pj_str_t *to, const pj_str_t *contact,
2346 const pj_str_t *mime_type, const pj_str_t *text)
2347{
2348 /* Note: call index may be -1 */
2349 PJ_UNUSED_ARG(call_id);
2350 PJ_UNUSED_ARG(to);
2351 PJ_UNUSED_ARG(contact);
2352 PJ_UNUSED_ARG(mime_type);
2353
Benny Prijonof4b538d2007-05-14 16:45:20 +00002354 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002355 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002356 (int)text->slen, text->ptr,
2357 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002358}
2359
2360
2361/**
2362 * Received typing indication
2363 */
2364static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2365 const pj_str_t *to, const pj_str_t *contact,
2366 pj_bool_t is_typing)
2367{
2368 PJ_UNUSED_ARG(call_id);
2369 PJ_UNUSED_ARG(to);
2370 PJ_UNUSED_ARG(contact);
2371
2372 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2373 (int)from->slen, from->ptr,
2374 (is_typing?"is typing..":"has stopped typing")));
2375}
2376
2377
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002378/**
2379 * Call transfer request status.
2380 */
2381static void on_call_transfer_status(pjsua_call_id call_id,
2382 int status_code,
2383 const pj_str_t *status_text,
2384 pj_bool_t final,
2385 pj_bool_t *p_cont)
2386{
2387 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2388 call_id, status_code,
2389 (int)status_text->slen, status_text->ptr,
2390 (final ? "[final]" : "")));
2391
2392 if (status_code/100 == 2) {
2393 PJ_LOG(3,(THIS_FILE,
2394 "Call %d: call transfered successfully, disconnecting call",
2395 call_id));
2396 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2397 *p_cont = PJ_FALSE;
2398 }
2399}
2400
2401
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002402/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002403 * Notification that call is being replaced.
2404 */
2405static void on_call_replaced(pjsua_call_id old_call_id,
2406 pjsua_call_id new_call_id)
2407{
2408 pjsua_call_info old_ci, new_ci;
2409
2410 pjsua_call_get_info(old_call_id, &old_ci);
2411 pjsua_call_get_info(new_call_id, &new_ci);
2412
2413 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2414 "call %d with %.*s",
2415 old_call_id,
2416 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2417 new_call_id,
2418 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2419}
2420
2421
2422/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002423 * NAT type detection callback.
2424 */
2425static void on_nat_detect(const pj_stun_nat_detect_result *res)
2426{
2427 if (res->status != PJ_SUCCESS) {
2428 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2429 } else {
2430 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2431 }
2432}
2433
2434
2435/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002436 * Print buddy list.
2437 */
2438static void print_buddy_list(void)
2439{
2440 pjsua_buddy_id ids[64];
2441 int i;
2442 unsigned count = PJ_ARRAY_SIZE(ids);
2443
2444 puts("Buddy list:");
2445
2446 pjsua_enum_buddies(ids, &count);
2447
2448 if (count == 0)
2449 puts(" -none-");
2450 else {
2451 for (i=0; i<(int)count; ++i) {
2452 pjsua_buddy_info info;
2453
2454 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2455 continue;
2456
Benny Prijono4461c7d2007-08-25 13:36:15 +00002457 printf(" [%2d] <%.*s> %.*s\n",
2458 ids[i]+1,
2459 (int)info.status_text.slen,
2460 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002461 (int)info.uri.slen,
2462 info.uri.ptr);
2463 }
2464 }
2465 puts("");
2466}
2467
2468
2469/*
2470 * Print account status.
2471 */
2472static void print_acc_status(int acc_id)
2473{
2474 char buf[80];
2475 pjsua_acc_info info;
2476
2477 pjsua_acc_get_info(acc_id, &info);
2478
2479 if (!info.has_registration) {
2480 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2481 (int)info.status_text.slen,
2482 info.status_text.ptr);
2483
2484 } else {
2485 pj_ansi_snprintf(buf, sizeof(buf),
2486 "%d/%.*s (expires=%d)",
2487 info.status,
2488 (int)info.status_text.slen,
2489 info.status_text.ptr,
2490 info.expires);
2491
2492 }
2493
2494 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2495 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002496 printf(" Online status: %.*s\n",
2497 (int)info.online_status_text.slen,
2498 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002499}
2500
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002501/* Playfile done notification, set timer to hangup calls */
2502pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2503{
2504 pj_time_val delay;
2505
2506 PJ_UNUSED_ARG(port);
2507 PJ_UNUSED_ARG(usr_data);
2508
2509 /* Just rewind WAV when it is played outside of call */
2510 if (pjsua_call_get_count() == 0) {
2511 pjsua_player_set_pos(app_config.wav_id, 0);
2512 return PJ_SUCCESS;
2513 }
2514
2515 /* Timer is already active */
2516 if (app_config.auto_hangup_timer.id == 1)
2517 return PJ_SUCCESS;
2518
2519 app_config.auto_hangup_timer.id = 1;
2520 delay.sec = 0;
2521 delay.msec = 200; /* Give 200 ms before hangup */
2522 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2523 &app_config.auto_hangup_timer,
2524 &delay);
2525
2526 return PJ_SUCCESS;
2527}
2528
2529/* Auto hangup timer callback */
2530static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2531 struct pj_timer_entry *entry)
2532{
2533 PJ_UNUSED_ARG(timer_heap);
2534 PJ_UNUSED_ARG(entry);
2535
2536 app_config.auto_hangup_timer.id = 0;
2537 pjsua_call_hangup_all();
2538}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002539
2540/*
2541 * Show a bit of help.
2542 */
2543static void keystroke_help(void)
2544{
2545 pjsua_acc_id acc_ids[16];
2546 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2547 int i;
2548
2549 printf(">>>>\n");
2550
2551 pjsua_enum_accs(acc_ids, &count);
2552
2553 printf("Account list:\n");
2554 for (i=0; i<(int)count; ++i)
2555 print_acc_status(acc_ids[i]);
2556
2557 print_buddy_list();
2558
2559 //puts("Commands:");
2560 puts("+=============================================================================+");
2561 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2562 puts("| | | |");
2563 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2564 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002565 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2566 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2567 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2568 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002569 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2570 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002571 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002572 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002573 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2574 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2575 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2576 puts("| | V Adjust audio Volume | f Save config |");
2577 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002578 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002579 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002580 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002581
2582 i = pjsua_call_get_count();
2583 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002584
2585 if (current_call != PJSUA_INVALID_ID) {
2586 pjsua_call_info ci;
2587 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2588 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2589 (int)ci.remote_info.slen, ci.remote_info.ptr,
2590 (int)ci.state_text.slen, ci.state_text.ptr);
2591 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002592}
2593
2594
2595/*
2596 * Input simple string
2597 */
2598static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2599{
2600 char *p;
2601
2602 printf("%s (empty to cancel): ", title); fflush(stdout);
2603 fgets(buf, len, stdin);
2604
2605 /* Remove trailing newlines. */
2606 for (p=buf; ; ++p) {
2607 if (*p=='\r' || *p=='\n') *p='\0';
2608 else if (!*p) break;
2609 }
2610
2611 if (!*buf)
2612 return PJ_FALSE;
2613
2614 return PJ_TRUE;
2615}
2616
2617
2618#define NO_NB -2
2619struct input_result
2620{
2621 int nb_result;
2622 char *uri_result;
2623};
2624
2625
2626/*
2627 * Input URL.
2628 */
2629static void ui_input_url(const char *title, char *buf, int len,
2630 struct input_result *result)
2631{
2632 result->nb_result = NO_NB;
2633 result->uri_result = NULL;
2634
2635 print_buddy_list();
2636
2637 printf("Choices:\n"
2638 " 0 For current dialog.\n"
2639 " -1 All %d buddies in buddy list\n"
2640 " [1 -%2d] Select from buddy list\n"
2641 " URL An URL\n"
2642 " <Enter> Empty input (or 'q') to cancel\n"
2643 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2644 printf("%s: ", title);
2645
2646 fflush(stdout);
2647 fgets(buf, len, stdin);
2648 len = strlen(buf);
2649
2650 /* Left trim */
2651 while (pj_isspace(*buf)) {
2652 ++buf;
2653 --len;
2654 }
2655
2656 /* Remove trailing newlines */
2657 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2658 buf[--len] = '\0';
2659
2660 if (len == 0 || buf[0]=='q')
2661 return;
2662
2663 if (pj_isdigit(*buf) || *buf=='-') {
2664
2665 int i;
2666
2667 if (*buf=='-')
2668 i = 1;
2669 else
2670 i = 0;
2671
2672 for (; i<len; ++i) {
2673 if (!pj_isdigit(buf[i])) {
2674 puts("Invalid input");
2675 return;
2676 }
2677 }
2678
2679 result->nb_result = my_atoi(buf);
2680
2681 if (result->nb_result >= 0 &&
2682 result->nb_result <= (int)pjsua_get_buddy_count())
2683 {
2684 return;
2685 }
2686 if (result->nb_result == -1)
2687 return;
2688
2689 puts("Invalid input");
2690 result->nb_result = NO_NB;
2691 return;
2692
2693 } else {
2694 pj_status_t status;
2695
2696 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2697 pjsua_perror(THIS_FILE, "Invalid URL", status);
2698 return;
2699 }
2700
2701 result->uri_result = buf;
2702 }
2703}
2704
2705/*
2706 * List the ports in conference bridge
2707 */
2708static void conf_list(void)
2709{
2710 unsigned i, count;
2711 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2712
2713 printf("Conference ports:\n");
2714
2715 count = PJ_ARRAY_SIZE(id);
2716 pjsua_enum_conf_ports(id, &count);
2717
2718 for (i=0; i<count; ++i) {
2719 char txlist[PJSUA_MAX_CALLS*4+10];
2720 unsigned j;
2721 pjsua_conf_port_info info;
2722
2723 pjsua_conf_get_port_info(id[i], &info);
2724
2725 txlist[0] = '\0';
2726 for (j=0; j<info.listener_cnt; ++j) {
2727 char s[10];
2728 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2729 pj_ansi_strcat(txlist, s);
2730 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002731 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002732 info.slot_id,
2733 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002734 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002735 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002736 (int)info.name.slen,
2737 info.name.ptr,
2738 txlist);
2739
2740 }
2741 puts("");
2742}
2743
2744
2745/*
Benny Prijono56315612006-07-18 14:39:40 +00002746 * Send arbitrary request to remote host
2747 */
2748static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2749{
2750 pj_str_t str_method;
2751 pjsip_method method;
2752 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002753 pjsip_endpoint *endpt;
2754 pj_status_t status;
2755
2756 endpt = pjsua_get_pjsip_endpt();
2757
2758 str_method = pj_str(cstr_method);
2759 pjsip_method_init_np(&method, &str_method);
2760
Benny Prijonofff245c2007-04-02 11:44:47 +00002761 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002762
Benny Prijonob988d762007-12-05 04:30:04 +00002763 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002764 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002765 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002766 return;
2767 }
2768}
2769
2770
2771/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002772 * Change extended online status.
2773 */
2774static void change_online_status(void)
2775{
2776 char menuin[32];
2777 pj_bool_t online_status;
2778 pjrpid_element elem;
2779 int i, choice;
2780
2781 enum {
2782 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2783 };
2784
2785 struct opt {
2786 int id;
2787 char *name;
2788 } opts[] = {
2789 { AVAILABLE, "Available" },
2790 { BUSY, "Busy"},
2791 { OTP, "On the phone"},
2792 { IDLE, "Idle"},
2793 { AWAY, "Away"},
2794 { BRB, "Be right back"},
2795 { OFFLINE, "Offline"}
2796 };
2797
2798 printf("\n"
2799 "Choices:\n");
2800 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2801 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2802 }
2803
2804 if (!simple_input("Select status", menuin, sizeof(menuin)))
2805 return;
2806
2807 choice = atoi(menuin) - 1;
2808 if (choice < 0 || choice >= OPT_MAX) {
2809 puts("Invalid selection");
2810 return;
2811 }
2812
2813 pj_bzero(&elem, sizeof(elem));
2814 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2815
2816 online_status = PJ_TRUE;
2817
2818 switch (choice) {
2819 case AVAILABLE:
2820 break;
2821 case BUSY:
2822 elem.activity = PJRPID_ACTIVITY_BUSY;
2823 elem.note = pj_str("Busy");
2824 break;
2825 case OTP:
2826 elem.activity = PJRPID_ACTIVITY_BUSY;
2827 elem.note = pj_str("On the phone");
2828 break;
2829 case IDLE:
2830 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2831 elem.note = pj_str("Idle");
2832 break;
2833 case AWAY:
2834 elem.activity = PJRPID_ACTIVITY_AWAY;
2835 elem.note = pj_str("Away");
2836 break;
2837 case BRB:
2838 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2839 elem.note = pj_str("Be right back");
2840 break;
2841 case OFFLINE:
2842 online_status = PJ_FALSE;
2843 break;
2844 }
2845
2846 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2847}
2848
2849
2850/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002851 * Change codec priorities.
2852 */
2853static void manage_codec_prio(void)
2854{
2855 pjsua_codec_info c[32];
2856 unsigned i, count = PJ_ARRAY_SIZE(c);
2857 char input[32];
2858 char *codec, *prio;
2859 pj_str_t id;
2860 int new_prio;
2861 pj_status_t status;
2862
2863 printf("List of codecs:\n");
2864
2865 pjsua_enum_codecs(c, &count);
2866 for (i=0; i<count; ++i) {
2867 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2868 c[i].codec_id.ptr);
2869 }
2870
2871 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00002872 puts("Enter codec id and its new priority "
2873 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002874
Benny Prijono88accae2008-06-26 15:48:14 +00002875 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002876 fgets(input, sizeof(input), stdin);
2877 if (input[0]=='\r' || input[0]=='\n') {
2878 puts("Done");
2879 return;
2880 }
2881
2882 codec = strtok(input, " \t\r\n");
2883 prio = strtok(NULL, " \r\n");
2884
2885 if (!codec || !prio) {
2886 puts("Invalid input");
2887 return;
2888 }
2889
2890 new_prio = atoi(prio);
2891 if (new_prio < 0)
2892 new_prio = 0;
2893 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2894 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2895
2896 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2897 (pj_uint8_t)new_prio);
2898 if (status != PJ_SUCCESS)
2899 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2900}
2901
2902
2903/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002904 * Main "user interface" loop.
2905 */
2906void console_app_main(const pj_str_t *uri_to_call)
2907{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002908 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002909 char buf[128];
2910 char text[128];
2911 int i, count;
2912 char *uri;
2913 pj_str_t tmp;
2914 struct input_result result;
2915 pjsua_call_info call_info;
2916 pjsua_acc_info acc_info;
2917
2918
2919 /* If user specifies URI to call, then call the URI */
2920 if (uri_to_call->slen) {
2921 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2922 }
2923
2924 keystroke_help();
2925
2926 for (;;) {
2927
2928 printf(">>> ");
2929 fflush(stdout);
2930
Benny Prijono990042e2007-01-21 19:36:00 +00002931 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2932 /*
2933 * Be friendly to users who redirect commands into
2934 * program, when file ends, resume with kbd.
2935 * If exit is desired end script with q for quit
2936 */
2937 /* Reopen stdin/stdout/stderr to /dev/console */
2938#if defined(PJ_WIN32) && PJ_WIN32!=0
2939 if (freopen ("CONIN$", "r", stdin) == NULL) {
2940#else
2941 if (1) {
2942#endif
2943 puts("Cannot switch back to console from file redirection");
2944 menuin[0] = 'q';
2945 menuin[1] = '\0';
2946 } else {
2947 puts("Switched back to console from file redirection");
2948 continue;
2949 }
2950 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002951
Benny Prijonoebc32c32008-06-12 13:30:39 +00002952 if (cmd_echo) {
2953 printf("%s", menuin);
2954 }
2955
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002956 switch (menuin[0]) {
2957
2958 case 'm':
2959 /* Make call! : */
2960 printf("(You currently have %d calls)\n",
2961 pjsua_call_get_count());
2962
2963 uri = NULL;
2964 ui_input_url("Make call", buf, sizeof(buf), &result);
2965 if (result.nb_result != NO_NB) {
2966
2967 if (result.nb_result == -1 || result.nb_result == 0) {
2968 puts("You can't do that with make call!");
2969 continue;
2970 } else {
2971 pjsua_buddy_info binfo;
2972 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2973 uri = binfo.uri.ptr;
2974 }
2975
2976 } else if (result.uri_result) {
2977 uri = result.uri_result;
2978 }
2979
2980 tmp = pj_str(uri);
2981 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2982 break;
2983
2984 case 'M':
2985 /* Make multiple calls! : */
2986 printf("(You currently have %d calls)\n",
2987 pjsua_call_get_count());
2988
2989 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2990 continue;
2991
2992 count = my_atoi(menuin);
2993 if (count < 1)
2994 continue;
2995
2996 ui_input_url("Make call", buf, sizeof(buf), &result);
2997 if (result.nb_result != NO_NB) {
2998 pjsua_buddy_info binfo;
2999 if (result.nb_result == -1 || result.nb_result == 0) {
3000 puts("You can't do that with make call!");
3001 continue;
3002 }
3003 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3004 uri = binfo.uri.ptr;
3005 } else {
3006 uri = result.uri_result;
3007 }
3008
3009 for (i=0; i<my_atoi(menuin); ++i) {
3010 pj_status_t status;
3011
3012 tmp = pj_str(uri);
3013 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3014 NULL, NULL);
3015 if (status != PJ_SUCCESS)
3016 break;
3017 }
3018 break;
3019
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003020 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003021 i = pjsua_detect_nat_type();
3022 if (i != PJ_SUCCESS)
3023 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003024 break;
3025
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003026 case 'i':
3027 /* Send instant messaeg */
3028
3029 /* i is for call index to send message, if any */
3030 i = -1;
3031
3032 /* Make compiler happy. */
3033 uri = NULL;
3034
3035 /* Input destination. */
3036 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3037 if (result.nb_result != NO_NB) {
3038
3039 if (result.nb_result == -1) {
3040 puts("You can't send broadcast IM like that!");
3041 continue;
3042
3043 } else if (result.nb_result == 0) {
3044
3045 i = current_call;
3046
3047 } else {
3048 pjsua_buddy_info binfo;
3049 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3050 uri = binfo.uri.ptr;
3051 }
3052
3053 } else if (result.uri_result) {
3054 uri = result.uri_result;
3055 }
3056
3057
3058 /* Send typing indication. */
3059 if (i != -1)
3060 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3061 else {
3062 pj_str_t tmp_uri = pj_str(uri);
3063 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3064 }
3065
3066 /* Input the IM . */
3067 if (!simple_input("Message", text, sizeof(text))) {
3068 /*
3069 * Cancelled.
3070 * Send typing notification too, saying we're not typing.
3071 */
3072 if (i != -1)
3073 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3074 else {
3075 pj_str_t tmp_uri = pj_str(uri);
3076 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3077 }
3078 continue;
3079 }
3080
3081 tmp = pj_str(text);
3082
3083 /* Send the IM */
3084 if (i != -1)
3085 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3086 else {
3087 pj_str_t tmp_uri = pj_str(uri);
3088 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3089 }
3090
3091 break;
3092
3093 case 'a':
3094
3095 if (current_call != -1) {
3096 pjsua_call_get_info(current_call, &call_info);
3097 } else {
3098 /* Make compiler happy */
3099 call_info.role = PJSIP_ROLE_UAC;
3100 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3101 }
3102
3103 if (current_call == -1 ||
3104 call_info.role != PJSIP_ROLE_UAS ||
3105 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3106 {
3107 puts("No pending incoming call");
3108 fflush(stdout);
3109 continue;
3110
3111 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003112 int st_code;
3113 char contact[120];
3114 pj_str_t hname = { "Contact", 7 };
3115 pj_str_t hvalue;
3116 pjsip_generic_string_hdr hcontact;
3117 pjsua_msg_data msg_data;
3118
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003119 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3120 continue;
3121
Benny Prijono20d36722007-02-22 14:52:24 +00003122 st_code = my_atoi(buf);
3123 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003124 continue;
3125
Benny Prijono20d36722007-02-22 14:52:24 +00003126 pjsua_msg_data_init(&msg_data);
3127
3128 if (st_code/100 == 3) {
3129 if (!simple_input("Enter URL to be put in Contact",
3130 contact, sizeof(contact)))
3131 continue;
3132 hvalue = pj_str(contact);
3133 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3134
3135 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3136 }
3137
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003138 /*
3139 * Must check again!
3140 * Call may have been disconnected while we're waiting for
3141 * keyboard input.
3142 */
3143 if (current_call == -1) {
3144 puts("Call has been disconnected");
3145 fflush(stdout);
3146 continue;
3147 }
3148
Benny Prijono20d36722007-02-22 14:52:24 +00003149 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003150 }
3151
3152 break;
3153
3154
3155 case 'h':
3156
3157 if (current_call == -1) {
3158 puts("No current call");
3159 fflush(stdout);
3160 continue;
3161
3162 } else if (menuin[1] == 'a') {
3163
3164 /* Hangup all calls */
3165 pjsua_call_hangup_all();
3166
3167 } else {
3168
3169 /* Hangup current calls */
3170 pjsua_call_hangup(current_call, 0, NULL, NULL);
3171 }
3172 break;
3173
3174 case ']':
3175 case '[':
3176 /*
3177 * Cycle next/prev dialog.
3178 */
3179 if (menuin[0] == ']') {
3180 find_next_call();
3181
3182 } else {
3183 find_prev_call();
3184 }
3185
3186 if (current_call != -1) {
3187
3188 pjsua_call_get_info(current_call, &call_info);
3189 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3190 (int)call_info.remote_info.slen,
3191 call_info.remote_info.ptr));
3192
3193 } else {
3194 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3195 }
3196 break;
3197
3198
3199 case '>':
3200 case '<':
3201 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3202 break;
3203
3204 i = my_atoi(buf);
3205 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003206 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003207 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3208 } else {
3209 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3210 }
3211 break;
3212
3213
3214 case '+':
3215 if (menuin[1] == 'b') {
3216
3217 pjsua_buddy_config buddy_cfg;
3218 pjsua_buddy_id buddy_id;
3219 pj_status_t status;
3220
3221 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3222 break;
3223
3224 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3225 printf("Invalid SIP URI '%s'\n", buf);
3226 break;
3227 }
3228
Benny Prijonoac623b32006-07-03 15:19:31 +00003229 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003230
3231 buddy_cfg.uri = pj_str(buf);
3232 buddy_cfg.subscribe = PJ_TRUE;
3233
3234 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3235 if (status == PJ_SUCCESS) {
3236 printf("New buddy '%s' added at index %d\n",
3237 buf, buddy_id+1);
3238 }
3239
3240 } else if (menuin[1] == 'a') {
3241
Benny Prijonofb2b3652007-06-28 07:15:03 +00003242 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3243 pjsua_acc_config acc_cfg;
3244 pj_status_t status;
3245
3246 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3247 break;
3248 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3249 break;
3250 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3251 break;
3252 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3253 break;
3254 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3255 break;
3256
3257 pjsua_acc_config_default(&acc_cfg);
3258 acc_cfg.id = pj_str(id);
3259 acc_cfg.reg_uri = pj_str(registrar);
3260 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003261 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003262 acc_cfg.cred_info[0].realm = pj_str(realm);
3263 acc_cfg.cred_info[0].username = pj_str(uname);
3264 acc_cfg.cred_info[0].data_type = 0;
3265 acc_cfg.cred_info[0].data = pj_str(passwd);
3266
3267 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3268 if (status != PJ_SUCCESS) {
3269 pjsua_perror(THIS_FILE, "Error adding new account", status);
3270 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003271
3272 } else {
3273 printf("Invalid input %s\n", menuin);
3274 }
3275 break;
3276
3277 case '-':
3278 if (menuin[1] == 'b') {
3279 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3280 break;
3281
3282 i = my_atoi(buf) - 1;
3283
3284 if (!pjsua_buddy_is_valid(i)) {
3285 printf("Invalid buddy id %d\n", i);
3286 } else {
3287 pjsua_buddy_del(i);
3288 printf("Buddy %d deleted\n", i);
3289 }
3290
3291 } else if (menuin[1] == 'a') {
3292
3293 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3294 break;
3295
3296 i = my_atoi(buf);
3297
3298 if (!pjsua_acc_is_valid(i)) {
3299 printf("Invalid account id %d\n", i);
3300 } else {
3301 pjsua_acc_del(i);
3302 printf("Account %d deleted\n", i);
3303 }
3304
3305 } else {
3306 printf("Invalid input %s\n", menuin);
3307 }
3308 break;
3309
3310 case 'H':
3311 /*
3312 * Hold call.
3313 */
3314 if (current_call != -1) {
3315
3316 pjsua_call_set_hold(current_call, NULL);
3317
3318 } else {
3319 PJ_LOG(3,(THIS_FILE, "No current call"));
3320 }
3321 break;
3322
3323 case 'v':
3324 /*
3325 * Send re-INVITE (to release hold, etc).
3326 */
3327 if (current_call != -1) {
3328
3329 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3330
3331 } else {
3332 PJ_LOG(3,(THIS_FILE, "No current call"));
3333 }
3334 break;
3335
Benny Prijonoc08682e2007-10-04 06:17:58 +00003336 case 'U':
3337 /*
3338 * Send UPDATE
3339 */
3340 if (current_call != -1) {
3341
3342 pjsua_call_update(current_call, 0, NULL);
3343
3344 } else {
3345 PJ_LOG(3,(THIS_FILE, "No current call"));
3346 }
3347 break;
3348
3349 case 'C':
3350 if (menuin[1] == 'p') {
3351 manage_codec_prio();
3352 }
3353 break;
3354
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003355 case 'x':
3356 /*
3357 * Transfer call.
3358 */
3359 if (current_call == -1) {
3360
3361 PJ_LOG(3,(THIS_FILE, "No current call"));
3362
3363 } else {
3364 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003365 pjsua_msg_data msg_data;
3366 pjsip_generic_string_hdr refer_sub;
3367 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3368 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003369 pjsua_call_info ci;
3370
3371 pjsua_call_get_info(current_call, &ci);
3372 printf("Transfering current call [%d] %.*s\n",
3373 current_call,
3374 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003375
3376 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3377
3378 /* Check if call is still there. */
3379
3380 if (call != current_call) {
3381 puts("Call has been disconnected");
3382 continue;
3383 }
3384
Benny Prijonod524e822006-09-22 12:48:18 +00003385 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003386 if (app_config.no_refersub) {
3387 /* Add Refer-Sub: false in outgoing REFER request */
3388 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3389 &STR_FALSE);
3390 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3391 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003392 if (result.nb_result != NO_NB) {
3393 if (result.nb_result == -1 || result.nb_result == 0)
3394 puts("You can't do that with transfer call!");
3395 else {
3396 pjsua_buddy_info binfo;
3397 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003398 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003399 }
3400
3401 } else if (result.uri_result) {
3402 pj_str_t tmp;
3403 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003404 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003405 }
3406 }
3407 break;
3408
Benny Prijonof7b1c392006-11-11 16:46:34 +00003409 case 'X':
3410 /*
3411 * Transfer call with replaces.
3412 */
3413 if (current_call == -1) {
3414
3415 PJ_LOG(3,(THIS_FILE, "No current call"));
3416
3417 } else {
3418 int call = current_call;
3419 int dst_call;
3420 pjsua_msg_data msg_data;
3421 pjsip_generic_string_hdr refer_sub;
3422 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3423 pj_str_t STR_FALSE = { "false", 5 };
3424 pjsua_call_id ids[PJSUA_MAX_CALLS];
3425 pjsua_call_info ci;
3426 unsigned i, count;
3427
3428 count = PJ_ARRAY_SIZE(ids);
3429 pjsua_enum_calls(ids, &count);
3430
3431 if (count <= 1) {
3432 puts("There are no other calls");
3433 continue;
3434 }
3435
3436 pjsua_call_get_info(current_call, &ci);
3437 printf("Transfer call [%d] %.*s to one of the following:\n",
3438 current_call,
3439 (int)ci.remote_info.slen, ci.remote_info.ptr);
3440
3441 for (i=0; i<count; ++i) {
3442 pjsua_call_info call_info;
3443
3444 if (ids[i] == call)
3445 continue;
3446
3447 pjsua_call_get_info(ids[i], &call_info);
3448 printf("%d %.*s [%.*s]\n",
3449 ids[i],
3450 (int)call_info.remote_info.slen,
3451 call_info.remote_info.ptr,
3452 (int)call_info.state_text.slen,
3453 call_info.state_text.ptr);
3454 }
3455
3456 if (!simple_input("Enter call number to be replaced",
3457 buf, sizeof(buf)))
3458 continue;
3459
3460 dst_call = my_atoi(buf);
3461
3462 /* Check if call is still there. */
3463
3464 if (call != current_call) {
3465 puts("Call has been disconnected");
3466 continue;
3467 }
3468
3469 /* Check that destination call is valid. */
3470 if (dst_call == call) {
3471 puts("Destination call number must not be the same "
3472 "as the call being transfered");
3473 continue;
3474 }
3475 if (dst_call >= PJSUA_MAX_CALLS) {
3476 puts("Invalid destination call number");
3477 continue;
3478 }
3479 if (!pjsua_call_is_active(dst_call)) {
3480 puts("Invalid destination call number");
3481 continue;
3482 }
3483
3484 pjsua_msg_data_init(&msg_data);
3485 if (app_config.no_refersub) {
3486 /* Add Refer-Sub: false in outgoing REFER request */
3487 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3488 &STR_FALSE);
3489 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3490 }
3491
3492 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3493 }
3494 break;
3495
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003496 case '#':
3497 /*
3498 * Send DTMF strings.
3499 */
3500 if (current_call == -1) {
3501
3502 PJ_LOG(3,(THIS_FILE, "No current call"));
3503
3504 } else if (!pjsua_call_has_media(current_call)) {
3505
3506 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3507
3508 } else {
3509 pj_str_t digits;
3510 int call = current_call;
3511 pj_status_t status;
3512
3513 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3514 sizeof(buf)))
3515 {
3516 break;
3517 }
3518
3519 if (call != current_call) {
3520 puts("Call has been disconnected");
3521 continue;
3522 }
3523
3524 digits = pj_str(buf);
3525 status = pjsua_call_dial_dtmf(current_call, &digits);
3526 if (status != PJ_SUCCESS) {
3527 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3528 } else {
3529 puts("DTMF digits enqueued for transmission");
3530 }
3531 }
3532 break;
3533
Benny Prijonofeb69f42007-10-05 09:12:26 +00003534 case '*':
3535 /* Send DTMF with INFO */
3536 if (current_call == -1) {
3537
3538 PJ_LOG(3,(THIS_FILE, "No current call"));
3539
3540 } else {
3541 const pj_str_t SIP_INFO = pj_str("INFO");
3542 pj_str_t digits;
3543 int call = current_call;
3544 int i;
3545 pj_status_t status;
3546
3547 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3548 sizeof(buf)))
3549 {
3550 break;
3551 }
3552
3553 if (call != current_call) {
3554 puts("Call has been disconnected");
3555 continue;
3556 }
3557
3558 digits = pj_str(buf);
3559 for (i=0; i<digits.slen; ++i) {
3560 pjsua_msg_data msg_data;
3561 char body[80];
3562
3563 pjsua_msg_data_init(&msg_data);
3564 msg_data.content_type = pj_str("application/dtmf-relay");
3565
3566 pj_ansi_snprintf(body, sizeof(body),
3567 "Signal=%c\r\n"
3568 "Duration=160",
3569 buf[i]);
3570 msg_data.msg_body = pj_str(body);
3571
3572 status = pjsua_call_send_request(current_call, &SIP_INFO,
3573 &msg_data);
3574 if (status != PJ_SUCCESS) {
3575 break;
3576 }
3577 }
3578 }
3579 break;
3580
Benny Prijono56315612006-07-18 14:39:40 +00003581 case 'S':
3582 /*
3583 * Send arbitrary request
3584 */
3585 if (pjsua_acc_get_count() == 0) {
3586 puts("Sorry, need at least one account configured");
3587 break;
3588 }
3589
3590 puts("Send arbitrary request to remote host");
3591
3592 /* Input METHOD */
3593 if (!simple_input("Request method:",text,sizeof(text)))
3594 break;
3595
3596 /* Input destination URI */
3597 uri = NULL;
3598 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3599 if (result.nb_result != NO_NB) {
3600
3601 if (result.nb_result == -1 || result.nb_result == 0) {
3602 puts("Sorry you can't do that!");
3603 continue;
3604 } else {
3605 pjsua_buddy_info binfo;
3606 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3607 uri = binfo.uri.ptr;
3608 }
3609
3610 } else if (result.uri_result) {
3611 uri = result.uri_result;
3612 }
3613
3614 tmp = pj_str(uri);
3615
3616 send_request(text, &tmp);
3617 break;
3618
Benny Prijonoebc32c32008-06-12 13:30:39 +00003619 case 'e':
3620 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3621 pj_str_t tmp;
3622
3623 tmp.ptr = menuin+5;
3624 tmp.slen = pj_ansi_strlen(menuin)-6;
3625
3626 if (tmp.slen < 1) {
3627 puts("Usage: echo [0|1]");
3628 break;
3629 }
3630
Benny Prijonoddd02de2008-06-26 22:20:11 +00003631 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003632 }
3633 break;
3634
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003635 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003636 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3637 pj_str_t tmp;
3638 int delay;
3639
3640 tmp.ptr = menuin+6;
3641 tmp.slen = pj_ansi_strlen(menuin)-7;
3642
3643 if (tmp.slen < 1) {
3644 puts("Usage: sleep MSEC");
3645 break;
3646 }
3647
3648 delay = pj_strtoul(&tmp);
3649 if (delay < 0) delay = 0;
3650 pj_thread_sleep(delay);
3651 break;
3652 }
3653 /* Continue below */
3654
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003655 case 'u':
3656 /*
3657 * Subscribe/unsubscribe presence.
3658 */
3659 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3660 if (result.nb_result != NO_NB) {
3661 if (result.nb_result == -1) {
3662 int i, count;
3663 count = pjsua_get_buddy_count();
3664 for (i=0; i<count; ++i)
3665 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3666 } else if (result.nb_result == 0) {
3667 puts("Sorry, can only subscribe to buddy's presence, "
3668 "not from existing call");
3669 } else {
3670 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3671 }
3672
3673 } else if (result.uri_result) {
3674 puts("Sorry, can only subscribe to buddy's presence, "
3675 "not arbitrary URL (for now)");
3676 }
3677
3678 break;
3679
3680 case 'r':
3681 switch (menuin[1]) {
3682 case 'r':
3683 /*
3684 * Re-Register.
3685 */
3686 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3687 break;
3688 case 'u':
3689 /*
3690 * Unregister
3691 */
3692 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3693 break;
3694 }
3695 break;
3696
3697 case 't':
3698 pjsua_acc_get_info(current_acc, &acc_info);
3699 acc_info.online_status = !acc_info.online_status;
3700 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3701 printf("Setting %s online status to %s\n",
3702 acc_info.acc_uri.ptr,
3703 (acc_info.online_status?"online":"offline"));
3704 break;
3705
Benny Prijono4461c7d2007-08-25 13:36:15 +00003706 case 'T':
3707 change_online_status();
3708 break;
3709
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003710 case 'c':
3711 switch (menuin[1]) {
3712 case 'l':
3713 conf_list();
3714 break;
3715 case 'c':
3716 case 'd':
3717 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003718 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003719 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003720 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003721 const char *src_title, *dst_title;
3722
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003723 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003724
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003725 if (cnt != 3) {
3726 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003727
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003728 src_title = (menuin[1]=='c'?
3729 "Connect src port #":
3730 "Disconnect src port #");
3731 dst_title = (menuin[1]=='c'?
3732 "To dst port #":
3733 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003734
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003735 if (!simple_input(src_title, src_port, sizeof(src_port)))
3736 break;
3737
3738 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3739 break;
3740 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003741
3742 if (menuin[1]=='c') {
3743 status = pjsua_conf_connect(my_atoi(src_port),
3744 my_atoi(dst_port));
3745 } else {
3746 status = pjsua_conf_disconnect(my_atoi(src_port),
3747 my_atoi(dst_port));
3748 }
3749 if (status == PJ_SUCCESS) {
3750 puts("Success");
3751 } else {
3752 puts("ERROR!!");
3753 }
3754 }
3755 break;
3756 }
3757 break;
3758
Benny Prijono6dd967c2006-12-26 02:27:14 +00003759 case 'V':
3760 /* Adjust audio volume */
3761 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3762 if (simple_input(buf,text,sizeof(text))) {
3763 char *err;
3764 app_config.mic_level = (float)strtod(text, &err);
3765 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3766 }
3767 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3768 app_config.speaker_level);
3769 if (simple_input(buf,text,sizeof(text))) {
3770 char *err;
3771 app_config.speaker_level = (float)strtod(text, &err);
3772 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3773 }
3774
3775 break;
3776
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003777 case 'd':
3778 if (menuin[1] == 'c') {
3779 char settings[2000];
3780 int len;
3781
3782 len = write_settings(&app_config, settings, sizeof(settings));
3783 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003784 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003785 else
3786 PJ_LOG(3,(THIS_FILE,
3787 "Dumping configuration (%d bytes):\n%s\n",
3788 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003789
3790 } else if (menuin[1] == 'q') {
3791
3792 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003793 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003794 } else {
3795 PJ_LOG(3,(THIS_FILE, "No current call"));
3796 }
3797
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003798 } else {
3799 app_dump(menuin[1]=='d');
3800 }
3801 break;
3802
3803
3804 case 'f':
3805 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3806 char settings[2000];
3807 int len;
3808
3809 len = write_settings(&app_config, settings, sizeof(settings));
3810 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003811 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003812 else {
3813 pj_oshandle_t fd;
3814 pj_status_t status;
3815
3816 status = pj_file_open(app_config.pool, buf,
3817 PJ_O_WRONLY, &fd);
3818 if (status != PJ_SUCCESS) {
3819 pjsua_perror(THIS_FILE, "Unable to open file", status);
3820 } else {
3821 pj_ssize_t size = len;
3822 pj_file_write(fd, settings, &size);
3823 pj_file_close(fd);
3824
3825 printf("Settings successfully written to '%s'\n", buf);
3826 }
3827 }
3828
3829 }
3830 break;
3831
3832
3833 case 'q':
3834 goto on_exit;
3835
3836
3837 default:
3838 if (menuin[0] != '\n' && menuin[0] != '\r') {
3839 printf("Invalid input %s", menuin);
3840 }
3841 keystroke_help();
3842 break;
3843 }
3844 }
3845
3846on_exit:
3847 ;
3848}
3849
3850
3851/*****************************************************************************
3852 * Public API
3853 */
3854
3855pj_status_t app_init(int argc, char *argv[])
3856{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003857 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003858 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003859 unsigned i;
3860 pj_status_t status;
3861
3862 /* Create pjsua */
3863 status = pjsua_create();
3864 if (status != PJ_SUCCESS)
3865 return status;
3866
3867 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00003868 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003869
3870 /* Initialize default config */
3871 default_config(&app_config);
3872
3873 /* Parse the arguments */
3874 status = parse_args(argc, argv, &app_config, &uri_arg);
3875 if (status != PJ_SUCCESS)
3876 return status;
3877
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003878 /* Initialize application callbacks */
3879 app_config.cfg.cb.on_call_state = &on_call_state;
3880 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3881 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003882 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003883 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003884 app_config.cfg.cb.on_reg_state = &on_reg_state;
3885 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3886 app_config.cfg.cb.on_pager = &on_pager;
3887 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003888 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003889 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003890 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003891
3892 /* Initialize pjsua */
3893 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3894 &app_config.media_cfg);
3895 if (status != PJ_SUCCESS)
3896 return status;
3897
Benny Prijonoe909eac2006-07-27 22:04:56 +00003898#ifdef STEREO_DEMO
3899 stereo_demo();
3900#endif
3901
Benny Prijono804ff0a2006-09-14 11:17:48 +00003902 /* Initialize calls data */
3903 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3904 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3905 app_config.call_data[i].timer.cb = &call_timeout_callback;
3906 }
3907
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003908 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003909 for (i=0; i<app_config.wav_count; ++i) {
3910 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003911 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00003912
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003913 if (app_config.auto_play_hangup)
3914 play_options |= PJMEDIA_FILE_NO_LOOP;
3915
3916 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00003917 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003918 if (status != PJ_SUCCESS)
3919 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003920
Benny Prijono72c04d22007-02-17 22:20:58 +00003921 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003922 app_config.wav_id = wav_id;
3923 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003924 if (app_config.auto_play_hangup) {
3925 pjmedia_port *port;
3926
3927 pjsua_player_get_port(app_config.wav_id, &port);
3928 status = pjmedia_wav_player_set_eof_cb(port, NULL,
3929 &on_playfile_done);
3930 if (status != PJ_SUCCESS)
3931 goto on_error;
3932
3933 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
3934 &hangup_timeout_callback);
3935 }
Benny Prijono32e4f492007-01-24 00:44:26 +00003936 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003937 }
3938
Benny Prijono4af234b2007-01-24 02:02:09 +00003939 /* Optionally registers tone players */
3940 for (i=0; i<app_config.tone_count; ++i) {
3941 pjmedia_port *tport;
3942 char name[80];
3943 pj_str_t label;
3944 pj_status_t status;
3945
3946 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3947 app_config.tones[i].freq1,
3948 app_config.tones[i].freq2);
3949 label = pj_str(name);
3950 status = pjmedia_tonegen_create2(app_config.pool, &label,
3951 8000, 1, 160, 16,
3952 PJMEDIA_TONEGEN_LOOP, &tport);
3953 if (status != PJ_SUCCESS) {
3954 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3955 goto on_error;
3956 }
3957
3958 status = pjsua_conf_add_port(app_config.pool, tport,
3959 &app_config.tone_slots[i]);
3960 pj_assert(status == PJ_SUCCESS);
3961
3962 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3963 pj_assert(status == PJ_SUCCESS);
3964 }
3965
Benny Prijono1ebd6142006-10-19 15:48:02 +00003966 /* Optionally create recorder file, if any. */
3967 if (app_config.rec_file.slen) {
3968 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3969 &app_config.rec_id);
3970 if (status != PJ_SUCCESS)
3971 goto on_error;
3972
3973 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3974 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003975
Benny Prijonoe347cb02007-02-14 14:36:13 +00003976 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3977
Benny Prijono91d20f42008-06-14 19:42:37 +00003978 /* Create ringback tones */
3979 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00003980 unsigned i, samples_per_frame;
3981 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00003982 pj_str_t name;
3983
Benny Prijono4f966892008-06-14 22:43:56 +00003984 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
3985 app_config.media_cfg.clock_rate *
3986 app_config.media_cfg.channel_count / 1000;
3987
Benny Prijono91d20f42008-06-14 19:42:37 +00003988 /* Ringback tone (call is ringing) */
3989 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00003990 status = pjmedia_tonegen_create2(app_config.pool, &name,
3991 app_config.media_cfg.clock_rate,
3992 app_config.media_cfg.channel_count,
3993 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00003994 16, PJMEDIA_TONEGEN_LOOP,
3995 &app_config.ringback_port);
3996 if (status != PJ_SUCCESS)
3997 goto on_error;
3998
Benny Prijonoe28cec82008-06-14 20:40:53 +00003999 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004000 for (i=0; i<RINGBACK_CNT; ++i) {
4001 tone[i].freq1 = RINGBACK_FREQ1;
4002 tone[i].freq2 = RINGBACK_FREQ2;
4003 tone[i].on_msec = RINGBACK_ON;
4004 tone[i].off_msec = RINGBACK_OFF;
4005 }
4006 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004007
Benny Prijono4f966892008-06-14 22:43:56 +00004008 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004009 PJMEDIA_TONEGEN_LOOP);
4010
4011
Benny Prijono91d20f42008-06-14 19:42:37 +00004012 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4013 &app_config.ringback_slot);
4014 if (status != PJ_SUCCESS)
4015 goto on_error;
4016
4017 /* Ring (to alert incoming call) */
4018 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004019 status = pjmedia_tonegen_create2(app_config.pool, &name,
4020 app_config.media_cfg.clock_rate,
4021 app_config.media_cfg.channel_count,
4022 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004023 16, PJMEDIA_TONEGEN_LOOP,
4024 &app_config.ring_port);
4025 if (status != PJ_SUCCESS)
4026 goto on_error;
4027
Benny Prijono4f966892008-06-14 22:43:56 +00004028 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004029 tone[i].freq1 = RING_FREQ1;
4030 tone[i].freq2 = RING_FREQ2;
4031 tone[i].on_msec = RING_ON;
4032 tone[i].off_msec = RING_OFF;
4033 }
Benny Prijono4f966892008-06-14 22:43:56 +00004034 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004035
Benny Prijono4f966892008-06-14 22:43:56 +00004036 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004037 tone, PJMEDIA_TONEGEN_LOOP);
4038
Benny Prijono91d20f42008-06-14 19:42:37 +00004039 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4040 &app_config.ring_slot);
4041 if (status != PJ_SUCCESS)
4042 goto on_error;
4043
4044 }
4045
Benny Prijono87ef89a2007-01-14 00:39:45 +00004046 /* Add UDP transport unless it's disabled. */
4047 if (!app_config.no_udp) {
4048 pjsua_acc_id aid;
4049
4050 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4051 &app_config.udp_cfg,
4052 &transport_id);
4053 if (status != PJ_SUCCESS)
4054 goto on_error;
4055
4056 /* Add local account */
4057 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4058 //pjsua_acc_set_transport(aid, transport_id);
4059 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004060
4061 if (app_config.udp_cfg.port == 0) {
4062 pjsua_transport_info ti;
4063 pj_sockaddr_in *a;
4064
4065 pjsua_transport_get_info(transport_id, &ti);
4066 a = (pj_sockaddr_in*)&ti.local_addr;
4067
4068 tcp_cfg.port = pj_ntohs(a->sin_port);
4069 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004070 }
4071
Benny Prijonoe93e2872006-06-28 16:46:49 +00004072 /* Add TCP transport unless it's disabled */
4073 if (!app_config.no_tcp) {
4074 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004075 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004076 &transport_id);
4077 if (status != PJ_SUCCESS)
4078 goto on_error;
4079
4080 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004081 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004082 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4083
4084 }
4085
Benny Prijonoe93e2872006-06-28 16:46:49 +00004086
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004087#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4088 /* Add TLS transport when application wants one */
4089 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004090
4091 pjsua_acc_id acc_id;
4092
4093 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004094 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004095 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004096 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004097 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004098 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004099 if (status != PJ_SUCCESS)
4100 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004101
4102 /* Add local account */
4103 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4104 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004105 }
4106#endif
4107
Benny Prijonoe93e2872006-06-28 16:46:49 +00004108 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004109 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004110 status = -1;
4111 goto on_error;
4112 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004113
4114
4115 /* Add accounts */
4116 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004117 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004118 if (status != PJ_SUCCESS)
4119 goto on_error;
4120 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4121 }
4122
4123 /* Add buddies */
4124 for (i=0; i<app_config.buddy_cnt; ++i) {
4125 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4126 if (status != PJ_SUCCESS)
4127 goto on_error;
4128 }
4129
4130 /* Optionally set codec orders */
4131 for (i=0; i<app_config.codec_cnt; ++i) {
4132 pjsua_codec_set_priority(&app_config.codec_arg[i],
4133 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4134 }
4135
Benny Prijonofce28542007-12-09 15:41:10 +00004136 /* Optionally disable some codec */
4137 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4138 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4139 }
4140
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004141 /* Add RTP transports */
4142 status = pjsua_media_transports_create(&app_config.rtp_cfg);
4143 if (status != PJ_SUCCESS)
4144 goto on_error;
4145
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004146 /* Set sound device latency */
4147 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4148
Benny Prijono22a300a2006-06-14 20:04:55 +00004149 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004150#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004151 if (app_config.null_audio) {
4152 status = pjsua_set_null_snd_dev();
4153 if (status != PJ_SUCCESS)
4154 return status;
4155 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004156#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004157
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004158 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4159 app_config.playback_dev != PJSUA_INVALID_ID)
4160 {
4161 status = pjsua_set_snd_dev(app_config.capture_dev,
4162 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004163 if (status != PJ_SUCCESS)
4164 goto on_error;
4165 }
4166
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004167 return PJ_SUCCESS;
4168
4169on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004170 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004171 return status;
4172}
4173
4174
Benny Prijonoebc32c32008-06-12 13:30:39 +00004175static int stdout_refresh_proc(void *arg)
4176{
4177 PJ_UNUSED_ARG(arg);
4178
4179 /* Set thread to lowest priority so that it doesn't clobber
4180 * stdout output
4181 */
4182 pj_thread_set_prio(pj_thread_this(),
4183 pj_thread_get_prio_min(pj_thread_this()));
4184
4185 while (!stdout_refresh_quit) {
4186 pj_thread_sleep(stdout_refresh * 1000);
4187 puts(stdout_refresh_text);
4188 fflush(stdout);
4189 }
4190
4191 return 0;
4192}
4193
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004194pj_status_t app_main(void)
4195{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004196 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004197 pj_status_t status;
4198
4199 /* Start pjsua */
4200 status = pjsua_start();
4201 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004202 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004203 return status;
4204 }
4205
Benny Prijonoebc32c32008-06-12 13:30:39 +00004206 /* Start console refresh thread */
4207 if (stdout_refresh > 0) {
4208 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4209 NULL, 0, 0, &stdout_refresh_thread);
4210 }
4211
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004212 console_app_main(&uri_arg);
4213
Benny Prijonoebc32c32008-06-12 13:30:39 +00004214 if (stdout_refresh_thread) {
4215 stdout_refresh_quit = PJ_TRUE;
4216 pj_thread_join(stdout_refresh_thread);
4217 pj_thread_destroy(stdout_refresh_thread);
4218 }
4219
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004220 return PJ_SUCCESS;
4221}
4222
4223pj_status_t app_destroy(void)
4224{
Benny Prijonof762ee72006-12-01 11:14:37 +00004225 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004226 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004227
Benny Prijonoe909eac2006-07-27 22:04:56 +00004228#ifdef STEREO_DEMO
4229 if (app_config.snd) {
4230 pjmedia_snd_port_destroy(app_config.snd);
4231 app_config.snd = NULL;
4232 }
4233#endif
4234
Benny Prijono91d20f42008-06-14 19:42:37 +00004235 /* Close ringback port */
4236 if (app_config.ringback_port &&
4237 app_config.ringback_slot != PJSUA_INVALID_ID)
4238 {
4239 pjsua_conf_remove_port(app_config.ringback_slot);
4240 app_config.ringback_slot = PJSUA_INVALID_ID;
4241 pjmedia_port_destroy(app_config.ringback_port);
4242 app_config.ringback_port = NULL;
4243 }
4244
4245 /* Close ring port */
4246 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4247 pjsua_conf_remove_port(app_config.ring_slot);
4248 app_config.ring_slot = PJSUA_INVALID_ID;
4249 pjmedia_port_destroy(app_config.ring_port);
4250 app_config.ring_port = NULL;
4251 }
4252
Benny Prijono4af234b2007-01-24 02:02:09 +00004253 /* Close tone generators */
4254 for (i=0; i<app_config.tone_count; ++i) {
4255 pjsua_conf_remove_port(app_config.tone_slots[i]);
4256 }
4257
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004258 if (app_config.pool) {
4259 pj_pool_release(app_config.pool);
4260 app_config.pool = NULL;
4261 }
4262
Benny Prijonof762ee72006-12-01 11:14:37 +00004263 status = pjsua_destroy();
4264
4265 pj_bzero(&app_config, sizeof(app_config));
4266
4267 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004268}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004269
4270
4271#ifdef STEREO_DEMO
4272static void stereo_demo()
4273{
4274 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004275 pj_status_t status;
4276
4277 /* Disable existing sound device */
4278 conf = pjsua_set_no_snd_dev();
4279
Benny Prijonoe909eac2006-07-27 22:04:56 +00004280 /* Create stereo-mono splitter/combiner */
4281 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004282 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004283 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004284 2 * conf->info.samples_per_frame,
4285 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004286 0 /* options */,
4287 &splitter);
4288 pj_assert(status == PJ_SUCCESS);
4289
4290 /* Connect channel0 (left channel?) to conference port slot0 */
4291 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4292 0 /*options*/,
4293 conf);
4294 pj_assert(status == PJ_SUCCESS);
4295
4296 /* Create reverse channel for channel1 (right channel?)... */
4297 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4298 splitter,
4299 1 /* ch1 */,
4300 0 /* options */,
4301 &ch1);
4302 pj_assert(status == PJ_SUCCESS);
4303
4304 /* .. and register it to conference bridge (it would be slot1
4305 * if there's no other devices connected to the bridge)
4306 */
4307 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4308 pj_assert(status == PJ_SUCCESS);
4309
4310 /* Create sound device */
4311 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004312 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004313 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004314 2 * conf->info.samples_per_frame,
4315 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004316 0, &app_config.snd);
4317 pj_assert(status == PJ_SUCCESS);
4318
4319
4320 /* Connect the splitter to the sound device */
4321 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4322 pj_assert(status == PJ_SUCCESS);
4323
4324}
4325#endif
4326