blob: 73c2fe551ceb11f5dbce4247b8c5122e54da7169 [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 Prijono59b3ffe2008-08-11 18:10:42 +0000238 puts (" --ec-opt=OPT Select echo canceller algorithm (0=default, ");
239 puts (" 1=speex, 2=suppressor)");
Benny Prijono00cae612006-07-31 15:19:36 +0000240 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000241 puts (" --capture-dev=id Audio capture device ID (default=-1)");
242 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000243 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000244 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000245 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
246 puts (" Specify N=-1 (default) to disable this feature.");
247 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000248 puts (" --no-tones Disable audible tones");
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000249 puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000250
Benny Prijonof76e1392008-06-06 14:51:48 +0000251 puts ("");
252 puts ("Media Transport Options:");
253 puts (" --use-ice Enable ICE (default:no)");
Benny Prijono551af422008-08-07 09:55:52 +0000254 puts (" --ice-no-host Disable ICE host candidates (default: no)");
255 puts (" --ice-no-rtcp Disable RTCP component in ICE (default: no)");
Benny Prijonof76e1392008-06-06 14:51:48 +0000256 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
257 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
258 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
259 puts (" --use-turn Enable TURN relay with ICE (default:no)");
260 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
261 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
262 puts (" --turn-user TURN username");
263 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000264
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000265 puts ("");
266 puts ("Buddy List (can be more than one):");
267 puts (" --add-buddy url Add the specified URL to the buddy list.");
268 puts ("");
269 puts ("User Agent options:");
270 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
271 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000272 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000273 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000274 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000275 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000276
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000277 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000278 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
279 puts ("");
280
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000281 fflush(stdout);
282}
283
284
285/* Set default config. */
286static void default_config(struct app_config *cfg)
287{
Benny Prijono56315612006-07-18 14:39:40 +0000288 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000289 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000290
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000291 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000292 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000293 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
294
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000295 pjsua_logging_config_default(&cfg->log_cfg);
296 pjsua_media_config_default(&cfg->media_cfg);
297 pjsua_transport_config_default(&cfg->udp_cfg);
298 cfg->udp_cfg.port = 5060;
299 pjsua_transport_config_default(&cfg->rtp_cfg);
300 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000301 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000302 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000303 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000304 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000305 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000306 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000307 cfg->capture_dev = PJSUA_INVALID_ID;
308 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000309 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
310 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000311 cfg->ringback_slot = PJSUA_INVALID_ID;
312 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000313
314 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
315 pjsua_acc_config_default(&cfg->acc_cfg[i]);
316
317 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
318 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000319}
320
321
322/*
323 * Read command arguments from config file.
324 */
325static int read_config_file(pj_pool_t *pool, const char *filename,
326 int *app_argc, char ***app_argv)
327{
328 int i;
329 FILE *fhnd;
330 char line[200];
331 int argc = 0;
332 char **argv;
333 enum { MAX_ARGS = 64 };
334
335 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
336 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
337 argv[argc++] = *app_argv[0];
338
339 /* Open config file. */
340 fhnd = fopen(filename, "rt");
341 if (!fhnd) {
342 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
343 fflush(stdout);
344 return -1;
345 }
346
347 /* Scan tokens in the file. */
348 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000349 char *token;
350 char *p;
351 const char *whitespace = " \t\r\n";
352 char cDelimiter;
353 int len, token_len;
354
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000356
357 // Trim ending newlines
358 len = strlen(line);
359 if (line[len-1]=='\n')
360 line[--len] = '\0';
361 if (line[len-1]=='\r')
362 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363
Benny Prijonobf5b4692007-06-28 03:20:17 +0000364 if (len==0) continue;
365
366 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
367 // first, scan whitespaces
368 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
369
370 if (*p == '\0') // are we done yet?
371 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372
Benny Prijonobf5b4692007-06-28 03:20:17 +0000373 if (*p == '"' || *p == '\'') { // is token a quoted string
374 cDelimiter = *p++; // save quote delimiter
375 token = p;
376
377 while (*p != '\0' && *p != cDelimiter) p++;
378
379 if (*p == '\0') // found end of the line, but,
380 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381
Benny Prijonobf5b4692007-06-28 03:20:17 +0000382 } else { // token's not a quoted string
383 token = p;
384
385 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
386
387 cDelimiter = *p;
388 }
389
390 *p = '\0';
391 token_len = p-token;
392
393 if (token_len > 0) {
394 if (*token == '#')
395 break; // ignore remainder of line
396
397 argv[argc] = pj_pool_alloc(pool, token_len + 1);
398 pj_memcpy(argv[argc], token, token_len + 1);
399 ++argc;
400 }
401
402 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000403 }
404 }
405
406 /* Copy arguments from command line */
407 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
408 argv[argc++] = (*app_argv)[i];
409
410 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
411 PJ_LOG(1,(THIS_FILE,
412 "Too many arguments specified in cmd line/config file"));
413 fflush(stdout);
414 fclose(fhnd);
415 return -1;
416 }
417
418 fclose(fhnd);
419
420 /* Assign the new command line back to the original command line. */
421 *app_argc = argc;
422 *app_argv = argv;
423 return 0;
424
425}
426
427static int my_atoi(const char *cs)
428{
429 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000430
431 pj_cstr(&s, cs);
432 if (cs[0] == '-') {
433 s.ptr++, s.slen--;
434 return 0 - (int)pj_strtoul(&s);
435 } else if (cs[0] == '+') {
436 s.ptr++, s.slen--;
437 return pj_strtoul(&s);
438 } else {
439 return pj_strtoul(&s);
440 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000441}
442
443
444/* Parse arguments. */
445static pj_status_t parse_args(int argc, char *argv[],
446 struct app_config *cfg,
447 pj_str_t *uri_to_call)
448{
449 int c;
450 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000451 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonob7944862008-07-19 20:53:49 +0000452 OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000453 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000454 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
455 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000456 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000457 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000458 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000459 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000460 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
461 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijono551af422008-08-07 09:55:52 +0000462 OPT_USE_TURN, OPT_ICE_NO_HOST, OPT_ICE_NO_RTCP, OPT_TURN_SRV,
463 OPT_TURN_TCP, OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000464 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
465 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000466 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000467 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL, OPT_EC_OPT,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000468 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000469 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000470 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000471 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
472 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000473 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000474 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000475 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000476 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000477#ifdef _IONBF
478 OPT_STDOUT_NO_BUF,
479#endif
Benny Prijonofce28542007-12-09 15:41:10 +0000480 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000481 };
482 struct pj_getopt_option long_options[] = {
483 { "config-file",1, 0, OPT_CONFIG_FILE},
484 { "log-file", 1, 0, OPT_LOG_FILE},
485 { "log-level", 1, 0, OPT_LOG_LEVEL},
486 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
Benny Prijonod6e362a2008-07-19 17:53:47 +0000487 { "color", 0, 0, OPT_COLOR},
488 { "no-color", 0, 0, OPT_NO_COLOR},
Benny Prijonob7944862008-07-19 20:53:49 +0000489 { "light-bg", 0, 0, OPT_LIGHT_BG},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000490 { "help", 0, 0, OPT_HELP},
491 { "version", 0, 0, OPT_VERSION},
492 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000493 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000494 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000495 { "null-audio", 0, 0, OPT_NULL_AUDIO},
496 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000497 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000498 { "no-tcp", 0, 0, OPT_NO_TCP},
499 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000500 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000501 { "proxy", 1, 0, OPT_PROXY},
502 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
503 { "registrar", 1, 0, OPT_REGISTRAR},
504 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000505 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000506 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000507 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 { "id", 1, 0, OPT_ID},
509 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000510 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
511 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000512 { "realm", 1, 0, OPT_REALM},
513 { "username", 1, 0, OPT_USERNAME},
514 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000515 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000516 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000517 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000518 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
519 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
520 { "no-presence", 0, 0, OPT_NO_PRESENCE},
521 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000522 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000523 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000524 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000525 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
526 { "auto-conf", 0, 0, OPT_AUTO_CONF},
527 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000528 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000529 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000531
Benny Prijonoc97608e2007-03-23 16:34:20 +0000532 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000533 { "use-turn", 0, 0, OPT_USE_TURN},
534 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
Benny Prijono551af422008-08-07 09:55:52 +0000535 { "ice-no-rtcp",0, 0, OPT_ICE_NO_RTCP},
Benny Prijonof76e1392008-06-06 14:51:48 +0000536 { "turn-srv", 1, 0, OPT_TURN_SRV},
537 { "turn-tcp", 0, 0, OPT_TURN_TCP},
538 { "turn-user", 1, 0, OPT_TURN_USER},
539 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
540
Benny Prijonod8179652008-01-23 20:39:07 +0000541#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
542 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000543 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000544#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000545 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000546 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 { "complexity", 1, 0, OPT_COMPLEXITY},
548 { "quality", 1, 0, OPT_QUALITY},
549 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000550 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000551 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000552 { "ec-opt", 1, 0, OPT_EC_OPT},
Benny Prijono00cae612006-07-31 15:19:36 +0000553 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
554 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
555 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000556 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000557 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000558 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000559 { "duration", 1, 0, OPT_DURATION},
560 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000561 { "use-tls", 0, 0, OPT_USE_TLS},
562 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000563 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
564 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000565 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000566 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
567 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
568 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000569 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000570 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
571 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000572 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
573 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000574 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
575 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000576#ifdef _IONBF
577 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
578#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000579 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000580 { "no-tones", 0, 0, OPT_NO_TONES},
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000581 { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000582 { NULL, 0, 0, 0}
583 };
584 pj_status_t status;
585 pjsua_acc_config *cur_acc;
586 char *config_file = NULL;
587 unsigned i;
588
589 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000590 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000591 while ((c=pj_getopt_long(argc, argv, "", long_options,
592 &option_index)) != -1)
593 {
594 switch (c) {
595 case OPT_CONFIG_FILE:
596 config_file = pj_optarg;
597 break;
598 }
599 if (config_file)
600 break;
601 }
602
603 if (config_file) {
604 status = read_config_file(app_config.pool, config_file, &argc, &argv);
605 if (status != 0)
606 return status;
607 }
608
609 cfg->acc_cnt = 0;
610 cur_acc = &cfg->acc_cfg[0];
611
612
613 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
614 * read from config file.
615 */
616 pj_optind = 0;
617 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000618 pj_str_t tmp;
619 long lval;
620
621 switch (c) {
622
Benny Prijono6f137482006-06-15 11:31:36 +0000623 case OPT_CONFIG_FILE:
624 /* Ignore as this has been processed before */
625 break;
626
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000627 case OPT_LOG_FILE:
628 cfg->log_cfg.log_filename = pj_str(pj_optarg);
629 break;
630
631 case OPT_LOG_LEVEL:
632 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
633 if (c < 0 || c > 6) {
634 PJ_LOG(1,(THIS_FILE,
635 "Error: expecting integer value 0-6 "
636 "for --log-level"));
637 return PJ_EINVAL;
638 }
639 cfg->log_cfg.level = c;
640 pj_log_set_level( c );
641 break;
642
643 case OPT_APP_LOG_LEVEL:
644 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
645 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
646 PJ_LOG(1,(THIS_FILE,
647 "Error: expecting integer value 0-6 "
648 "for --app-log-level"));
649 return PJ_EINVAL;
650 }
651 break;
652
Benny Prijonod6e362a2008-07-19 17:53:47 +0000653 case OPT_COLOR:
654 cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
655 break;
656
657 case OPT_NO_COLOR:
658 cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
659 break;
660
Benny Prijonob7944862008-07-19 20:53:49 +0000661 case OPT_LIGHT_BG:
662 pj_log_set_color(1, PJ_TERM_COLOR_R);
663 pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
664 pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
665 pj_log_set_color(4, 0);
666 pj_log_set_color(5, 0);
667 pj_log_set_color(77, 0);
668 break;
669
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000670 case OPT_HELP:
671 usage();
672 return PJ_EINVAL;
673
674 case OPT_VERSION: /* version */
675 pj_dump_config();
676 return PJ_EINVAL;
677
678 case OPT_NULL_AUDIO:
679 cfg->null_audio = PJ_TRUE;
680 break;
681
682 case OPT_CLOCK_RATE:
683 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000684 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000685 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000686 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000687 return PJ_EINVAL;
688 }
689 cfg->media_cfg.clock_rate = lval;
690 break;
691
Benny Prijonof3758ee2008-02-26 15:32:16 +0000692 case OPT_SND_CLOCK_RATE:
693 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000694 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000695 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000696 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000697 return PJ_EINVAL;
698 }
699 cfg->media_cfg.snd_clock_rate = lval;
700 break;
701
Benny Prijono7d60d052008-03-29 12:24:20 +0000702 case OPT_STEREO:
703 cfg->media_cfg.channel_count = 2;
704 break;
705
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000706 case OPT_LOCAL_PORT: /* local-port */
707 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000708 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000709 PJ_LOG(1,(THIS_FILE,
710 "Error: expecting integer value for "
711 "--local-port"));
712 return PJ_EINVAL;
713 }
714 cfg->udp_cfg.port = (pj_uint16_t)lval;
715 break;
716
Benny Prijono0a5cad82006-09-26 13:21:02 +0000717 case OPT_IP_ADDR: /* ip-addr */
718 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
719 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
720 break;
721
Benny Prijonoe93e2872006-06-28 16:46:49 +0000722 case OPT_NO_UDP: /* no-udp */
723 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000724 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
725 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000726 }
727
728 cfg->no_udp = PJ_TRUE;
729 break;
730
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000731 case OPT_NOREFERSUB: /* norefersub */
732 cfg->no_refersub = PJ_TRUE;
733 break;
734
Benny Prijonoe93e2872006-06-28 16:46:49 +0000735 case OPT_NO_TCP: /* no-tcp */
736 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000737 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
738 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000739 }
740
741 cfg->no_tcp = PJ_TRUE;
742 break;
743
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000744 case OPT_PROXY: /* proxy */
745 if (pjsua_verify_sip_url(pj_optarg) != 0) {
746 PJ_LOG(1,(THIS_FILE,
747 "Error: invalid SIP URL '%s' "
748 "in proxy argument", pj_optarg));
749 return PJ_EINVAL;
750 }
751 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
752 break;
753
754 case OPT_OUTBOUND_PROXY: /* outbound proxy */
755 if (pjsua_verify_sip_url(pj_optarg) != 0) {
756 PJ_LOG(1,(THIS_FILE,
757 "Error: invalid SIP URL '%s' "
758 "in outbound proxy argument", pj_optarg));
759 return PJ_EINVAL;
760 }
761 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
762 break;
763
764 case OPT_REGISTRAR: /* registrar */
765 if (pjsua_verify_sip_url(pj_optarg) != 0) {
766 PJ_LOG(1,(THIS_FILE,
767 "Error: invalid SIP URL '%s' in "
768 "registrar argument", pj_optarg));
769 return PJ_EINVAL;
770 }
771 cur_acc->reg_uri = pj_str(pj_optarg);
772 break;
773
774 case OPT_REG_TIMEOUT: /* reg-timeout */
775 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
776 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
777 PJ_LOG(1,(THIS_FILE,
778 "Error: invalid value for --reg-timeout "
779 "(expecting 1-3600)"));
780 return PJ_EINVAL;
781 }
782 break;
783
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000784 case OPT_PUBLISH: /* publish */
785 cur_acc->publish_enabled = PJ_TRUE;
786 break;
787
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000788 case OPT_100REL: /** 100rel */
789 cur_acc->require_100rel = PJ_TRUE;
790 cfg->cfg.require_100rel = PJ_TRUE;
791 break;
792
Benny Prijono48ab2b72007-11-08 09:24:30 +0000793 case OPT_USE_IMS: /* Activate IMS settings */
794 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000795 break;
796
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000797 case OPT_ID: /* id */
798 if (pjsua_verify_sip_url(pj_optarg) != 0) {
799 PJ_LOG(1,(THIS_FILE,
800 "Error: invalid SIP URL '%s' "
801 "in local id argument", pj_optarg));
802 return PJ_EINVAL;
803 }
804 cur_acc->id = pj_str(pj_optarg);
805 break;
806
807 case OPT_CONTACT: /* contact */
808 if (pjsua_verify_sip_url(pj_optarg) != 0) {
809 PJ_LOG(1,(THIS_FILE,
810 "Error: invalid SIP URL '%s' "
811 "in contact argument", pj_optarg));
812 return PJ_EINVAL;
813 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000814 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000815 break;
816
Benny Prijonofce28542007-12-09 15:41:10 +0000817 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000818 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000819 break;
820
821 case OPT_USE_COMPACT_FORM:
822 /* enable compact form - from Ticket #342 */
823 {
824 extern pj_bool_t pjsip_use_compact_form;
825 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
826 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
827
828 pjsip_use_compact_form = PJ_TRUE;
829 /* do not transmit Allow header */
830 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
831 /* Do not include rtpmap for static payload types (<96) */
832 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
833 }
834 break;
835
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000836 case OPT_NEXT_ACCOUNT: /* Add more account. */
837 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000838 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000839 break;
840
841 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000842 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000843 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000844 break;
845
846 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000847 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000848 break;
849
850 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000851 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
852 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000853#if PJSIP_HAS_DIGEST_AKA_AUTH
854 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
855 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
856 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
857#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000858 break;
859
860 case OPT_NEXT_CRED: /* next credential */
861 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000862 break;
863
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000864 case OPT_NAMESERVER: /* nameserver */
865 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
866 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
867 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
868 return PJ_ETOOMANY;
869 }
870 break;
871
Benny Prijonoebbf6892007-03-24 17:37:25 +0000872 case OPT_STUN_DOMAIN: /* STUN domain */
873 cfg->cfg.stun_domain = pj_str(pj_optarg);
874 break;
875
Benny Prijonoc97608e2007-03-23 16:34:20 +0000876 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000877 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000878 break;
879
880 case OPT_ADD_BUDDY: /* Add to buddy list. */
881 if (pjsua_verify_sip_url(pj_optarg) != 0) {
882 PJ_LOG(1,(THIS_FILE,
883 "Error: invalid URL '%s' in "
884 "--add-buddy option", pj_optarg));
885 return -1;
886 }
887 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
888 PJ_LOG(1,(THIS_FILE,
889 "Error: too many buddies in buddy list."));
890 return -1;
891 }
892 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
893 cfg->buddy_cnt++;
894 break;
895
896 case OPT_AUTO_PLAY:
897 cfg->auto_play = 1;
898 break;
899
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000900 case OPT_AUTO_PLAY_HANGUP:
901 cfg->auto_play_hangup = 1;
902 break;
903
Benny Prijono1ebd6142006-10-19 15:48:02 +0000904 case OPT_AUTO_REC:
905 cfg->auto_rec = 1;
906 break;
907
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000908 case OPT_AUTO_LOOP:
909 cfg->auto_loop = 1;
910 break;
911
Benny Prijono7ca96da2006-08-07 12:11:40 +0000912 case OPT_AUTO_CONF:
913 cfg->auto_conf = 1;
914 break;
915
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000916 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000917 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000918 break;
919
Benny Prijono4af234b2007-01-24 02:02:09 +0000920 case OPT_PLAY_TONE:
921 {
922 int f1, f2, on, off;
923 int n;
924
925 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
926 if (n != 4) {
927 puts("Expecting f1,f2,on,off in --play-tone");
928 return -1;
929 }
930
931 cfg->tones[cfg->tone_count].freq1 = (short)f1;
932 cfg->tones[cfg->tone_count].freq2 = (short)f2;
933 cfg->tones[cfg->tone_count].on_msec = (short)on;
934 cfg->tones[cfg->tone_count].off_msec = (short)off;
935 ++cfg->tone_count;
936 }
937 break;
938
Benny Prijono1ebd6142006-10-19 15:48:02 +0000939 case OPT_REC_FILE:
940 cfg->rec_file = pj_str(pj_optarg);
941 break;
942
Benny Prijonoc97608e2007-03-23 16:34:20 +0000943 case OPT_USE_ICE:
944 cfg->media_cfg.enable_ice = PJ_TRUE;
945 break;
946
Benny Prijonof76e1392008-06-06 14:51:48 +0000947 case OPT_USE_TURN:
948 cfg->media_cfg.enable_turn = PJ_TRUE;
949 break;
950
951 case OPT_ICE_NO_HOST:
952 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
953 break;
954
Benny Prijono551af422008-08-07 09:55:52 +0000955 case OPT_ICE_NO_RTCP:
956 cfg->media_cfg.ice_no_rtcp = PJ_TRUE;
957 break;
958
Benny Prijonof76e1392008-06-06 14:51:48 +0000959 case OPT_TURN_SRV:
960 cfg->media_cfg.turn_server = pj_str(pj_optarg);
961 break;
962
963 case OPT_TURN_TCP:
964 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
965 break;
966
967 case OPT_TURN_USER:
968 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
969 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
970 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
971 break;
972
973 case OPT_TURN_PASSWD:
974 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
975 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
976 break;
977
Benny Prijonod8179652008-01-23 20:39:07 +0000978#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
979 case OPT_USE_SRTP:
980 app_config.cfg.use_srtp = my_atoi(pj_optarg);
981 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
982 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
983 return -1;
984 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000985 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +0000986 break;
Benny Prijonof6508982008-01-25 09:02:33 +0000987 case OPT_SRTP_SECURE:
988 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
989 if (!pj_isdigit(*pj_optarg) ||
990 app_config.cfg.srtp_secure_signaling > 2)
991 {
992 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
993 return -1;
994 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000995 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +0000996 break;
Benny Prijonod8179652008-01-23 20:39:07 +0000997#endif
998
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000999 case OPT_RTP_PORT:
1000 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +00001001 if (cfg->rtp_cfg.port == 0) {
1002 enum { START_PORT=4000 };
1003 unsigned range;
1004
1005 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
1006 cfg->rtp_cfg.port = START_PORT +
1007 ((pj_rand() % range) & 0xFFFE);
1008 }
1009
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001010 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
1011 PJ_LOG(1,(THIS_FILE,
1012 "Error: rtp-port argument value "
1013 "(expecting 1-65535"));
1014 return -1;
1015 }
1016 break;
1017
Benny Prijonofce28542007-12-09 15:41:10 +00001018 case OPT_DIS_CODEC:
1019 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
1020 break;
1021
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001022 case OPT_ADD_CODEC:
1023 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
1024 break;
1025
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001026 /* These options were no longer valid after new pjsua */
1027 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001028 case OPT_COMPLEXITY:
1029 cfg->complexity = my_atoi(pj_optarg);
1030 if (cfg->complexity < 0 || cfg->complexity > 10) {
1031 PJ_LOG(1,(THIS_FILE,
1032 "Error: invalid --complexity (expecting 0-10"));
1033 return -1;
1034 }
1035 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001036 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001037
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001038 case OPT_DURATION:
1039 cfg->duration = my_atoi(pj_optarg);
1040 break;
1041
Benny Prijonof521eb02006-08-06 23:07:25 +00001042 case OPT_THREAD_CNT:
1043 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1044 if (cfg->cfg.thread_cnt > 128) {
1045 PJ_LOG(1,(THIS_FILE,
1046 "Error: invalid --thread-cnt option"));
1047 return -1;
1048 }
1049 break;
1050
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001051 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001052 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1053 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001054 PJ_LOG(1,(THIS_FILE,
1055 "Error: invalid --ptime option"));
1056 return -1;
1057 }
1058 break;
1059
Benny Prijono0a12f002006-07-26 17:05:39 +00001060 case OPT_NO_VAD:
1061 cfg->media_cfg.no_vad = PJ_TRUE;
1062 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001063
Benny Prijonod79f25c2006-08-02 19:41:37 +00001064 case OPT_EC_TAIL:
1065 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1066 if (cfg->media_cfg.ec_tail_len > 1000) {
1067 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1068 "is too big"));
1069 return -1;
1070 }
1071 break;
1072
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001073 case OPT_EC_OPT:
1074 cfg->media_cfg.ec_options = my_atoi(pj_optarg);
1075 break;
1076
Benny Prijono0498d902006-06-19 14:49:14 +00001077 case OPT_QUALITY:
1078 cfg->media_cfg.quality = my_atoi(pj_optarg);
1079 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1080 PJ_LOG(1,(THIS_FILE,
1081 "Error: invalid --quality (expecting 0-10"));
1082 return -1;
1083 }
1084 break;
1085
Benny Prijono00cae612006-07-31 15:19:36 +00001086 case OPT_ILBC_MODE:
1087 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1088 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1089 PJ_LOG(1,(THIS_FILE,
1090 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1091 return -1;
1092 }
1093 break;
1094
1095 case OPT_RX_DROP_PCT:
1096 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1097 if (cfg->media_cfg.rx_drop_pct > 100) {
1098 PJ_LOG(1,(THIS_FILE,
1099 "Error: invalid --rx-drop-pct (expecting <= 100"));
1100 return -1;
1101 }
1102 break;
1103
1104 case OPT_TX_DROP_PCT:
1105 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1106 if (cfg->media_cfg.tx_drop_pct > 100) {
1107 PJ_LOG(1,(THIS_FILE,
1108 "Error: invalid --tx-drop-pct (expecting <= 100"));
1109 return -1;
1110 }
1111 break;
1112
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001113 case OPT_AUTO_ANSWER:
1114 cfg->auto_answer = my_atoi(pj_optarg);
1115 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1116 PJ_LOG(1,(THIS_FILE,
1117 "Error: invalid code in --auto-answer "
1118 "(expecting 100-699"));
1119 return -1;
1120 }
1121 break;
1122
1123 case OPT_MAX_CALLS:
1124 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001125 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001126 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1127 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001128 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001129 return -1;
1130 }
1131 break;
1132
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001133 case OPT_USE_TLS:
1134 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001135#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1136 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1137 return -1;
1138#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001139 break;
1140
1141 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001142 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1143#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1144 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1145 return -1;
1146#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001147 break;
1148
Benny Prijonof3bbc132006-12-25 06:43:59 +00001149 case OPT_TLS_CERT_FILE:
1150 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1151#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1152 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1153 return -1;
1154#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001155 break;
1156
Benny Prijonof3bbc132006-12-25 06:43:59 +00001157 case OPT_TLS_PRIV_FILE:
1158 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1159 break;
1160
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001161 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001162 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1163#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1164 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1165 return -1;
1166#endif
1167 break;
1168
1169 case OPT_TLS_VERIFY_SERVER:
1170 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1171 break;
1172
1173 case OPT_TLS_VERIFY_CLIENT:
1174 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1175 break;
1176
1177 case OPT_TLS_NEG_TIMEOUT:
1178 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001179 break;
1180
Benny Prijonoe10db842008-07-01 15:31:59 +00001181 case OPT_TLS_SRV_NAME:
1182 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1183 break;
1184
Benny Prijono4e5d5512007-03-06 18:11:30 +00001185 case OPT_CAPTURE_DEV:
1186 cfg->capture_dev = atoi(pj_optarg);
1187 break;
1188
1189 case OPT_PLAYBACK_DEV:
1190 cfg->playback_dev = atoi(pj_optarg);
1191 break;
1192
Benny Prijonoebc32c32008-06-12 13:30:39 +00001193 case OPT_STDOUT_REFRESH:
1194 stdout_refresh = atoi(pj_optarg);
1195 break;
1196
1197 case OPT_STDOUT_REFRESH_TEXT:
1198 stdout_refresh_text = pj_optarg;
1199 break;
1200
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001201#ifdef _IONBF
1202 case OPT_STDOUT_NO_BUF:
1203 setvbuf(stdout, NULL, _IONBF, 0);
1204 break;
1205#endif
1206
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001207 case OPT_CAPTURE_LAT:
1208 cfg->capture_lat = atoi(pj_optarg);
1209 break;
1210
1211 case OPT_PLAYBACK_LAT:
1212 cfg->playback_lat = atoi(pj_optarg);
1213 break;
1214
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001215 case OPT_SND_AUTO_CLOSE:
1216 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1217 break;
1218
Benny Prijono91d20f42008-06-14 19:42:37 +00001219 case OPT_NO_TONES:
1220 cfg->no_tones = PJ_TRUE;
1221 break;
1222
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001223 case OPT_JB_MAX_SIZE:
1224 cfg->media_cfg.jb_max = atoi(pj_optarg);
1225 break;
1226
Benny Prijono22a300a2006-06-14 20:04:55 +00001227 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001228 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001229 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001230 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001231 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001232 }
1233 }
1234
1235 if (pj_optind != argc) {
1236 pj_str_t uri_arg;
1237
1238 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1239 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1240 return -1;
1241 }
1242 uri_arg = pj_str(argv[pj_optind]);
1243 if (uri_to_call)
1244 *uri_to_call = uri_arg;
1245 pj_optind++;
1246
1247 /* Add URI to call to buddy list if it's not already there */
1248 for (i=0; i<cfg->buddy_cnt; ++i) {
1249 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1250 break;
1251 }
1252 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1253 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1254 }
1255
1256 } else {
1257 if (uri_to_call)
1258 uri_to_call->slen = 0;
1259 }
1260
1261 if (pj_optind != argc) {
1262 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1263 return PJ_EINVAL;
1264 }
1265
Benny Prijono56315612006-07-18 14:39:40 +00001266 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1267 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001268
1269 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001270 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1271
1272 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001274 acfg->cred_count++;
1275 }
1276
1277 /* When IMS mode is enabled for the account, verify that settings
1278 * are okay.
1279 */
1280 /* For now we check if IMS mode is activated by looking if
1281 * initial_auth is set.
1282 */
1283 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1284 /* Realm must point to the real domain */
1285 if (*acfg->cred_info[0].realm.ptr=='*') {
1286 PJ_LOG(1,(THIS_FILE,
1287 "Error: cannot use '*' as realm with IMS"));
1288 return PJ_EINVAL;
1289 }
1290
1291 /* Username for authentication must be in a@b format */
1292 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1293 PJ_LOG(1,(THIS_FILE,
1294 "Error: Username for authentication must "
1295 "be in user@domain format with IMS"));
1296 return PJ_EINVAL;
1297 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001298 }
1299 }
1300
1301
1302 return PJ_SUCCESS;
1303}
1304
1305
1306/*
1307 * Save account settings
1308 */
1309static void write_account_settings(int acc_index, pj_str_t *result)
1310{
1311 unsigned i;
1312 char line[128];
1313 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1314
1315
1316 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1317 pj_strcat2(result, line);
1318
1319
1320 /* Identity */
1321 if (acc_cfg->id.slen) {
1322 pj_ansi_sprintf(line, "--id %.*s\n",
1323 (int)acc_cfg->id.slen,
1324 acc_cfg->id.ptr);
1325 pj_strcat2(result, line);
1326 }
1327
1328 /* Registrar server */
1329 if (acc_cfg->reg_uri.slen) {
1330 pj_ansi_sprintf(line, "--registrar %.*s\n",
1331 (int)acc_cfg->reg_uri.slen,
1332 acc_cfg->reg_uri.ptr);
1333 pj_strcat2(result, line);
1334
1335 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1336 acc_cfg->reg_timeout);
1337 pj_strcat2(result, line);
1338 }
1339
1340 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001341 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001342 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001343 (int)acc_cfg->force_contact.slen,
1344 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001345 pj_strcat2(result, line);
1346 }
1347
Benny Prijonofce28542007-12-09 15:41:10 +00001348 /* */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001349 if (acc_cfg->allow_contact_rewrite==0)
Benny Prijonofce28542007-12-09 15:41:10 +00001350 {
Benny Prijono251e02a2008-07-25 10:05:55 +00001351 pj_ansi_sprintf(line, "--auto-update-nat %i\n",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001352 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001353 pj_strcat2(result, line);
1354 }
1355
Benny Prijonod8179652008-01-23 20:39:07 +00001356#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1357 /* SRTP */
1358 if (acc_cfg->use_srtp) {
1359 pj_ansi_sprintf(line, "--use-srtp %i\n",
1360 (int)acc_cfg->use_srtp);
1361 pj_strcat2(result, line);
1362 }
1363#endif
1364
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001365 /* Proxy */
1366 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1367 pj_ansi_sprintf(line, "--proxy %.*s\n",
1368 (int)acc_cfg->proxy[i].slen,
1369 acc_cfg->proxy[i].ptr);
1370 pj_strcat2(result, line);
1371 }
1372
1373 /* Credentials */
1374 for (i=0; i<acc_cfg->cred_count; ++i) {
1375 if (acc_cfg->cred_info[i].realm.slen) {
1376 pj_ansi_sprintf(line, "--realm %.*s\n",
1377 (int)acc_cfg->cred_info[i].realm.slen,
1378 acc_cfg->cred_info[i].realm.ptr);
1379 pj_strcat2(result, line);
1380 }
1381
1382 if (acc_cfg->cred_info[i].username.slen) {
1383 pj_ansi_sprintf(line, "--username %.*s\n",
1384 (int)acc_cfg->cred_info[i].username.slen,
1385 acc_cfg->cred_info[i].username.ptr);
1386 pj_strcat2(result, line);
1387 }
1388
1389 if (acc_cfg->cred_info[i].data.slen) {
1390 pj_ansi_sprintf(line, "--password %.*s\n",
1391 (int)acc_cfg->cred_info[i].data.slen,
1392 acc_cfg->cred_info[i].data.ptr);
1393 pj_strcat2(result, line);
1394 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001395
1396 if (i != acc_cfg->cred_count - 1)
1397 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001398 }
1399
1400}
1401
1402
1403/*
1404 * Write settings.
1405 */
1406static int write_settings(const struct app_config *config,
1407 char *buf, pj_size_t max)
1408{
1409 unsigned acc_index;
1410 unsigned i;
1411 pj_str_t cfg;
1412 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001413 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001414
1415 PJ_UNUSED_ARG(max);
1416
1417 cfg.ptr = buf;
1418 cfg.slen = 0;
1419
1420 /* Logging. */
1421 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1422 pj_ansi_sprintf(line, "--log-level %d\n",
1423 config->log_cfg.level);
1424 pj_strcat2(&cfg, line);
1425
1426 pj_ansi_sprintf(line, "--app-log-level %d\n",
1427 config->log_cfg.console_level);
1428 pj_strcat2(&cfg, line);
1429
1430 if (config->log_cfg.log_filename.slen) {
1431 pj_ansi_sprintf(line, "--log-file %.*s\n",
1432 (int)config->log_cfg.log_filename.slen,
1433 config->log_cfg.log_filename.ptr);
1434 pj_strcat2(&cfg, line);
1435 }
1436
1437
1438 /* Save account settings. */
1439 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1440
1441 write_account_settings(acc_index, &cfg);
1442
1443 if (acc_index < config->acc_cnt-1)
1444 pj_strcat2(&cfg, "--next-account\n");
1445 }
1446
1447
1448 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1449
1450 /* Outbound proxy */
1451 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1452 pj_ansi_sprintf(line, "--outbound %.*s\n",
1453 (int)config->cfg.outbound_proxy[i].slen,
1454 config->cfg.outbound_proxy[i].ptr);
1455 pj_strcat2(&cfg, line);
1456 }
1457
1458
1459 /* UDP Transport. */
1460 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1461 pj_strcat2(&cfg, line);
1462
Benny Prijono0a5cad82006-09-26 13:21:02 +00001463 /* IP address, if any. */
1464 if (config->udp_cfg.public_addr.slen) {
1465 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1466 (int)config->udp_cfg.public_addr.slen,
1467 config->udp_cfg.public_addr.ptr);
1468 pj_strcat2(&cfg, line);
1469 }
1470
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001471 /* No TCP ? */
1472 if (config->no_tcp) {
1473 pj_strcat2(&cfg, "--no-tcp\n");
1474 }
1475
1476 /* No UDP ? */
1477 if (config->no_udp) {
1478 pj_strcat2(&cfg, "--no-udp\n");
1479 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001480
1481 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001482 if (config->cfg.stun_domain.slen) {
1483 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1484 (int)config->cfg.stun_domain.slen,
1485 config->cfg.stun_domain.ptr);
1486 pj_strcat2(&cfg, line);
1487 }
1488 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001489 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001490 (int)config->cfg.stun_host.slen,
1491 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001492 pj_strcat2(&cfg, line);
1493 }
1494
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001495 /* TLS */
1496 if (config->use_tls)
1497 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001498 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001499 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001500 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1501 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001502 pj_strcat2(&cfg, line);
1503 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001504 if (config->udp_cfg.tls_setting.cert_file.slen) {
1505 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1506 (int)config->udp_cfg.tls_setting.cert_file.slen,
1507 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001508 pj_strcat2(&cfg, line);
1509 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001510 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1511 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1512 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1513 config->udp_cfg.tls_setting.privkey_file.ptr);
1514 pj_strcat2(&cfg, line);
1515 }
1516
1517 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001518 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001519 (int)config->udp_cfg.tls_setting.password.slen,
1520 config->udp_cfg.tls_setting.password.ptr);
1521 pj_strcat2(&cfg, line);
1522 }
1523
Benny Prijonoe10db842008-07-01 15:31:59 +00001524 if (config->udp_cfg.tls_setting.server_name.slen) {
1525 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1526 (int)config->udp_cfg.tls_setting.server_name.slen,
1527 config->udp_cfg.tls_setting.server_name.ptr);
1528 pj_strcat2(&cfg, line);
1529 }
1530
Benny Prijonof3bbc132006-12-25 06:43:59 +00001531 if (config->udp_cfg.tls_setting.verify_server)
1532 pj_strcat2(&cfg, "--tls-verify-server\n");
1533
1534 if (config->udp_cfg.tls_setting.verify_client)
1535 pj_strcat2(&cfg, "--tls-verify-client\n");
1536
1537 if (config->udp_cfg.tls_setting.timeout.sec) {
1538 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001539 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001540 pj_strcat2(&cfg, line);
1541 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001542
1543 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1544
Benny Prijonof6508982008-01-25 09:02:33 +00001545 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001546#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001547 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1548 pj_ansi_sprintf(line, "--use-srtp %d\n",
1549 app_config.cfg.use_srtp);
1550 pj_strcat2(&cfg, line);
1551 }
1552 if (app_config.cfg.srtp_secure_signaling !=
1553 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1554 {
1555 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1556 app_config.cfg.srtp_secure_signaling);
1557 pj_strcat2(&cfg, line);
1558 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001559#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001560
Benny Prijonof76e1392008-06-06 14:51:48 +00001561 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001562 if (config->media_cfg.enable_ice)
1563 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001564
1565 if (config->media_cfg.enable_turn)
1566 pj_strcat2(&cfg, "--use-turn\n");
1567
1568 if (config->media_cfg.ice_no_host_cands)
1569 pj_strcat2(&cfg, "--ice-no-host\n");
1570
Benny Prijono551af422008-08-07 09:55:52 +00001571 if (config->media_cfg.ice_no_rtcp)
1572 pj_strcat2(&cfg, "--ice-no-rtcp\n");
1573
Benny Prijonof76e1392008-06-06 14:51:48 +00001574 if (config->media_cfg.turn_server.slen) {
1575 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1576 (int)config->media_cfg.turn_server.slen,
1577 config->media_cfg.turn_server.ptr);
1578 pj_strcat2(&cfg, line);
1579 }
1580
1581 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1582 pj_strcat2(&cfg, "--turn-tcp\n");
1583
1584 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1585 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1586 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1587 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1588 pj_strcat2(&cfg, line);
1589 }
1590
1591 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1592 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1593 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1594 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1595 pj_strcat2(&cfg, line);
1596 }
1597
1598 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001599 if (config->null_audio)
1600 pj_strcat2(&cfg, "--null-audio\n");
1601 if (config->auto_play)
1602 pj_strcat2(&cfg, "--auto-play\n");
1603 if (config->auto_loop)
1604 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001605 if (config->auto_conf)
1606 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001607 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001608 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001609 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001610 pj_strcat2(&cfg, line);
1611 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001612 for (i=0; i<config->tone_count; ++i) {
1613 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1614 config->tones[i].freq1, config->tones[i].freq2,
1615 config->tones[i].on_msec, config->tones[i].off_msec);
1616 pj_strcat2(&cfg, line);
1617 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001618 if (config->rec_file.slen) {
1619 pj_ansi_sprintf(line, "--rec-file %s\n",
1620 config->rec_file.ptr);
1621 pj_strcat2(&cfg, line);
1622 }
1623 if (config->auto_rec)
1624 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001625 if (config->capture_dev != PJSUA_INVALID_ID) {
1626 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1627 pj_strcat2(&cfg, line);
1628 }
1629 if (config->playback_dev != PJSUA_INVALID_ID) {
1630 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1631 pj_strcat2(&cfg, line);
1632 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001633 if (config->media_cfg.snd_auto_close_time != -1) {
1634 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1635 config->media_cfg.snd_auto_close_time);
1636 pj_strcat2(&cfg, line);
1637 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001638 if (config->no_tones) {
1639 pj_strcat2(&cfg, "--no-tones\n");
1640 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001641 if (config->media_cfg.jb_max != -1) {
1642 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1643 config->media_cfg.jb_max);
1644 pj_strcat2(&cfg, line);
1645 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001646
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001647 /* Sound device latency */
1648 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1649 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1650 pj_strcat2(&cfg, line);
1651 }
1652 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1653 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1654 pj_strcat2(&cfg, line);
1655 }
1656
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001657 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001658 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001659 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001660 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001661 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001662 } else {
1663 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1664 config->media_cfg.clock_rate);
1665 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001666 }
Benny Prijono70972992006-08-05 11:13:58 +00001667
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001668 if (config->media_cfg.snd_clock_rate &&
1669 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1670 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001671 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1672 config->media_cfg.snd_clock_rate);
1673 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001674 }
1675
Benny Prijono7d60d052008-03-29 12:24:20 +00001676 /* Stereo mode. */
1677 if (config->media_cfg.channel_count == 2) {
1678 pj_ansi_sprintf(line, "--stereo\n");
1679 pj_strcat2(&cfg, line);
1680 }
1681
Benny Prijono70972992006-08-05 11:13:58 +00001682 /* quality */
1683 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001684 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001685 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001686 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001687 } else {
1688 pj_ansi_sprintf(line, "#using default --quality %d\n",
1689 config->media_cfg.quality);
1690 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001691 }
Benny Prijono0498d902006-06-19 14:49:14 +00001692
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001693
1694 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001695 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001696 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001697 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001698 pj_strcat2(&cfg, line);
1699 }
1700
Benny Prijono70972992006-08-05 11:13:58 +00001701 /* no-vad */
1702 if (config->media_cfg.no_vad) {
1703 pj_strcat2(&cfg, "--no-vad\n");
1704 }
1705
1706 /* ec-tail */
1707 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1708 pj_ansi_sprintf(line, "--ec-tail %d\n",
1709 config->media_cfg.ec_tail_len);
1710 pj_strcat2(&cfg, line);
1711 } else {
1712 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1713 config->media_cfg.ec_tail_len);
1714 pj_strcat2(&cfg, line);
1715 }
1716
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001717 /* ec-opt */
1718 if (config->media_cfg.ec_options != 0) {
1719 pj_ansi_sprintf(line, "--ec-opt %d\n",
1720 config->media_cfg.ec_options);
1721 pj_strcat2(&cfg, line);
1722 }
Benny Prijono70972992006-08-05 11:13:58 +00001723
1724 /* ilbc-mode */
1725 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1726 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1727 config->media_cfg.ilbc_mode);
1728 pj_strcat2(&cfg, line);
1729 } else {
1730 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1731 config->media_cfg.ilbc_mode);
1732 pj_strcat2(&cfg, line);
1733 }
1734
1735 /* RTP drop */
1736 if (config->media_cfg.tx_drop_pct) {
1737 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1738 config->media_cfg.tx_drop_pct);
1739 pj_strcat2(&cfg, line);
1740
1741 }
1742 if (config->media_cfg.rx_drop_pct) {
1743 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1744 config->media_cfg.rx_drop_pct);
1745 pj_strcat2(&cfg, line);
1746
1747 }
1748
1749
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001750 /* Start RTP port. */
1751 pj_ansi_sprintf(line, "--rtp-port %d\n",
1752 config->rtp_cfg.port);
1753 pj_strcat2(&cfg, line);
1754
1755 /* Add codec. */
1756 for (i=0; i<config->codec_cnt; ++i) {
1757 pj_ansi_sprintf(line, "--add-codec %s\n",
1758 config->codec_arg[i].ptr);
1759 pj_strcat2(&cfg, line);
1760 }
Benny Prijonofce28542007-12-09 15:41:10 +00001761 /* Disable codec */
1762 for (i=0; i<config->codec_dis_cnt; ++i) {
1763 pj_ansi_sprintf(line, "--dis-codec %s\n",
1764 config->codec_dis[i].ptr);
1765 pj_strcat2(&cfg, line);
1766 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001767
1768 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1769
1770 /* Auto-answer. */
1771 if (config->auto_answer != 0) {
1772 pj_ansi_sprintf(line, "--auto-answer %d\n",
1773 config->auto_answer);
1774 pj_strcat2(&cfg, line);
1775 }
1776
1777 /* Max calls. */
1778 pj_ansi_sprintf(line, "--max-calls %d\n",
1779 config->cfg.max_calls);
1780 pj_strcat2(&cfg, line);
1781
1782 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001783 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001784 pj_ansi_sprintf(line, "--duration %d\n",
1785 config->duration);
1786 pj_strcat2(&cfg, line);
1787 }
1788
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001789 /* norefersub ? */
1790 if (config->no_refersub) {
1791 pj_strcat2(&cfg, "--norefersub\n");
1792 }
1793
Benny Prijonofce28542007-12-09 15:41:10 +00001794 if (pjsip_use_compact_form)
1795 {
1796 pj_strcat2(&cfg, "--use-compact-form\n");
1797 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001798
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001799 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1800
1801 /* Add buddies. */
1802 for (i=0; i<config->buddy_cnt; ++i) {
1803 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1804 (int)config->buddy_cfg[i].uri.slen,
1805 config->buddy_cfg[i].uri.ptr);
1806 pj_strcat2(&cfg, line);
1807 }
1808
1809
1810 *(cfg.ptr + cfg.slen) = '\0';
1811 return cfg.slen;
1812}
1813
1814
1815/*
1816 * Dump application states.
1817 */
1818static void app_dump(pj_bool_t detail)
1819{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001820 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001821}
1822
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001823/*
1824 * Print log of call states. Since call states may be too long for logger,
1825 * printing it is a bit tricky, it should be printed part by part as long
1826 * as the logger can accept.
1827 */
1828static void log_call_dump(int call_id) {
1829 unsigned call_dump_len;
1830 unsigned part_len;
1831 unsigned part_idx;
1832 unsigned log_decor;
1833
1834 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1835 sizeof(some_buf), " ");
1836 call_dump_len = strlen(some_buf);
1837
1838 log_decor = pj_log_get_decor();
1839 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1840 PJ_LOG(3,(THIS_FILE, "\n"));
1841 pj_log_set_decor(0);
1842
1843 part_idx = 0;
1844 part_len = PJ_LOG_MAX_SIZE-80;
1845 while (part_idx < call_dump_len) {
1846 char p_orig, *p;
1847
1848 p = &some_buf[part_idx];
1849 if (part_idx + part_len > call_dump_len)
1850 part_len = call_dump_len - part_idx;
1851 p_orig = p[part_len];
1852 p[part_len] = '\0';
1853 PJ_LOG(3,(THIS_FILE, "%s", p));
1854 p[part_len] = p_orig;
1855 part_idx += part_len;
1856 }
1857 pj_log_set_decor(log_decor);
1858}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001859
1860/*****************************************************************************
1861 * Console application
1862 */
1863
Benny Prijono91d20f42008-06-14 19:42:37 +00001864static void ringback_start(pjsua_call_id call_id)
1865{
1866 if (app_config.no_tones)
1867 return;
1868
1869 if (app_config.call_data[call_id].ringback_on)
1870 return;
1871
1872 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1873
1874 if (++app_config.ringback_cnt==1 &&
1875 app_config.ringback_slot!=PJSUA_INVALID_ID)
1876 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001877 pjsua_conf_connect(app_config.ringback_slot, 0);
1878 }
1879}
1880
1881static void ring_stop(pjsua_call_id call_id)
1882{
1883 if (app_config.no_tones)
1884 return;
1885
1886 if (app_config.call_data[call_id].ringback_on) {
1887 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1888
1889 pj_assert(app_config.ringback_cnt>0);
1890 if (--app_config.ringback_cnt == 0 &&
1891 app_config.ringback_slot!=PJSUA_INVALID_ID)
1892 {
1893 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001894 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001895 }
1896 }
1897
1898 if (app_config.call_data[call_id].ring_on) {
1899 app_config.call_data[call_id].ring_on = PJ_FALSE;
1900
1901 pj_assert(app_config.ring_cnt>0);
1902 if (--app_config.ring_cnt == 0 &&
1903 app_config.ring_slot!=PJSUA_INVALID_ID)
1904 {
1905 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001906 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001907 }
1908 }
1909}
1910
1911static void ring_start(pjsua_call_id call_id)
1912{
1913 if (app_config.no_tones)
1914 return;
1915
1916 if (app_config.call_data[call_id].ring_on)
1917 return;
1918
1919 app_config.call_data[call_id].ring_on = PJ_TRUE;
1920
1921 if (++app_config.ring_cnt==1 &&
1922 app_config.ring_slot!=PJSUA_INVALID_ID)
1923 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001924 pjsua_conf_connect(app_config.ring_slot, 0);
1925 }
1926}
1927
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001928/*
1929 * Find next call when current call is disconnected or when user
1930 * press ']'
1931 */
1932static pj_bool_t find_next_call(void)
1933{
1934 int i, max;
1935
1936 max = pjsua_call_get_max_count();
1937 for (i=current_call+1; i<max; ++i) {
1938 if (pjsua_call_is_active(i)) {
1939 current_call = i;
1940 return PJ_TRUE;
1941 }
1942 }
1943
1944 for (i=0; i<current_call; ++i) {
1945 if (pjsua_call_is_active(i)) {
1946 current_call = i;
1947 return PJ_TRUE;
1948 }
1949 }
1950
1951 current_call = PJSUA_INVALID_ID;
1952 return PJ_FALSE;
1953}
1954
1955
1956/*
1957 * Find previous call when user press '['
1958 */
1959static pj_bool_t find_prev_call(void)
1960{
1961 int i, max;
1962
1963 max = pjsua_call_get_max_count();
1964 for (i=current_call-1; i>=0; --i) {
1965 if (pjsua_call_is_active(i)) {
1966 current_call = i;
1967 return PJ_TRUE;
1968 }
1969 }
1970
1971 for (i=max-1; i>current_call; --i) {
1972 if (pjsua_call_is_active(i)) {
1973 current_call = i;
1974 return PJ_TRUE;
1975 }
1976 }
1977
1978 current_call = PJSUA_INVALID_ID;
1979 return PJ_FALSE;
1980}
1981
1982
Benny Prijono804ff0a2006-09-14 11:17:48 +00001983/* Callback from timer when the maximum call duration has been
1984 * exceeded.
1985 */
1986static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1987 struct pj_timer_entry *entry)
1988{
1989 pjsua_call_id call_id = entry->id;
1990 pjsua_msg_data msg_data;
1991 pjsip_generic_string_hdr warn;
1992 pj_str_t hname = pj_str("Warning");
1993 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1994
1995 PJ_UNUSED_ARG(timer_heap);
1996
Benny Prijono148c9dd2006-09-19 13:37:53 +00001997 if (call_id == PJSUA_INVALID_ID) {
1998 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1999 return;
2000 }
2001
Benny Prijono804ff0a2006-09-14 11:17:48 +00002002 /* Add warning header */
2003 pjsua_msg_data_init(&msg_data);
2004 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
2005 pj_list_push_back(&msg_data.hdr_list, &warn);
2006
2007 /* Call duration has been exceeded; disconnect the call */
2008 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
2009 "for call %d, disconnecting the call",
2010 app_config.duration, call_id));
2011 entry->id = PJSUA_INVALID_ID;
2012 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
2013}
2014
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002015
2016/*
2017 * Handler when invite state has changed.
2018 */
2019static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
2020{
2021 pjsua_call_info call_info;
2022
2023 PJ_UNUSED_ARG(e);
2024
2025 pjsua_call_get_info(call_id, &call_info);
2026
2027 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2028
Benny Prijono91d20f42008-06-14 19:42:37 +00002029 /* Stop all ringback for this call */
2030 ring_stop(call_id);
2031
Benny Prijono804ff0a2006-09-14 11:17:48 +00002032 /* Cancel duration timer, if any */
2033 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2034 struct call_data *cd = &app_config.call_data[call_id];
2035 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2036
2037 cd->timer.id = PJSUA_INVALID_ID;
2038 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2039 }
2040
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002041 /* Rewind play file when hangup automatically,
2042 * since file is not looped
2043 */
2044 if (app_config.auto_play_hangup)
2045 pjsua_player_set_pos(app_config.wav_id, 0);
2046
2047
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002048 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2049 call_id,
2050 call_info.last_status,
2051 call_info.last_status_text.ptr));
2052
2053 if (call_id == current_call) {
2054 find_next_call();
2055 }
2056
Benny Prijono4be63b52006-11-25 14:50:25 +00002057 /* Dump media state upon disconnected */
2058 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002059 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002060 "Call %d disconnected, dumping media stats..",
2061 call_id));
2062 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002063 }
2064
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002065 } else {
2066
Benny Prijono804ff0a2006-09-14 11:17:48 +00002067 if (app_config.duration!=NO_LIMIT &&
2068 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2069 {
2070 /* Schedule timer to hangup call after the specified duration */
2071 struct call_data *cd = &app_config.call_data[call_id];
2072 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2073 pj_time_val delay;
2074
2075 cd->timer.id = call_id;
2076 delay.sec = app_config.duration;
2077 delay.msec = 0;
2078 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2079 }
2080
Benny Prijono4be63b52006-11-25 14:50:25 +00002081 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2082 int code;
2083 pj_str_t reason;
2084 pjsip_msg *msg;
2085
2086 /* This can only occur because of TX or RX message */
2087 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2088
2089 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2090 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2091 } else {
2092 msg = e->body.tsx_state.src.tdata->msg;
2093 }
2094
2095 code = msg->line.status.code;
2096 reason = msg->line.status.reason;
2097
Benny Prijono91d20f42008-06-14 19:42:37 +00002098 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2099 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2100 msg->body == NULL &&
2101 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2102 {
2103 ringback_start(call_id);
2104 }
2105
Benny Prijono4be63b52006-11-25 14:50:25 +00002106 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2107 call_id, call_info.state_text.ptr,
2108 code, (int)reason.slen, reason.ptr));
2109 } else {
2110 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2111 call_id,
2112 call_info.state_text.ptr));
2113 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002114
2115 if (current_call==PJSUA_INVALID_ID)
2116 current_call = call_id;
2117
2118 }
2119}
2120
2121
2122/**
2123 * Handler when there is incoming call.
2124 */
2125static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2126 pjsip_rx_data *rdata)
2127{
2128 pjsua_call_info call_info;
2129
2130 PJ_UNUSED_ARG(acc_id);
2131 PJ_UNUSED_ARG(rdata);
2132
2133 pjsua_call_get_info(call_id, &call_info);
2134
Benny Prijono91d20f42008-06-14 19:42:37 +00002135 /* Start ringback */
2136 ring_start(call_id);
2137
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002138 if (app_config.auto_answer > 0) {
2139 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2140 }
2141
2142 if (app_config.auto_answer < 200) {
2143 PJ_LOG(3,(THIS_FILE,
2144 "Incoming call for account %d!\n"
2145 "From: %s\n"
2146 "To: %s\n"
2147 "Press a to answer or h to reject call",
2148 acc_id,
2149 call_info.remote_info.ptr,
2150 call_info.local_info.ptr));
2151 }
2152}
2153
2154
2155/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002156 * Handler when a transaction within a call has changed state.
2157 */
2158static void on_call_tsx_state(pjsua_call_id call_id,
2159 pjsip_transaction *tsx,
2160 pjsip_event *e)
2161{
2162 const pjsip_method info_method =
2163 {
2164 PJSIP_OTHER_METHOD,
2165 { "INFO", 4 }
2166 };
2167
2168 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2169 /*
2170 * Handle INFO method.
2171 */
2172 if (tsx->role == PJSIP_ROLE_UAC &&
2173 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002174 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2175 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002176 {
2177 /* Status of outgoing INFO request */
2178 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2179 PJ_LOG(4,(THIS_FILE,
2180 "Call %d: DTMF sent successfully with INFO",
2181 call_id));
2182 } else if (tsx->status_code >= 300) {
2183 PJ_LOG(4,(THIS_FILE,
2184 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2185 call_id,
2186 tsx->status_code,
2187 (int)tsx->status_text.slen,
2188 tsx->status_text.ptr));
2189 }
2190 } else if (tsx->role == PJSIP_ROLE_UAS &&
2191 tsx->state == PJSIP_TSX_STATE_TRYING)
2192 {
2193 /* Answer incoming INFO with 200/OK */
2194 pjsip_rx_data *rdata;
2195 pjsip_tx_data *tdata;
2196 pj_status_t status;
2197
2198 rdata = e->body.tsx_state.src.rdata;
2199
2200 if (rdata->msg_info.msg->body) {
2201 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2202 200, NULL, &tdata);
2203 if (status == PJ_SUCCESS)
2204 status = pjsip_tsx_send_msg(tsx, tdata);
2205
2206 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2207 call_id,
2208 (int)rdata->msg_info.msg->body->len,
2209 rdata->msg_info.msg->body->data));
2210 } else {
2211 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2212 400, NULL, &tdata);
2213 if (status == PJ_SUCCESS)
2214 status = pjsip_tsx_send_msg(tsx, tdata);
2215 }
2216 }
2217 }
2218}
2219
2220
2221/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002222 * Callback on media state changed event.
2223 * The action may connect the call to sound device, to file, or
2224 * to loop the call.
2225 */
2226static void on_call_media_state(pjsua_call_id call_id)
2227{
2228 pjsua_call_info call_info;
2229
2230 pjsua_call_get_info(call_id, &call_info);
2231
Benny Prijono91d20f42008-06-14 19:42:37 +00002232 /* Stop ringback */
2233 ring_stop(call_id);
2234
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002235 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
2236 * otherwise we should NOT connect the ports.
2237 */
2238 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE ||
2239 call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
2240 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002241 pj_bool_t connect_sound = PJ_TRUE;
2242
2243 /* Loopback sound, if desired */
2244 if (app_config.auto_loop) {
2245 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2246 connect_sound = PJ_FALSE;
Benny Prijonof6c77f42008-08-13 13:52:19 +00002247 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00002248
Benny Prijonof6c77f42008-08-13 13:52:19 +00002249 /* Automatically record conversation, if desired */
2250 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2251 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002252 }
2253
2254 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002255 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2256 app_config.wav_port != PJSUA_INVALID_ID)
2257 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002258 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2259 connect_sound = PJ_FALSE;
2260 }
2261
Benny Prijono7ca96da2006-08-07 12:11:40 +00002262 /* Put call in conference with other calls, if desired */
2263 if (app_config.auto_conf) {
2264 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002265 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002266 unsigned i;
2267
2268 /* Get all calls, and establish media connection between
2269 * this call and other calls.
2270 */
2271 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002272
Benny Prijono7ca96da2006-08-07 12:11:40 +00002273 for (i=0; i<call_cnt; ++i) {
2274 if (call_ids[i] == call_id)
2275 continue;
2276
2277 if (!pjsua_call_has_media(call_ids[i]))
2278 continue;
2279
2280 pjsua_conf_connect(call_info.conf_slot,
2281 pjsua_call_get_conf_port(call_ids[i]));
2282 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2283 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002284
2285 /* Automatically record conversation, if desired */
2286 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2287 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2288 app_config.rec_port);
2289 }
2290
Benny Prijono7ca96da2006-08-07 12:11:40 +00002291 }
2292
2293 /* Also connect call to local sound device */
2294 connect_sound = PJ_TRUE;
2295 }
2296
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002297 /* Otherwise connect to sound device */
2298 if (connect_sound) {
2299 pjsua_conf_connect(call_info.conf_slot, 0);
2300 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002301
2302 /* Automatically record conversation, if desired */
2303 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2304 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2305 pjsua_conf_connect(0, app_config.rec_port);
2306 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002307 }
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002308 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002309
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002310 /* Handle media status */
2311 switch (call_info.media_status) {
2312 case PJSUA_CALL_MEDIA_ACTIVE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002313 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002314 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002315
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002316 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002317 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2318 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002319 break;
2320
2321 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002322 PJ_LOG(3,(THIS_FILE,
2323 "Media for call %d is suspended (hold) by remote",
2324 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002325 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002326
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002327 case PJSUA_CALL_MEDIA_ERROR:
2328 PJ_LOG(3,(THIS_FILE,
Benny Prijono096c56c2007-09-15 08:30:16 +00002329 "Media has reported error, disconnecting call"));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002330 {
2331 pj_str_t reason = pj_str("ICE negotiation failed");
2332 pjsua_call_hangup(call_id, 500, &reason, NULL);
2333 }
2334 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002335
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002336 case PJSUA_CALL_MEDIA_NONE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002337 PJ_LOG(3,(THIS_FILE,
2338 "Media for call %d is inactive",
2339 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002340 break;
2341
2342 default:
2343 pj_assert(!"Unhandled media status");
2344 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002345 }
2346}
2347
Benny Prijono0875ae82006-12-26 00:11:48 +00002348/*
2349 * DTMF callback.
2350 */
2351static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2352{
2353 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2354}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002355
2356/*
2357 * Handler registration status has changed.
2358 */
2359static void on_reg_state(pjsua_acc_id acc_id)
2360{
2361 PJ_UNUSED_ARG(acc_id);
2362
2363 // Log already written.
2364}
2365
2366
2367/*
2368 * Handler on buddy state changed.
2369 */
2370static void on_buddy_state(pjsua_buddy_id buddy_id)
2371{
2372 pjsua_buddy_info info;
2373 pjsua_buddy_get_info(buddy_id, &info);
2374
2375 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2376 (int)info.uri.slen,
2377 info.uri.ptr,
2378 (int)info.status_text.slen,
2379 info.status_text.ptr));
2380}
2381
2382
2383/**
2384 * Incoming IM message (i.e. MESSAGE request)!
2385 */
2386static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2387 const pj_str_t *to, const pj_str_t *contact,
2388 const pj_str_t *mime_type, const pj_str_t *text)
2389{
2390 /* Note: call index may be -1 */
2391 PJ_UNUSED_ARG(call_id);
2392 PJ_UNUSED_ARG(to);
2393 PJ_UNUSED_ARG(contact);
2394 PJ_UNUSED_ARG(mime_type);
2395
Benny Prijonof4b538d2007-05-14 16:45:20 +00002396 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002397 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002398 (int)text->slen, text->ptr,
2399 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002400}
2401
2402
2403/**
2404 * Received typing indication
2405 */
2406static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2407 const pj_str_t *to, const pj_str_t *contact,
2408 pj_bool_t is_typing)
2409{
2410 PJ_UNUSED_ARG(call_id);
2411 PJ_UNUSED_ARG(to);
2412 PJ_UNUSED_ARG(contact);
2413
2414 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2415 (int)from->slen, from->ptr,
2416 (is_typing?"is typing..":"has stopped typing")));
2417}
2418
2419
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002420/**
2421 * Call transfer request status.
2422 */
2423static void on_call_transfer_status(pjsua_call_id call_id,
2424 int status_code,
2425 const pj_str_t *status_text,
2426 pj_bool_t final,
2427 pj_bool_t *p_cont)
2428{
2429 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2430 call_id, status_code,
2431 (int)status_text->slen, status_text->ptr,
2432 (final ? "[final]" : "")));
2433
2434 if (status_code/100 == 2) {
2435 PJ_LOG(3,(THIS_FILE,
2436 "Call %d: call transfered successfully, disconnecting call",
2437 call_id));
2438 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2439 *p_cont = PJ_FALSE;
2440 }
2441}
2442
2443
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002444/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002445 * Notification that call is being replaced.
2446 */
2447static void on_call_replaced(pjsua_call_id old_call_id,
2448 pjsua_call_id new_call_id)
2449{
2450 pjsua_call_info old_ci, new_ci;
2451
2452 pjsua_call_get_info(old_call_id, &old_ci);
2453 pjsua_call_get_info(new_call_id, &new_ci);
2454
2455 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2456 "call %d with %.*s",
2457 old_call_id,
2458 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2459 new_call_id,
2460 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2461}
2462
2463
2464/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002465 * NAT type detection callback.
2466 */
2467static void on_nat_detect(const pj_stun_nat_detect_result *res)
2468{
2469 if (res->status != PJ_SUCCESS) {
2470 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2471 } else {
2472 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2473 }
2474}
2475
2476
2477/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002478 * Print buddy list.
2479 */
2480static void print_buddy_list(void)
2481{
2482 pjsua_buddy_id ids[64];
2483 int i;
2484 unsigned count = PJ_ARRAY_SIZE(ids);
2485
2486 puts("Buddy list:");
2487
2488 pjsua_enum_buddies(ids, &count);
2489
2490 if (count == 0)
2491 puts(" -none-");
2492 else {
2493 for (i=0; i<(int)count; ++i) {
2494 pjsua_buddy_info info;
2495
2496 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2497 continue;
2498
Benny Prijono4461c7d2007-08-25 13:36:15 +00002499 printf(" [%2d] <%.*s> %.*s\n",
2500 ids[i]+1,
2501 (int)info.status_text.slen,
2502 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002503 (int)info.uri.slen,
2504 info.uri.ptr);
2505 }
2506 }
2507 puts("");
2508}
2509
2510
2511/*
2512 * Print account status.
2513 */
2514static void print_acc_status(int acc_id)
2515{
2516 char buf[80];
2517 pjsua_acc_info info;
2518
2519 pjsua_acc_get_info(acc_id, &info);
2520
2521 if (!info.has_registration) {
2522 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2523 (int)info.status_text.slen,
2524 info.status_text.ptr);
2525
2526 } else {
2527 pj_ansi_snprintf(buf, sizeof(buf),
2528 "%d/%.*s (expires=%d)",
2529 info.status,
2530 (int)info.status_text.slen,
2531 info.status_text.ptr,
2532 info.expires);
2533
2534 }
2535
2536 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2537 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002538 printf(" Online status: %.*s\n",
2539 (int)info.online_status_text.slen,
2540 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002541}
2542
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002543/* Playfile done notification, set timer to hangup calls */
2544pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2545{
2546 pj_time_val delay;
2547
2548 PJ_UNUSED_ARG(port);
2549 PJ_UNUSED_ARG(usr_data);
2550
2551 /* Just rewind WAV when it is played outside of call */
2552 if (pjsua_call_get_count() == 0) {
2553 pjsua_player_set_pos(app_config.wav_id, 0);
2554 return PJ_SUCCESS;
2555 }
2556
2557 /* Timer is already active */
2558 if (app_config.auto_hangup_timer.id == 1)
2559 return PJ_SUCCESS;
2560
2561 app_config.auto_hangup_timer.id = 1;
2562 delay.sec = 0;
2563 delay.msec = 200; /* Give 200 ms before hangup */
2564 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2565 &app_config.auto_hangup_timer,
2566 &delay);
2567
2568 return PJ_SUCCESS;
2569}
2570
2571/* Auto hangup timer callback */
2572static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2573 struct pj_timer_entry *entry)
2574{
2575 PJ_UNUSED_ARG(timer_heap);
2576 PJ_UNUSED_ARG(entry);
2577
2578 app_config.auto_hangup_timer.id = 0;
2579 pjsua_call_hangup_all();
2580}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002581
2582/*
2583 * Show a bit of help.
2584 */
2585static void keystroke_help(void)
2586{
2587 pjsua_acc_id acc_ids[16];
2588 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2589 int i;
2590
2591 printf(">>>>\n");
2592
2593 pjsua_enum_accs(acc_ids, &count);
2594
2595 printf("Account list:\n");
2596 for (i=0; i<(int)count; ++i)
2597 print_acc_status(acc_ids[i]);
2598
2599 print_buddy_list();
2600
2601 //puts("Commands:");
2602 puts("+=============================================================================+");
2603 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2604 puts("| | | |");
2605 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2606 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002607 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2608 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2609 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2610 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002611 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2612 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002613 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002614 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002615 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2616 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2617 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2618 puts("| | V Adjust audio Volume | f Save config |");
2619 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002620 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002621 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002622 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002623
2624 i = pjsua_call_get_count();
2625 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002626
2627 if (current_call != PJSUA_INVALID_ID) {
2628 pjsua_call_info ci;
2629 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2630 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2631 (int)ci.remote_info.slen, ci.remote_info.ptr,
2632 (int)ci.state_text.slen, ci.state_text.ptr);
2633 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002634}
2635
2636
2637/*
2638 * Input simple string
2639 */
2640static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2641{
2642 char *p;
2643
2644 printf("%s (empty to cancel): ", title); fflush(stdout);
2645 fgets(buf, len, stdin);
2646
2647 /* Remove trailing newlines. */
2648 for (p=buf; ; ++p) {
2649 if (*p=='\r' || *p=='\n') *p='\0';
2650 else if (!*p) break;
2651 }
2652
2653 if (!*buf)
2654 return PJ_FALSE;
2655
2656 return PJ_TRUE;
2657}
2658
2659
2660#define NO_NB -2
2661struct input_result
2662{
2663 int nb_result;
2664 char *uri_result;
2665};
2666
2667
2668/*
2669 * Input URL.
2670 */
2671static void ui_input_url(const char *title, char *buf, int len,
2672 struct input_result *result)
2673{
2674 result->nb_result = NO_NB;
2675 result->uri_result = NULL;
2676
2677 print_buddy_list();
2678
2679 printf("Choices:\n"
2680 " 0 For current dialog.\n"
2681 " -1 All %d buddies in buddy list\n"
2682 " [1 -%2d] Select from buddy list\n"
2683 " URL An URL\n"
2684 " <Enter> Empty input (or 'q') to cancel\n"
2685 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2686 printf("%s: ", title);
2687
2688 fflush(stdout);
2689 fgets(buf, len, stdin);
2690 len = strlen(buf);
2691
2692 /* Left trim */
2693 while (pj_isspace(*buf)) {
2694 ++buf;
2695 --len;
2696 }
2697
2698 /* Remove trailing newlines */
2699 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2700 buf[--len] = '\0';
2701
2702 if (len == 0 || buf[0]=='q')
2703 return;
2704
2705 if (pj_isdigit(*buf) || *buf=='-') {
2706
2707 int i;
2708
2709 if (*buf=='-')
2710 i = 1;
2711 else
2712 i = 0;
2713
2714 for (; i<len; ++i) {
2715 if (!pj_isdigit(buf[i])) {
2716 puts("Invalid input");
2717 return;
2718 }
2719 }
2720
2721 result->nb_result = my_atoi(buf);
2722
2723 if (result->nb_result >= 0 &&
2724 result->nb_result <= (int)pjsua_get_buddy_count())
2725 {
2726 return;
2727 }
2728 if (result->nb_result == -1)
2729 return;
2730
2731 puts("Invalid input");
2732 result->nb_result = NO_NB;
2733 return;
2734
2735 } else {
2736 pj_status_t status;
2737
2738 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2739 pjsua_perror(THIS_FILE, "Invalid URL", status);
2740 return;
2741 }
2742
2743 result->uri_result = buf;
2744 }
2745}
2746
2747/*
2748 * List the ports in conference bridge
2749 */
2750static void conf_list(void)
2751{
2752 unsigned i, count;
2753 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2754
2755 printf("Conference ports:\n");
2756
2757 count = PJ_ARRAY_SIZE(id);
2758 pjsua_enum_conf_ports(id, &count);
2759
2760 for (i=0; i<count; ++i) {
2761 char txlist[PJSUA_MAX_CALLS*4+10];
2762 unsigned j;
2763 pjsua_conf_port_info info;
2764
2765 pjsua_conf_get_port_info(id[i], &info);
2766
2767 txlist[0] = '\0';
2768 for (j=0; j<info.listener_cnt; ++j) {
2769 char s[10];
2770 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2771 pj_ansi_strcat(txlist, s);
2772 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002773 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002774 info.slot_id,
2775 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002776 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002777 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002778 (int)info.name.slen,
2779 info.name.ptr,
2780 txlist);
2781
2782 }
2783 puts("");
2784}
2785
2786
2787/*
Benny Prijono56315612006-07-18 14:39:40 +00002788 * Send arbitrary request to remote host
2789 */
2790static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2791{
2792 pj_str_t str_method;
2793 pjsip_method method;
2794 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002795 pjsip_endpoint *endpt;
2796 pj_status_t status;
2797
2798 endpt = pjsua_get_pjsip_endpt();
2799
2800 str_method = pj_str(cstr_method);
2801 pjsip_method_init_np(&method, &str_method);
2802
Benny Prijonofff245c2007-04-02 11:44:47 +00002803 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002804
Benny Prijonob988d762007-12-05 04:30:04 +00002805 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002806 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002807 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002808 return;
2809 }
2810}
2811
2812
2813/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002814 * Change extended online status.
2815 */
2816static void change_online_status(void)
2817{
2818 char menuin[32];
2819 pj_bool_t online_status;
2820 pjrpid_element elem;
2821 int i, choice;
2822
2823 enum {
2824 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2825 };
2826
2827 struct opt {
2828 int id;
2829 char *name;
2830 } opts[] = {
2831 { AVAILABLE, "Available" },
2832 { BUSY, "Busy"},
2833 { OTP, "On the phone"},
2834 { IDLE, "Idle"},
2835 { AWAY, "Away"},
2836 { BRB, "Be right back"},
2837 { OFFLINE, "Offline"}
2838 };
2839
2840 printf("\n"
2841 "Choices:\n");
2842 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2843 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2844 }
2845
2846 if (!simple_input("Select status", menuin, sizeof(menuin)))
2847 return;
2848
2849 choice = atoi(menuin) - 1;
2850 if (choice < 0 || choice >= OPT_MAX) {
2851 puts("Invalid selection");
2852 return;
2853 }
2854
2855 pj_bzero(&elem, sizeof(elem));
2856 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2857
2858 online_status = PJ_TRUE;
2859
2860 switch (choice) {
2861 case AVAILABLE:
2862 break;
2863 case BUSY:
2864 elem.activity = PJRPID_ACTIVITY_BUSY;
2865 elem.note = pj_str("Busy");
2866 break;
2867 case OTP:
2868 elem.activity = PJRPID_ACTIVITY_BUSY;
2869 elem.note = pj_str("On the phone");
2870 break;
2871 case IDLE:
2872 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2873 elem.note = pj_str("Idle");
2874 break;
2875 case AWAY:
2876 elem.activity = PJRPID_ACTIVITY_AWAY;
2877 elem.note = pj_str("Away");
2878 break;
2879 case BRB:
2880 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2881 elem.note = pj_str("Be right back");
2882 break;
2883 case OFFLINE:
2884 online_status = PJ_FALSE;
2885 break;
2886 }
2887
2888 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2889}
2890
2891
2892/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002893 * Change codec priorities.
2894 */
2895static void manage_codec_prio(void)
2896{
2897 pjsua_codec_info c[32];
2898 unsigned i, count = PJ_ARRAY_SIZE(c);
2899 char input[32];
2900 char *codec, *prio;
2901 pj_str_t id;
2902 int new_prio;
2903 pj_status_t status;
2904
2905 printf("List of codecs:\n");
2906
2907 pjsua_enum_codecs(c, &count);
2908 for (i=0; i<count; ++i) {
2909 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2910 c[i].codec_id.ptr);
2911 }
2912
2913 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00002914 puts("Enter codec id and its new priority "
2915 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002916
Benny Prijono88accae2008-06-26 15:48:14 +00002917 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002918 fgets(input, sizeof(input), stdin);
2919 if (input[0]=='\r' || input[0]=='\n') {
2920 puts("Done");
2921 return;
2922 }
2923
2924 codec = strtok(input, " \t\r\n");
2925 prio = strtok(NULL, " \r\n");
2926
2927 if (!codec || !prio) {
2928 puts("Invalid input");
2929 return;
2930 }
2931
2932 new_prio = atoi(prio);
2933 if (new_prio < 0)
2934 new_prio = 0;
2935 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2936 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2937
2938 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2939 (pj_uint8_t)new_prio);
2940 if (status != PJ_SUCCESS)
2941 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2942}
2943
2944
2945/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002946 * Main "user interface" loop.
2947 */
2948void console_app_main(const pj_str_t *uri_to_call)
2949{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002950 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002951 char buf[128];
2952 char text[128];
2953 int i, count;
2954 char *uri;
2955 pj_str_t tmp;
2956 struct input_result result;
2957 pjsua_call_info call_info;
2958 pjsua_acc_info acc_info;
2959
2960
2961 /* If user specifies URI to call, then call the URI */
2962 if (uri_to_call->slen) {
2963 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2964 }
2965
2966 keystroke_help();
2967
2968 for (;;) {
2969
2970 printf(">>> ");
2971 fflush(stdout);
2972
Benny Prijono990042e2007-01-21 19:36:00 +00002973 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2974 /*
2975 * Be friendly to users who redirect commands into
2976 * program, when file ends, resume with kbd.
2977 * If exit is desired end script with q for quit
2978 */
2979 /* Reopen stdin/stdout/stderr to /dev/console */
2980#if defined(PJ_WIN32) && PJ_WIN32!=0
2981 if (freopen ("CONIN$", "r", stdin) == NULL) {
2982#else
2983 if (1) {
2984#endif
2985 puts("Cannot switch back to console from file redirection");
2986 menuin[0] = 'q';
2987 menuin[1] = '\0';
2988 } else {
2989 puts("Switched back to console from file redirection");
2990 continue;
2991 }
2992 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002993
Benny Prijonoebc32c32008-06-12 13:30:39 +00002994 if (cmd_echo) {
2995 printf("%s", menuin);
2996 }
2997
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002998 switch (menuin[0]) {
2999
3000 case 'm':
3001 /* Make call! : */
3002 printf("(You currently have %d calls)\n",
3003 pjsua_call_get_count());
3004
3005 uri = NULL;
3006 ui_input_url("Make call", buf, sizeof(buf), &result);
3007 if (result.nb_result != NO_NB) {
3008
3009 if (result.nb_result == -1 || result.nb_result == 0) {
3010 puts("You can't do that with make call!");
3011 continue;
3012 } else {
3013 pjsua_buddy_info binfo;
3014 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3015 uri = binfo.uri.ptr;
3016 }
3017
3018 } else if (result.uri_result) {
3019 uri = result.uri_result;
3020 }
3021
3022 tmp = pj_str(uri);
3023 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
3024 break;
3025
3026 case 'M':
3027 /* Make multiple calls! : */
3028 printf("(You currently have %d calls)\n",
3029 pjsua_call_get_count());
3030
3031 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
3032 continue;
3033
3034 count = my_atoi(menuin);
3035 if (count < 1)
3036 continue;
3037
3038 ui_input_url("Make call", buf, sizeof(buf), &result);
3039 if (result.nb_result != NO_NB) {
3040 pjsua_buddy_info binfo;
3041 if (result.nb_result == -1 || result.nb_result == 0) {
3042 puts("You can't do that with make call!");
3043 continue;
3044 }
3045 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3046 uri = binfo.uri.ptr;
3047 } else {
3048 uri = result.uri_result;
3049 }
3050
3051 for (i=0; i<my_atoi(menuin); ++i) {
3052 pj_status_t status;
3053
3054 tmp = pj_str(uri);
3055 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3056 NULL, NULL);
3057 if (status != PJ_SUCCESS)
3058 break;
3059 }
3060 break;
3061
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003062 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003063 i = pjsua_detect_nat_type();
3064 if (i != PJ_SUCCESS)
3065 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003066 break;
3067
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003068 case 'i':
3069 /* Send instant messaeg */
3070
3071 /* i is for call index to send message, if any */
3072 i = -1;
3073
3074 /* Make compiler happy. */
3075 uri = NULL;
3076
3077 /* Input destination. */
3078 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3079 if (result.nb_result != NO_NB) {
3080
3081 if (result.nb_result == -1) {
3082 puts("You can't send broadcast IM like that!");
3083 continue;
3084
3085 } else if (result.nb_result == 0) {
3086
3087 i = current_call;
3088
3089 } else {
3090 pjsua_buddy_info binfo;
3091 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3092 uri = binfo.uri.ptr;
3093 }
3094
3095 } else if (result.uri_result) {
3096 uri = result.uri_result;
3097 }
3098
3099
3100 /* Send typing indication. */
3101 if (i != -1)
3102 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3103 else {
3104 pj_str_t tmp_uri = pj_str(uri);
3105 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3106 }
3107
3108 /* Input the IM . */
3109 if (!simple_input("Message", text, sizeof(text))) {
3110 /*
3111 * Cancelled.
3112 * Send typing notification too, saying we're not typing.
3113 */
3114 if (i != -1)
3115 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3116 else {
3117 pj_str_t tmp_uri = pj_str(uri);
3118 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3119 }
3120 continue;
3121 }
3122
3123 tmp = pj_str(text);
3124
3125 /* Send the IM */
3126 if (i != -1)
3127 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3128 else {
3129 pj_str_t tmp_uri = pj_str(uri);
3130 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3131 }
3132
3133 break;
3134
3135 case 'a':
3136
3137 if (current_call != -1) {
3138 pjsua_call_get_info(current_call, &call_info);
3139 } else {
3140 /* Make compiler happy */
3141 call_info.role = PJSIP_ROLE_UAC;
3142 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3143 }
3144
3145 if (current_call == -1 ||
3146 call_info.role != PJSIP_ROLE_UAS ||
3147 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3148 {
3149 puts("No pending incoming call");
3150 fflush(stdout);
3151 continue;
3152
3153 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003154 int st_code;
3155 char contact[120];
3156 pj_str_t hname = { "Contact", 7 };
3157 pj_str_t hvalue;
3158 pjsip_generic_string_hdr hcontact;
3159 pjsua_msg_data msg_data;
3160
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003161 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3162 continue;
3163
Benny Prijono20d36722007-02-22 14:52:24 +00003164 st_code = my_atoi(buf);
3165 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003166 continue;
3167
Benny Prijono20d36722007-02-22 14:52:24 +00003168 pjsua_msg_data_init(&msg_data);
3169
3170 if (st_code/100 == 3) {
3171 if (!simple_input("Enter URL to be put in Contact",
3172 contact, sizeof(contact)))
3173 continue;
3174 hvalue = pj_str(contact);
3175 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3176
3177 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3178 }
3179
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003180 /*
3181 * Must check again!
3182 * Call may have been disconnected while we're waiting for
3183 * keyboard input.
3184 */
3185 if (current_call == -1) {
3186 puts("Call has been disconnected");
3187 fflush(stdout);
3188 continue;
3189 }
3190
Benny Prijono20d36722007-02-22 14:52:24 +00003191 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003192 }
3193
3194 break;
3195
3196
3197 case 'h':
3198
3199 if (current_call == -1) {
3200 puts("No current call");
3201 fflush(stdout);
3202 continue;
3203
3204 } else if (menuin[1] == 'a') {
3205
3206 /* Hangup all calls */
3207 pjsua_call_hangup_all();
3208
3209 } else {
3210
3211 /* Hangup current calls */
3212 pjsua_call_hangup(current_call, 0, NULL, NULL);
3213 }
3214 break;
3215
3216 case ']':
3217 case '[':
3218 /*
3219 * Cycle next/prev dialog.
3220 */
3221 if (menuin[0] == ']') {
3222 find_next_call();
3223
3224 } else {
3225 find_prev_call();
3226 }
3227
3228 if (current_call != -1) {
3229
3230 pjsua_call_get_info(current_call, &call_info);
3231 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3232 (int)call_info.remote_info.slen,
3233 call_info.remote_info.ptr));
3234
3235 } else {
3236 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3237 }
3238 break;
3239
3240
3241 case '>':
3242 case '<':
3243 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3244 break;
3245
3246 i = my_atoi(buf);
3247 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003248 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003249 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3250 } else {
3251 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3252 }
3253 break;
3254
3255
3256 case '+':
3257 if (menuin[1] == 'b') {
3258
3259 pjsua_buddy_config buddy_cfg;
3260 pjsua_buddy_id buddy_id;
3261 pj_status_t status;
3262
3263 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3264 break;
3265
3266 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3267 printf("Invalid SIP URI '%s'\n", buf);
3268 break;
3269 }
3270
Benny Prijonoac623b32006-07-03 15:19:31 +00003271 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003272
3273 buddy_cfg.uri = pj_str(buf);
3274 buddy_cfg.subscribe = PJ_TRUE;
3275
3276 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3277 if (status == PJ_SUCCESS) {
3278 printf("New buddy '%s' added at index %d\n",
3279 buf, buddy_id+1);
3280 }
3281
3282 } else if (menuin[1] == 'a') {
3283
Benny Prijonofb2b3652007-06-28 07:15:03 +00003284 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3285 pjsua_acc_config acc_cfg;
3286 pj_status_t status;
3287
3288 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3289 break;
3290 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3291 break;
3292 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3293 break;
3294 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3295 break;
3296 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3297 break;
3298
3299 pjsua_acc_config_default(&acc_cfg);
3300 acc_cfg.id = pj_str(id);
3301 acc_cfg.reg_uri = pj_str(registrar);
3302 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003303 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003304 acc_cfg.cred_info[0].realm = pj_str(realm);
3305 acc_cfg.cred_info[0].username = pj_str(uname);
3306 acc_cfg.cred_info[0].data_type = 0;
3307 acc_cfg.cred_info[0].data = pj_str(passwd);
3308
3309 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3310 if (status != PJ_SUCCESS) {
3311 pjsua_perror(THIS_FILE, "Error adding new account", status);
3312 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003313
3314 } else {
3315 printf("Invalid input %s\n", menuin);
3316 }
3317 break;
3318
3319 case '-':
3320 if (menuin[1] == 'b') {
3321 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3322 break;
3323
3324 i = my_atoi(buf) - 1;
3325
3326 if (!pjsua_buddy_is_valid(i)) {
3327 printf("Invalid buddy id %d\n", i);
3328 } else {
3329 pjsua_buddy_del(i);
3330 printf("Buddy %d deleted\n", i);
3331 }
3332
3333 } else if (menuin[1] == 'a') {
3334
3335 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3336 break;
3337
3338 i = my_atoi(buf);
3339
3340 if (!pjsua_acc_is_valid(i)) {
3341 printf("Invalid account id %d\n", i);
3342 } else {
3343 pjsua_acc_del(i);
3344 printf("Account %d deleted\n", i);
3345 }
3346
3347 } else {
3348 printf("Invalid input %s\n", menuin);
3349 }
3350 break;
3351
3352 case 'H':
3353 /*
3354 * Hold call.
3355 */
3356 if (current_call != -1) {
3357
3358 pjsua_call_set_hold(current_call, NULL);
3359
3360 } else {
3361 PJ_LOG(3,(THIS_FILE, "No current call"));
3362 }
3363 break;
3364
3365 case 'v':
3366 /*
3367 * Send re-INVITE (to release hold, etc).
3368 */
3369 if (current_call != -1) {
3370
3371 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3372
3373 } else {
3374 PJ_LOG(3,(THIS_FILE, "No current call"));
3375 }
3376 break;
3377
Benny Prijonoc08682e2007-10-04 06:17:58 +00003378 case 'U':
3379 /*
3380 * Send UPDATE
3381 */
3382 if (current_call != -1) {
3383
3384 pjsua_call_update(current_call, 0, NULL);
3385
3386 } else {
3387 PJ_LOG(3,(THIS_FILE, "No current call"));
3388 }
3389 break;
3390
3391 case 'C':
3392 if (menuin[1] == 'p') {
3393 manage_codec_prio();
3394 }
3395 break;
3396
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003397 case 'x':
3398 /*
3399 * Transfer call.
3400 */
3401 if (current_call == -1) {
3402
3403 PJ_LOG(3,(THIS_FILE, "No current call"));
3404
3405 } else {
3406 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003407 pjsua_msg_data msg_data;
3408 pjsip_generic_string_hdr refer_sub;
3409 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3410 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003411 pjsua_call_info ci;
3412
3413 pjsua_call_get_info(current_call, &ci);
3414 printf("Transfering current call [%d] %.*s\n",
3415 current_call,
3416 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003417
3418 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3419
3420 /* Check if call is still there. */
3421
3422 if (call != current_call) {
3423 puts("Call has been disconnected");
3424 continue;
3425 }
3426
Benny Prijonod524e822006-09-22 12:48:18 +00003427 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003428 if (app_config.no_refersub) {
3429 /* Add Refer-Sub: false in outgoing REFER request */
3430 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3431 &STR_FALSE);
3432 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3433 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003434 if (result.nb_result != NO_NB) {
3435 if (result.nb_result == -1 || result.nb_result == 0)
3436 puts("You can't do that with transfer call!");
3437 else {
3438 pjsua_buddy_info binfo;
3439 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003440 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003441 }
3442
3443 } else if (result.uri_result) {
3444 pj_str_t tmp;
3445 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003446 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003447 }
3448 }
3449 break;
3450
Benny Prijonof7b1c392006-11-11 16:46:34 +00003451 case 'X':
3452 /*
3453 * Transfer call with replaces.
3454 */
3455 if (current_call == -1) {
3456
3457 PJ_LOG(3,(THIS_FILE, "No current call"));
3458
3459 } else {
3460 int call = current_call;
3461 int dst_call;
3462 pjsua_msg_data msg_data;
3463 pjsip_generic_string_hdr refer_sub;
3464 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3465 pj_str_t STR_FALSE = { "false", 5 };
3466 pjsua_call_id ids[PJSUA_MAX_CALLS];
3467 pjsua_call_info ci;
3468 unsigned i, count;
3469
3470 count = PJ_ARRAY_SIZE(ids);
3471 pjsua_enum_calls(ids, &count);
3472
3473 if (count <= 1) {
3474 puts("There are no other calls");
3475 continue;
3476 }
3477
3478 pjsua_call_get_info(current_call, &ci);
3479 printf("Transfer call [%d] %.*s to one of the following:\n",
3480 current_call,
3481 (int)ci.remote_info.slen, ci.remote_info.ptr);
3482
3483 for (i=0; i<count; ++i) {
3484 pjsua_call_info call_info;
3485
3486 if (ids[i] == call)
3487 continue;
3488
3489 pjsua_call_get_info(ids[i], &call_info);
3490 printf("%d %.*s [%.*s]\n",
3491 ids[i],
3492 (int)call_info.remote_info.slen,
3493 call_info.remote_info.ptr,
3494 (int)call_info.state_text.slen,
3495 call_info.state_text.ptr);
3496 }
3497
3498 if (!simple_input("Enter call number to be replaced",
3499 buf, sizeof(buf)))
3500 continue;
3501
3502 dst_call = my_atoi(buf);
3503
3504 /* Check if call is still there. */
3505
3506 if (call != current_call) {
3507 puts("Call has been disconnected");
3508 continue;
3509 }
3510
3511 /* Check that destination call is valid. */
3512 if (dst_call == call) {
3513 puts("Destination call number must not be the same "
3514 "as the call being transfered");
3515 continue;
3516 }
3517 if (dst_call >= PJSUA_MAX_CALLS) {
3518 puts("Invalid destination call number");
3519 continue;
3520 }
3521 if (!pjsua_call_is_active(dst_call)) {
3522 puts("Invalid destination call number");
3523 continue;
3524 }
3525
3526 pjsua_msg_data_init(&msg_data);
3527 if (app_config.no_refersub) {
3528 /* Add Refer-Sub: false in outgoing REFER request */
3529 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3530 &STR_FALSE);
3531 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3532 }
3533
3534 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3535 }
3536 break;
3537
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003538 case '#':
3539 /*
3540 * Send DTMF strings.
3541 */
3542 if (current_call == -1) {
3543
3544 PJ_LOG(3,(THIS_FILE, "No current call"));
3545
3546 } else if (!pjsua_call_has_media(current_call)) {
3547
3548 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3549
3550 } else {
3551 pj_str_t digits;
3552 int call = current_call;
3553 pj_status_t status;
3554
3555 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3556 sizeof(buf)))
3557 {
3558 break;
3559 }
3560
3561 if (call != current_call) {
3562 puts("Call has been disconnected");
3563 continue;
3564 }
3565
3566 digits = pj_str(buf);
3567 status = pjsua_call_dial_dtmf(current_call, &digits);
3568 if (status != PJ_SUCCESS) {
3569 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3570 } else {
3571 puts("DTMF digits enqueued for transmission");
3572 }
3573 }
3574 break;
3575
Benny Prijonofeb69f42007-10-05 09:12:26 +00003576 case '*':
3577 /* Send DTMF with INFO */
3578 if (current_call == -1) {
3579
3580 PJ_LOG(3,(THIS_FILE, "No current call"));
3581
3582 } else {
3583 const pj_str_t SIP_INFO = pj_str("INFO");
3584 pj_str_t digits;
3585 int call = current_call;
3586 int i;
3587 pj_status_t status;
3588
3589 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3590 sizeof(buf)))
3591 {
3592 break;
3593 }
3594
3595 if (call != current_call) {
3596 puts("Call has been disconnected");
3597 continue;
3598 }
3599
3600 digits = pj_str(buf);
3601 for (i=0; i<digits.slen; ++i) {
3602 pjsua_msg_data msg_data;
3603 char body[80];
3604
3605 pjsua_msg_data_init(&msg_data);
3606 msg_data.content_type = pj_str("application/dtmf-relay");
3607
3608 pj_ansi_snprintf(body, sizeof(body),
3609 "Signal=%c\r\n"
3610 "Duration=160",
3611 buf[i]);
3612 msg_data.msg_body = pj_str(body);
3613
3614 status = pjsua_call_send_request(current_call, &SIP_INFO,
3615 &msg_data);
3616 if (status != PJ_SUCCESS) {
3617 break;
3618 }
3619 }
3620 }
3621 break;
3622
Benny Prijono56315612006-07-18 14:39:40 +00003623 case 'S':
3624 /*
3625 * Send arbitrary request
3626 */
3627 if (pjsua_acc_get_count() == 0) {
3628 puts("Sorry, need at least one account configured");
3629 break;
3630 }
3631
3632 puts("Send arbitrary request to remote host");
3633
3634 /* Input METHOD */
3635 if (!simple_input("Request method:",text,sizeof(text)))
3636 break;
3637
3638 /* Input destination URI */
3639 uri = NULL;
3640 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3641 if (result.nb_result != NO_NB) {
3642
3643 if (result.nb_result == -1 || result.nb_result == 0) {
3644 puts("Sorry you can't do that!");
3645 continue;
3646 } else {
3647 pjsua_buddy_info binfo;
3648 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3649 uri = binfo.uri.ptr;
3650 }
3651
3652 } else if (result.uri_result) {
3653 uri = result.uri_result;
3654 }
3655
3656 tmp = pj_str(uri);
3657
3658 send_request(text, &tmp);
3659 break;
3660
Benny Prijonoebc32c32008-06-12 13:30:39 +00003661 case 'e':
3662 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3663 pj_str_t tmp;
3664
3665 tmp.ptr = menuin+5;
3666 tmp.slen = pj_ansi_strlen(menuin)-6;
3667
3668 if (tmp.slen < 1) {
3669 puts("Usage: echo [0|1]");
3670 break;
3671 }
3672
Benny Prijonoddd02de2008-06-26 22:20:11 +00003673 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003674 }
3675 break;
3676
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003677 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003678 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3679 pj_str_t tmp;
3680 int delay;
3681
3682 tmp.ptr = menuin+6;
3683 tmp.slen = pj_ansi_strlen(menuin)-7;
3684
3685 if (tmp.slen < 1) {
3686 puts("Usage: sleep MSEC");
3687 break;
3688 }
3689
3690 delay = pj_strtoul(&tmp);
3691 if (delay < 0) delay = 0;
3692 pj_thread_sleep(delay);
3693 break;
3694 }
3695 /* Continue below */
3696
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003697 case 'u':
3698 /*
3699 * Subscribe/unsubscribe presence.
3700 */
3701 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3702 if (result.nb_result != NO_NB) {
3703 if (result.nb_result == -1) {
3704 int i, count;
3705 count = pjsua_get_buddy_count();
3706 for (i=0; i<count; ++i)
3707 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3708 } else if (result.nb_result == 0) {
3709 puts("Sorry, can only subscribe to buddy's presence, "
3710 "not from existing call");
3711 } else {
3712 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3713 }
3714
3715 } else if (result.uri_result) {
3716 puts("Sorry, can only subscribe to buddy's presence, "
3717 "not arbitrary URL (for now)");
3718 }
3719
3720 break;
3721
3722 case 'r':
3723 switch (menuin[1]) {
3724 case 'r':
3725 /*
3726 * Re-Register.
3727 */
3728 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3729 break;
3730 case 'u':
3731 /*
3732 * Unregister
3733 */
3734 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3735 break;
3736 }
3737 break;
3738
3739 case 't':
3740 pjsua_acc_get_info(current_acc, &acc_info);
3741 acc_info.online_status = !acc_info.online_status;
3742 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3743 printf("Setting %s online status to %s\n",
3744 acc_info.acc_uri.ptr,
3745 (acc_info.online_status?"online":"offline"));
3746 break;
3747
Benny Prijono4461c7d2007-08-25 13:36:15 +00003748 case 'T':
3749 change_online_status();
3750 break;
3751
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003752 case 'c':
3753 switch (menuin[1]) {
3754 case 'l':
3755 conf_list();
3756 break;
3757 case 'c':
3758 case 'd':
3759 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003760 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003761 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003762 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003763 const char *src_title, *dst_title;
3764
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003765 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003766
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003767 if (cnt != 3) {
3768 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003769
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003770 src_title = (menuin[1]=='c'?
3771 "Connect src port #":
3772 "Disconnect src port #");
3773 dst_title = (menuin[1]=='c'?
3774 "To dst port #":
3775 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003776
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003777 if (!simple_input(src_title, src_port, sizeof(src_port)))
3778 break;
3779
3780 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3781 break;
3782 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003783
3784 if (menuin[1]=='c') {
3785 status = pjsua_conf_connect(my_atoi(src_port),
3786 my_atoi(dst_port));
3787 } else {
3788 status = pjsua_conf_disconnect(my_atoi(src_port),
3789 my_atoi(dst_port));
3790 }
3791 if (status == PJ_SUCCESS) {
3792 puts("Success");
3793 } else {
3794 puts("ERROR!!");
3795 }
3796 }
3797 break;
3798 }
3799 break;
3800
Benny Prijono6dd967c2006-12-26 02:27:14 +00003801 case 'V':
3802 /* Adjust audio volume */
3803 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3804 if (simple_input(buf,text,sizeof(text))) {
3805 char *err;
3806 app_config.mic_level = (float)strtod(text, &err);
3807 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3808 }
3809 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3810 app_config.speaker_level);
3811 if (simple_input(buf,text,sizeof(text))) {
3812 char *err;
3813 app_config.speaker_level = (float)strtod(text, &err);
3814 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3815 }
3816
3817 break;
3818
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003819 case 'd':
3820 if (menuin[1] == 'c') {
3821 char settings[2000];
3822 int len;
3823
3824 len = write_settings(&app_config, settings, sizeof(settings));
3825 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003826 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003827 else
3828 PJ_LOG(3,(THIS_FILE,
3829 "Dumping configuration (%d bytes):\n%s\n",
3830 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003831
3832 } else if (menuin[1] == 'q') {
3833
3834 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003835 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003836 } else {
3837 PJ_LOG(3,(THIS_FILE, "No current call"));
3838 }
3839
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003840 } else {
3841 app_dump(menuin[1]=='d');
3842 }
3843 break;
3844
3845
3846 case 'f':
3847 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3848 char settings[2000];
3849 int len;
3850
3851 len = write_settings(&app_config, settings, sizeof(settings));
3852 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003853 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003854 else {
3855 pj_oshandle_t fd;
3856 pj_status_t status;
3857
3858 status = pj_file_open(app_config.pool, buf,
3859 PJ_O_WRONLY, &fd);
3860 if (status != PJ_SUCCESS) {
3861 pjsua_perror(THIS_FILE, "Unable to open file", status);
3862 } else {
3863 pj_ssize_t size = len;
3864 pj_file_write(fd, settings, &size);
3865 pj_file_close(fd);
3866
3867 printf("Settings successfully written to '%s'\n", buf);
3868 }
3869 }
3870
3871 }
3872 break;
3873
3874
3875 case 'q':
3876 goto on_exit;
3877
3878
3879 default:
3880 if (menuin[0] != '\n' && menuin[0] != '\r') {
3881 printf("Invalid input %s", menuin);
3882 }
3883 keystroke_help();
3884 break;
3885 }
3886 }
3887
3888on_exit:
3889 ;
3890}
3891
3892
3893/*****************************************************************************
3894 * Public API
3895 */
3896
3897pj_status_t app_init(int argc, char *argv[])
3898{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003899 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003900 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003901 unsigned i;
3902 pj_status_t status;
3903
3904 /* Create pjsua */
3905 status = pjsua_create();
3906 if (status != PJ_SUCCESS)
3907 return status;
3908
3909 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00003910 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003911
3912 /* Initialize default config */
3913 default_config(&app_config);
3914
3915 /* Parse the arguments */
3916 status = parse_args(argc, argv, &app_config, &uri_arg);
3917 if (status != PJ_SUCCESS)
3918 return status;
3919
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003920 /* Initialize application callbacks */
3921 app_config.cfg.cb.on_call_state = &on_call_state;
3922 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3923 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003924 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003925 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003926 app_config.cfg.cb.on_reg_state = &on_reg_state;
3927 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3928 app_config.cfg.cb.on_pager = &on_pager;
3929 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003930 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003931 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003932 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003933
3934 /* Initialize pjsua */
3935 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3936 &app_config.media_cfg);
3937 if (status != PJ_SUCCESS)
3938 return status;
3939
Benny Prijonoe909eac2006-07-27 22:04:56 +00003940#ifdef STEREO_DEMO
3941 stereo_demo();
3942#endif
3943
Benny Prijono804ff0a2006-09-14 11:17:48 +00003944 /* Initialize calls data */
3945 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3946 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3947 app_config.call_data[i].timer.cb = &call_timeout_callback;
3948 }
3949
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003950 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003951 for (i=0; i<app_config.wav_count; ++i) {
3952 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003953 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00003954
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003955 if (app_config.auto_play_hangup)
3956 play_options |= PJMEDIA_FILE_NO_LOOP;
3957
3958 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00003959 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003960 if (status != PJ_SUCCESS)
3961 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003962
Benny Prijono72c04d22007-02-17 22:20:58 +00003963 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003964 app_config.wav_id = wav_id;
3965 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00003966 if (app_config.auto_play_hangup) {
3967 pjmedia_port *port;
3968
3969 pjsua_player_get_port(app_config.wav_id, &port);
3970 status = pjmedia_wav_player_set_eof_cb(port, NULL,
3971 &on_playfile_done);
3972 if (status != PJ_SUCCESS)
3973 goto on_error;
3974
3975 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
3976 &hangup_timeout_callback);
3977 }
Benny Prijono32e4f492007-01-24 00:44:26 +00003978 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003979 }
3980
Benny Prijono4af234b2007-01-24 02:02:09 +00003981 /* Optionally registers tone players */
3982 for (i=0; i<app_config.tone_count; ++i) {
3983 pjmedia_port *tport;
3984 char name[80];
3985 pj_str_t label;
3986 pj_status_t status;
3987
3988 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3989 app_config.tones[i].freq1,
3990 app_config.tones[i].freq2);
3991 label = pj_str(name);
3992 status = pjmedia_tonegen_create2(app_config.pool, &label,
3993 8000, 1, 160, 16,
3994 PJMEDIA_TONEGEN_LOOP, &tport);
3995 if (status != PJ_SUCCESS) {
3996 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3997 goto on_error;
3998 }
3999
4000 status = pjsua_conf_add_port(app_config.pool, tport,
4001 &app_config.tone_slots[i]);
4002 pj_assert(status == PJ_SUCCESS);
4003
4004 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
4005 pj_assert(status == PJ_SUCCESS);
4006 }
4007
Benny Prijono1ebd6142006-10-19 15:48:02 +00004008 /* Optionally create recorder file, if any. */
4009 if (app_config.rec_file.slen) {
4010 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
4011 &app_config.rec_id);
4012 if (status != PJ_SUCCESS)
4013 goto on_error;
4014
4015 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
4016 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004017
Benny Prijonoe347cb02007-02-14 14:36:13 +00004018 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
4019
Benny Prijono91d20f42008-06-14 19:42:37 +00004020 /* Create ringback tones */
4021 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00004022 unsigned i, samples_per_frame;
4023 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00004024 pj_str_t name;
4025
Benny Prijono4f966892008-06-14 22:43:56 +00004026 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
4027 app_config.media_cfg.clock_rate *
4028 app_config.media_cfg.channel_count / 1000;
4029
Benny Prijono91d20f42008-06-14 19:42:37 +00004030 /* Ringback tone (call is ringing) */
4031 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00004032 status = pjmedia_tonegen_create2(app_config.pool, &name,
4033 app_config.media_cfg.clock_rate,
4034 app_config.media_cfg.channel_count,
4035 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004036 16, PJMEDIA_TONEGEN_LOOP,
4037 &app_config.ringback_port);
4038 if (status != PJ_SUCCESS)
4039 goto on_error;
4040
Benny Prijonoe28cec82008-06-14 20:40:53 +00004041 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004042 for (i=0; i<RINGBACK_CNT; ++i) {
4043 tone[i].freq1 = RINGBACK_FREQ1;
4044 tone[i].freq2 = RINGBACK_FREQ2;
4045 tone[i].on_msec = RINGBACK_ON;
4046 tone[i].off_msec = RINGBACK_OFF;
4047 }
4048 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004049
Benny Prijono4f966892008-06-14 22:43:56 +00004050 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004051 PJMEDIA_TONEGEN_LOOP);
4052
4053
Benny Prijono91d20f42008-06-14 19:42:37 +00004054 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4055 &app_config.ringback_slot);
4056 if (status != PJ_SUCCESS)
4057 goto on_error;
4058
4059 /* Ring (to alert incoming call) */
4060 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004061 status = pjmedia_tonegen_create2(app_config.pool, &name,
4062 app_config.media_cfg.clock_rate,
4063 app_config.media_cfg.channel_count,
4064 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004065 16, PJMEDIA_TONEGEN_LOOP,
4066 &app_config.ring_port);
4067 if (status != PJ_SUCCESS)
4068 goto on_error;
4069
Benny Prijono4f966892008-06-14 22:43:56 +00004070 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004071 tone[i].freq1 = RING_FREQ1;
4072 tone[i].freq2 = RING_FREQ2;
4073 tone[i].on_msec = RING_ON;
4074 tone[i].off_msec = RING_OFF;
4075 }
Benny Prijono4f966892008-06-14 22:43:56 +00004076 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004077
Benny Prijono4f966892008-06-14 22:43:56 +00004078 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004079 tone, PJMEDIA_TONEGEN_LOOP);
4080
Benny Prijono91d20f42008-06-14 19:42:37 +00004081 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4082 &app_config.ring_slot);
4083 if (status != PJ_SUCCESS)
4084 goto on_error;
4085
4086 }
4087
Benny Prijono87ef89a2007-01-14 00:39:45 +00004088 /* Add UDP transport unless it's disabled. */
4089 if (!app_config.no_udp) {
4090 pjsua_acc_id aid;
4091
4092 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4093 &app_config.udp_cfg,
4094 &transport_id);
4095 if (status != PJ_SUCCESS)
4096 goto on_error;
4097
4098 /* Add local account */
4099 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4100 //pjsua_acc_set_transport(aid, transport_id);
4101 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004102
4103 if (app_config.udp_cfg.port == 0) {
4104 pjsua_transport_info ti;
4105 pj_sockaddr_in *a;
4106
4107 pjsua_transport_get_info(transport_id, &ti);
4108 a = (pj_sockaddr_in*)&ti.local_addr;
4109
4110 tcp_cfg.port = pj_ntohs(a->sin_port);
4111 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004112 }
4113
Benny Prijonoe93e2872006-06-28 16:46:49 +00004114 /* Add TCP transport unless it's disabled */
4115 if (!app_config.no_tcp) {
4116 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004117 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004118 &transport_id);
4119 if (status != PJ_SUCCESS)
4120 goto on_error;
4121
4122 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004123 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004124 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4125
4126 }
4127
Benny Prijonoe93e2872006-06-28 16:46:49 +00004128
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004129#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4130 /* Add TLS transport when application wants one */
4131 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004132
4133 pjsua_acc_id acc_id;
4134
4135 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004136 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004137 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004138 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004139 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004140 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004141 if (status != PJ_SUCCESS)
4142 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004143
4144 /* Add local account */
4145 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4146 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004147 }
4148#endif
4149
Benny Prijonoe93e2872006-06-28 16:46:49 +00004150 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004151 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004152 status = -1;
4153 goto on_error;
4154 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004155
4156
4157 /* Add accounts */
4158 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004159 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004160 if (status != PJ_SUCCESS)
4161 goto on_error;
4162 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4163 }
4164
4165 /* Add buddies */
4166 for (i=0; i<app_config.buddy_cnt; ++i) {
4167 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4168 if (status != PJ_SUCCESS)
4169 goto on_error;
4170 }
4171
4172 /* Optionally set codec orders */
4173 for (i=0; i<app_config.codec_cnt; ++i) {
4174 pjsua_codec_set_priority(&app_config.codec_arg[i],
4175 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4176 }
4177
Benny Prijonofce28542007-12-09 15:41:10 +00004178 /* Optionally disable some codec */
4179 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4180 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4181 }
4182
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004183 /* Add RTP transports */
4184 status = pjsua_media_transports_create(&app_config.rtp_cfg);
4185 if (status != PJ_SUCCESS)
4186 goto on_error;
4187
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004188 /* Set sound device latency */
4189 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4190
Benny Prijono22a300a2006-06-14 20:04:55 +00004191 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004192#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004193 if (app_config.null_audio) {
4194 status = pjsua_set_null_snd_dev();
4195 if (status != PJ_SUCCESS)
4196 return status;
4197 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004198#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004199
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004200 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4201 app_config.playback_dev != PJSUA_INVALID_ID)
4202 {
4203 status = pjsua_set_snd_dev(app_config.capture_dev,
4204 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004205 if (status != PJ_SUCCESS)
4206 goto on_error;
4207 }
4208
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004209 return PJ_SUCCESS;
4210
4211on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004212 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004213 return status;
4214}
4215
4216
Benny Prijonoebc32c32008-06-12 13:30:39 +00004217static int stdout_refresh_proc(void *arg)
4218{
4219 PJ_UNUSED_ARG(arg);
4220
4221 /* Set thread to lowest priority so that it doesn't clobber
4222 * stdout output
4223 */
4224 pj_thread_set_prio(pj_thread_this(),
4225 pj_thread_get_prio_min(pj_thread_this()));
4226
4227 while (!stdout_refresh_quit) {
4228 pj_thread_sleep(stdout_refresh * 1000);
4229 puts(stdout_refresh_text);
4230 fflush(stdout);
4231 }
4232
4233 return 0;
4234}
4235
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004236pj_status_t app_main(void)
4237{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004238 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004239 pj_status_t status;
4240
4241 /* Start pjsua */
4242 status = pjsua_start();
4243 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004244 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004245 return status;
4246 }
4247
Benny Prijonoebc32c32008-06-12 13:30:39 +00004248 /* Start console refresh thread */
4249 if (stdout_refresh > 0) {
4250 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4251 NULL, 0, 0, &stdout_refresh_thread);
4252 }
4253
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004254 console_app_main(&uri_arg);
4255
Benny Prijonoebc32c32008-06-12 13:30:39 +00004256 if (stdout_refresh_thread) {
4257 stdout_refresh_quit = PJ_TRUE;
4258 pj_thread_join(stdout_refresh_thread);
4259 pj_thread_destroy(stdout_refresh_thread);
4260 }
4261
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004262 return PJ_SUCCESS;
4263}
4264
4265pj_status_t app_destroy(void)
4266{
Benny Prijonof762ee72006-12-01 11:14:37 +00004267 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004268 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004269
Benny Prijonoe909eac2006-07-27 22:04:56 +00004270#ifdef STEREO_DEMO
4271 if (app_config.snd) {
4272 pjmedia_snd_port_destroy(app_config.snd);
4273 app_config.snd = NULL;
4274 }
4275#endif
4276
Benny Prijono91d20f42008-06-14 19:42:37 +00004277 /* Close ringback port */
4278 if (app_config.ringback_port &&
4279 app_config.ringback_slot != PJSUA_INVALID_ID)
4280 {
4281 pjsua_conf_remove_port(app_config.ringback_slot);
4282 app_config.ringback_slot = PJSUA_INVALID_ID;
4283 pjmedia_port_destroy(app_config.ringback_port);
4284 app_config.ringback_port = NULL;
4285 }
4286
4287 /* Close ring port */
4288 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4289 pjsua_conf_remove_port(app_config.ring_slot);
4290 app_config.ring_slot = PJSUA_INVALID_ID;
4291 pjmedia_port_destroy(app_config.ring_port);
4292 app_config.ring_port = NULL;
4293 }
4294
Benny Prijono4af234b2007-01-24 02:02:09 +00004295 /* Close tone generators */
4296 for (i=0; i<app_config.tone_count; ++i) {
4297 pjsua_conf_remove_port(app_config.tone_slots[i]);
4298 }
4299
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004300 if (app_config.pool) {
4301 pj_pool_release(app_config.pool);
4302 app_config.pool = NULL;
4303 }
4304
Benny Prijonof762ee72006-12-01 11:14:37 +00004305 status = pjsua_destroy();
4306
4307 pj_bzero(&app_config, sizeof(app_config));
4308
4309 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004310}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004311
4312
4313#ifdef STEREO_DEMO
4314static void stereo_demo()
4315{
4316 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004317 pj_status_t status;
4318
4319 /* Disable existing sound device */
4320 conf = pjsua_set_no_snd_dev();
4321
Benny Prijonoe909eac2006-07-27 22:04:56 +00004322 /* Create stereo-mono splitter/combiner */
4323 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004324 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004325 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004326 2 * conf->info.samples_per_frame,
4327 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004328 0 /* options */,
4329 &splitter);
4330 pj_assert(status == PJ_SUCCESS);
4331
4332 /* Connect channel0 (left channel?) to conference port slot0 */
4333 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4334 0 /*options*/,
4335 conf);
4336 pj_assert(status == PJ_SUCCESS);
4337
4338 /* Create reverse channel for channel1 (right channel?)... */
4339 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4340 splitter,
4341 1 /* ch1 */,
4342 0 /* options */,
4343 &ch1);
4344 pj_assert(status == PJ_SUCCESS);
4345
4346 /* .. and register it to conference bridge (it would be slot1
4347 * if there's no other devices connected to the bridge)
4348 */
4349 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4350 pj_assert(status == PJ_SUCCESS);
4351
4352 /* Create sound device */
4353 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004354 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004355 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004356 2 * conf->info.samples_per_frame,
4357 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004358 0, &app_config.snd);
4359 pj_assert(status == PJ_SUCCESS);
4360
4361
4362 /* Connect the splitter to the sound device */
4363 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4364 pj_assert(status == PJ_SUCCESS);
4365
4366}
4367#endif
4368